From 3751379f1e9a4c215fb6eb898b4ccc67659b9ace Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Tue, 10 May 2022 21:51:50 +0700 Subject: initial commit 2 --- addons/website/static/src/js/editor/widget_link.js | 104 +++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 addons/website/static/src/js/editor/widget_link.js (limited to 'addons/website/static/src/js/editor/widget_link.js') diff --git a/addons/website/static/src/js/editor/widget_link.js b/addons/website/static/src/js/editor/widget_link.js new file mode 100644 index 00000000..de3c1d9c --- /dev/null +++ b/addons/website/static/src/js/editor/widget_link.js @@ -0,0 +1,104 @@ +odoo.define('website.editor.link', function (require) { +'use strict'; + +var weWidgets = require('wysiwyg.widgets'); +var wUtils = require('website.utils'); + +weWidgets.LinkDialog.include({ + xmlDependencies: (weWidgets.LinkDialog.prototype.xmlDependencies || []).concat( + ['/website/static/src/xml/website.editor.xml'] + ), + events: _.extend({}, weWidgets.LinkDialog.prototype.events || {}, { + 'change select[name="link_anchor"]': '_onAnchorChange', + 'input input[name="url"]': '_onURLInput', + }), + custom_events: _.extend({}, weWidgets.LinkDialog.prototype.custom_events || {}, { + website_url_chosen: '_onAutocompleteClose', + }), + LINK_DEBOUNCE: 1000, + + /** + * @constructor + */ + init: function () { + this._super.apply(this, arguments); + this._adaptPageAnchor = _.debounce(this._adaptPageAnchor, this.LINK_DEBOUNCE); + }, + /** + * Allows the URL input to propose existing website pages. + * + * @override + */ + start: function () { + var def = this._super.apply(this, arguments); + wUtils.autocompleteWithPages(this, this.$('input[name="url"]')); + this.opened(this._adaptPageAnchor.bind(this)); + return def; + }, + + //-------------------------------------------------------------------------- + // Private + //-------------------------------------------------------------------------- + + /** + * @private + */ + _adaptPageAnchor: function () { + var urlInputValue = this.$('input[name="url"]').val(); + var $pageAnchor = this.$('.o_link_dialog_page_anchor'); + var isFromWebsite = urlInputValue[0] === '/'; + var $selectMenu = this.$('select[name="link_anchor"]'); + var $anchorsLoading = this.$('.o_anchors_loading'); + + if ($selectMenu.data("anchor-for") !== urlInputValue) { // avoid useless query + $anchorsLoading.removeClass('d-none'); + $pageAnchor.toggleClass('d-none', !isFromWebsite); + $selectMenu.empty(); + wUtils.loadAnchors(urlInputValue).then(function (anchors) { + _.each(anchors, function (anchor) { + $selectMenu.append($('