summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/public
diff options
context:
space:
mode:
authorqsm-odoo <qsm@odoo.com>2019-02-13 15:21:03 +0000
committerqsm-odoo <qsm@odoo.com>2019-02-26 17:02:58 +0000
commit3f4e760f34b4e961ccc5587d5d1cb6eed1680e2a (patch)
tree49fc07e685e74e149885128cf8bd2bde68dc5703 /addons/web/static/src/js/public
parentff9c9341d6ed4e9ab18a0aa02e79441999d658f1 (diff)
[MOV] web, *: simple moves of JS files to 'public' folder
* web_editor, website, survey, im_livechat Part of https://github.com/odoo/odoo/pull/29442 task-1932066
Diffstat (limited to 'addons/web/static/src/js/public')
-rw-r--r--addons/web/static/src/js/public/public_root_instance.js23
-rw-r--r--addons/web/static/src/js/public/public_widget.js167
2 files changed, 190 insertions, 0 deletions
diff --git a/addons/web/static/src/js/public/public_root_instance.js b/addons/web/static/src/js/public/public_root_instance.js
new file mode 100644
index 00000000000..ce0cfa61bc6
--- /dev/null
+++ b/addons/web/static/src/js/public/public_root_instance.js
@@ -0,0 +1,23 @@
+odoo.define('root.widget', function (require) {
+"use strict";
+
+var ServiceProviderMixin = require('web.ServiceProviderMixin');
+var Widget = require('web.Widget');
+
+/**
+ * This is the root widget in the frontend bundle when website is not installed.
+ *
+ * This widget is important, because the tour manager needs a root widget in
+ * order to work. The root widget must be a service provider with the ajax
+ * service, so that the tour manager can let the server know when tours have
+ * been consumed.
+ */
+var PublicRootWidget = Widget.extend(ServiceProviderMixin, {
+ init: function () {
+ this._super.apply(this, arguments);
+ ServiceProviderMixin.init.call(this);
+ }
+});
+
+return new PublicRootWidget();
+});
diff --git a/addons/web/static/src/js/public/public_widget.js b/addons/web/static/src/js/public/public_widget.js
new file mode 100644
index 00000000000..462dcd9bae9
--- /dev/null
+++ b/addons/web/static/src/js/public/public_widget.js
@@ -0,0 +1,167 @@
+odoo.define('web_editor.root_widget', function (require) {
+'use strict';
+
+var Class = require('web.Class');
+var dom = require('web.dom');
+var mixins = require('web.mixins');
+var session = require('web.session');
+var Widget = require('web.Widget');
+
+/**
+ * Specialized Widget which automatically instantiates child widgets to attach
+ * to internal DOM elements once it is started. The widgets to instantiate are
+ * known thanks to a linked registry which contains info about the widget
+ * classes and jQuery selectors to use to find the elements to attach them to.
+ */
+var RootWidget = Widget.extend({
+ custom_events: _.extend({}, Widget.prototype.custom_events || {}, {
+ registry_update: '_onRegistryUpdate',
+ get_session: '_onGetSession',
+ }),
+ /**
+ * @constructor
+ */
+ init: function () {
+ this._super.apply(this, arguments);
+ this._widgets = [];
+ this._listenToUpdates = false;
+ this._getRegistry().setParent(this);
+ },
+ /**
+ * @override
+ * @see _attachComponents
+ */
+ start: function () {
+ var defs = [this._super.apply(this, arguments)];
+
+ defs.push(this._attachComponents());
+ this._listenToUpdates = true;
+
+ return $.when.apply($, defs);
+ },
+
+ //--------------------------------------------------------------------------
+ // Private
+ //--------------------------------------------------------------------------
+
+ /**
+ * Instantiates a child widget according to the given registry data.
+ *
+ * @private
+ * @param {Object} childInfo
+ * @param {function} childInfo.Widget - the widget class to instantiate
+ * @param {string} childInfo.selector
+ * the jQuery selector to use to find the internal DOM element which
+ * needs to be attached to the instantiated widget
+ * @param {jQuery} [$from] - only check DOM elements which are descendant of
+ * the given one. If not given, use this.$el.
+ * @returns {Deferred}
+ */
+ _attachComponent: function (childInfo, $from) {
+ var self = this;
+ var $elements = dom.cssFind($from || this.$el, childInfo.selector);
+ var defs = _.map($elements, function (element) {
+ var w = new childInfo.Widget(self);
+ self._widgets.push(w);
+ return w.attachTo(element);
+ });
+ return $.when.apply($, defs);
+ },
+ /**
+ * Instantiates the child widgets that need to be according to the linked
+ * registry.
+ *
+ * @private
+ * @param {jQuery} [$from] - only check DOM elements which are descendant of
+ * the given one. If not given, use this.$el.
+ * @returns {Deferred}
+ */
+ _attachComponents: function ($from) {
+ var self = this;
+ var childInfos = this._getRegistry().get();
+ var defs = _.map(childInfos, function (childInfo) {
+ return self._attachComponent(childInfo, $from);
+ });
+ return $.when.apply($, defs);
+ },
+ /**
+ * Returns the `RootWidgetRegistry` instance that is linked to this
+ * `RootWidget` instance.
+ *
+ * @abstract
+ * @private
+ * @returns {RootWidgetRegistry}
+ */
+ _getRegistry: function () {},
+
+ //--------------------------------------------------------------------------
+ // Handlers
+ //--------------------------------------------------------------------------
+
+ /**
+ * Get the curuent session module.
+ *
+ * @private
+ * @param {OdooEvent} ev
+ */
+ _onGetSession: function (event) {
+ if (event.data.callback) {
+ event.data.callback(session);
+ }
+ },
+ /**
+ * Called when the linked registry is updated after this `RootWidget`
+ *
+ * @private
+ * @param {OdooEvent} ev
+ */
+ _onRegistryUpdate: function (ev) {
+ ev.stopPropagation();
+ if (this._listenToUpdates) {
+ this._attachComponent(ev.data);
+ }
+ },
+});
+
+var RootWidgetRegistry = Class.extend(mixins.EventDispatcherMixin, {
+ /**
+ * @constructor
+ */
+ init: function () {
+ mixins.EventDispatcherMixin.init.call(this);
+ this._registry = [];
+ },
+
+ //--------------------------------------------------------------------------
+ // Public
+ //--------------------------------------------------------------------------
+
+ /**
+ * Adds an element to the registry (info of what and how to instantiate).
+ *
+ * @param {function} Widget - the widget class to instantiate
+ * @param {string} selector
+ * the jQuery selector to use to find the internal DOM element which
+ * needs to be attached to the instantiated widget
+ */
+ add: function (Widget, selector) {
+ var registryInfo = {
+ Widget: Widget,
+ selector: selector,
+ };
+ this._registry.push(registryInfo);
+ this.trigger_up('registry_update', registryInfo);
+ },
+ /**
+ * Retrieves all the registry elements.
+ */
+ get: function () {
+ return this._registry;
+ },
+});
+
+return {
+ RootWidget: RootWidget,
+ RootWidgetRegistry: RootWidgetRegistry,
+};
+});