Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. import { createWidget } from 'discourse/widgets/widget';
  2. import { h } from 'virtual-dom';
  3. import { ajax } from 'discourse/lib/ajax';
  4. import { popupAjaxError } from 'discourse/lib/ajax-error';
  5.  
  6.  
  7.  
  8.  
  9. export default createWidget('widget-bookmark', {
  10. tagName: 'div.widget-bookmark.widget-container',
  11. buildKey: (attrs) => 'widget-bookmark',
  12.  
  13. defaultState() {
  14. return {
  15. data: [],
  16. loaded: false,
  17. contents: []
  18. }
  19. },
  20.  
  21. getData(){
  22.  
  23. var user = Discourse.User.currentProp('username');
  24. var bookmarked;
  25. let self = this;
  26. if (user)
  27. {
  28. self.state.loaded = true;
  29. ajax(`user_actions.json?username=${user}&filter=3&no_results_help_key=user_activity.no_bookmarks`)
  30. .then(function(res,err){
  31.  
  32. bookmarked = res.user_actions;
  33. for(var i = 0 ; i < bookmarked.length ; i++)
  34. {
  35. self.state.data = bookmarked[i];
  36.  
  37. self.state.contents.push(h("button.bookmark-widget.btn.widget-button.no-text#"+bookmarked[i].post_id, [h("i.fa.fa-remove#"+bookmarked[i].post_id)]));
  38.  
  39. self.state.contents.push(h("div.bookmarkBlock",[h("a.bookmarded-link",{attributes:
  40. {href: "/t/" + bookmarked[i].slug + "/" + bookmarked[i].topic_id }},
  41. h("span.bookmarked-title", bookmarked[i].title))]));
  42.  
  43. self.state.contents.push(h("br"));
  44. }
  45. self.scheduleRerender();
  46. });
  47. }
  48. },
  49.  
  50. html(attrs, state) {
  51.  
  52. if (state.loaded == false)
  53. {
  54. state.contents.push(h("span.title-bookmark",I18n.t("main.bookmark-you")))
  55. state.contents.push(h("br"));
  56. this.getData();}
  57. return h('div.widget-inner', state.contents);
  58. },
  59. click(e){
  60. let self = this;
  61. if (e.target.localName != "button" && e.target.localName != "i")
  62. return;
  63. return ajax("/posts/" + e.target.id + "/bookmark", {
  64. type: 'PUT',
  65. data: { bookmarked: false }
  66. }).then(function (res, err){
  67. if (err) {
  68. popupAjaxError(err);
  69. }
  70. else
  71. {
  72. self.state.loaded = false;
  73. self.state.contents = [];
  74. self.scheduleRerender();
  75. };
  76. });
  77. }
  78.  
  79. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement