This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/app/components/bookmark-list.js
Bianca Nenciu a09b6fe114
FEATURE: Save scroll position on bookmarks page (#15296)
Clicking on a bookmark and then back will preserve the scrolling
position.
2021-12-15 15:27:09 +02:00

119 lines
2.8 KiB
JavaScript

import Component from "@ember/component";
import { action } from "@ember/object";
import { schedule } from "@ember/runloop";
import bootbox from "bootbox";
import discourseDebounce from "discourse-common/lib/debounce";
import { openBookmarkModal } from "discourse/controllers/bookmark";
import {
openLinkInNewTab,
shouldOpenInNewTab,
} from "discourse/lib/click-track";
import Scrolling from "discourse/mixins/scrolling";
import I18n from "I18n";
import { Promise } from "rsvp";
export default Component.extend(Scrolling, {
classNames: ["bookmark-list-wrapper"],
didInsertElement() {
this._super(...arguments);
this.bindScrolling();
this.scrollToLastPosition();
},
willDestroyElement() {
this._super(...arguments);
this.unbindScrolling();
},
scrollToLastPosition() {
let scrollTo = this.session.bookmarkListScrollPosition;
if (scrollTo && scrollTo >= 0) {
schedule("afterRender", () => {
discourseDebounce(
this,
function () {
if (this.element && !this.isDestroying && !this.isDestroyed) {
window.scrollTo(0, scrollTo + 1);
}
},
0
);
});
}
},
scrolled() {
this._super(...arguments);
this.session.set("bookmarkListScrollPosition", window.scrollY);
},
@action
removeBookmark(bookmark) {
return new Promise((resolve, reject) => {
const deleteBookmark = () => {
bookmark
.destroy()
.then(() => {
this.appEvents.trigger(
"bookmarks:changed",
null,
bookmark.attachedTo()
);
this._removeBookmarkFromList(bookmark);
resolve(true);
})
.catch((error) => {
reject(error);
});
};
if (!bookmark.reminder_at) {
return deleteBookmark();
}
bootbox.confirm(I18n.t("bookmarks.confirm_delete"), (result) => {
if (result) {
deleteBookmark();
} else {
resolve(false);
}
});
});
},
@action
screenExcerptForExternalLink(event) {
if (event.target && event.target.tagName === "A") {
let link = event.target;
if (shouldOpenInNewTab(link.href)) {
openLinkInNewTab(link);
}
}
},
@action
editBookmark(bookmark) {
openBookmarkModal(bookmark, {
onAfterSave: (savedData) => {
this.appEvents.trigger(
"bookmarks:changed",
savedData,
bookmark.attachedTo()
);
this.reload();
},
onAfterDelete: () => {
this.reload();
},
});
},
@action
togglePinBookmark(bookmark) {
bookmark.togglePin().then(this.reload);
},
_removeBookmarkFromList(bookmark) {
this.content.removeObject(bookmark);
},
});