Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { createWidget } from 'discourse/widgets/widget';
- import { h } from 'virtual-dom';
- import { ajax } from 'discourse/lib/ajax';
- import { popupAjaxError } from 'discourse/lib/ajax-error';
- export default createWidget('widget-bookmark', {
- tagName: 'div.widget-bookmark.widget-container',
- buildKey: (attrs) => 'widget-bookmark',
- defaultState() {
- return {
- data: [],
- loaded: false,
- contents: []
- }
- },
- getData(){
- var user = Discourse.User.currentProp('username');
- var bookmarked;
- let self = this;
- if (user)
- {
- self.state.loaded = true;
- ajax(`user_actions.json?username=${user}&filter=3&no_results_help_key=user_activity.no_bookmarks`)
- .then(function(res,err){
- bookmarked = res.user_actions;
- for(var i = 0 ; i < bookmarked.length ; i++)
- {
- self.state.data = bookmarked[i];
- 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)]));
- self.state.contents.push(h("div.bookmarkBlock",[h("a.bookmarded-link",{attributes:
- {href: "/t/" + bookmarked[i].slug + "/" + bookmarked[i].topic_id }},
- h("span.bookmarked-title", bookmarked[i].title))]));
- self.state.contents.push(h("br"));
- }
- self.scheduleRerender();
- });
- }
- },
- html(attrs, state) {
- if (state.loaded == false)
- {
- state.contents.push(h("span.title-bookmark",I18n.t("main.bookmark-you")))
- state.contents.push(h("br"));
- this.getData();}
- return h('div.widget-inner', state.contents);
- },
- click(e){
- let self = this;
- if (e.target.localName != "button" && e.target.localName != "i")
- return;
- return ajax("/posts/" + e.target.id + "/bookmark", {
- type: 'PUT',
- data: { bookmarked: false }
- }).then(function (res, err){
- if (err) {
- popupAjaxError(err);
- }
- else
- {
- self.state.loaded = false;
- self.state.contents = [];
- self.scheduleRerender();
- };
- });
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement