diff options
| author | qsm-odoo <qsm@odoo.com> | 2017-06-20 16:27:28 +0200 |
|---|---|---|
| committer | qsm-odoo <qsm@odoo.com> | 2017-06-21 12:56:13 +0200 |
| commit | 85d1e76ddbac9aeedfba4710443b74cbcc6586be (patch) | |
| tree | ce266dbcf69bb3aae986bc3cdc91536c03597ef6 /addons/web/static/src/js/libs/jquery.js | |
| parent | 9fc7a04b4b4bc68535620fda7cfe2bafba6e0081 (diff) | |
[IMP] web, *: reorganize our libraries' extensions
* web_editor, web_tour, website_event_track
Before this commit, one JS file was responsible for all the library
extensions and fixes. There were two main problems with this system:
- The file was only available in the backend (as some extensions are
made on backend-only libraries).
- Some other modules were doing library customizations.
This commit creates one file per library to regroup their associated
customization and adds them in the right asset. These are placed in
the web module and every other module should avoid doing customizations.
This allows to have libraries consistency accross all modules and
between the backend and the frontend.
Diffstat (limited to 'addons/web/static/src/js/libs/jquery.js')
| -rw-r--r-- | addons/web/static/src/js/libs/jquery.js | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/addons/web/static/src/js/libs/jquery.js b/addons/web/static/src/js/libs/jquery.js new file mode 100644 index 00000000000..ace99a0f8d3 --- /dev/null +++ b/addons/web/static/src/js/libs/jquery.js @@ -0,0 +1,98 @@ +odoo.define('web.jquery.extensions', function () { +'use strict'; + +/** + * The jquery library extensions and fixes should be done here to avoid patching + * in place. + */ + +// jQuery selectors extensions +$.extend($.expr[':'], { + containsLike: function (element, index, matches){ + return element.innerHTML.toUpperCase().indexOf(matches[3].toUpperCase()) >= 0; + }, + containsExact: function (element, index, matches){ + return $.trim(element.innerHTML) === matches[3]; + }, + /** + * Note all escaped characters need to be double escaped inside of the + * expression, so "\(" needs to be "\\(" + */ + containsRegex: function (element, index, matches){ + var regreg = /^\/((?:\\\/|[^\/])+)\/([mig]{0,3})$/, + reg = regreg.exec(matches[3]); + return reg ? new RegExp(reg[1], reg[2]).test($.trim(element.innerHTML)) : false; + }, + propChecked: function (element, index, matches) { + return $(element).prop("checked") === true; + }, + propSelected: function (element, index, matches) { + return $(element).prop("selected") === true; + }, + propValue: function (element, index, matches) { + return $(element).prop("value") === matches[3]; + }, + propValueContains: function (element, index, matches) { + return $(element).prop("value") && $(element).prop("value").indexOf(matches[3]) !== -1; + }, + hasData: function (element) { + return !!_.toArray(element.dataset).length; + }, + data: function (element, index, matches) { + return $(element).data(matches[3]); + }, +}); + +// jQuery functions extensions +$.fn.extend({ + /** + * Returns all the attributes of a DOM element (first one in the jQuery + * set). + * + * @returns {Object} attribute name -> attribute value + */ + getAttributes: function () { + var o = {}; + if (this.length) { + var attrs = this[0].attributes; + for (var i = 0, l = attrs.length ; i < l ; i++) { + var attr = attrs.item(i); + o[attr.name] = attr.value; + } + } + return o; + }, + /** + * Makes DOM elements bounce the way Odoo decided it. + */ + odooBounce: function () { + return this.each(function () { + var $elem = $(this); + $elem + .css('box-sizing', 'content-box') + .effect('bounce', {distance: 18, times: 5}, 250, function () { + $elem.css('box-sizing', ''); + }); + }); + }, + /** + * Allows to bind events to a handler just as the standard `$.on` function + * but binds the handler so that it is executed before any already-attached + * handler for the same events. + * + * @see jQuery.on + */ + prependEvent: function (events, selector, data, handler) { + this.on.apply(this, arguments); + + events = events.split(' '); + return this.each(function () { + var el = this; + _.each(events, function (evName) { + var handler = $._data(el, 'events')[evName].pop(); + $._data(el, 'events')[evName].unshift(handler); + }); + }); + }, +}); +}); |
