1 openerp.web.search = function(instance) {
2 var QWeb = instance.web.qweb,
4 _lt = instance.web._lt;
6 sum: function (obj) { return _.reduce(obj, function (a, b) { return a + b; }, 0); }
10 var my = instance.web.search = {};
13 my.FacetValue = B.Model.extend({
16 my.FacetValues = B.Collection.extend({
19 my.Facet = B.Model.extend({
20 initialize: function (attrs) {
21 var values = attrs.values;
24 B.Model.prototype.initialize.apply(this, arguments);
26 this.values = new my.FacetValues(values || []);
27 this.values.on('add remove change reset', function () {
28 this.trigger('change', this);
32 if (key !== 'values') {
33 return B.Model.prototype.get.call(this, key);
35 return this.values.toJSON();
37 set: function (key, value) {
38 if (key !== 'values') {
39 return B.Model.prototype.set.call(this, key, value);
41 this.values.reset(value);
45 var attrs = this.attributes;
46 for(var att in attrs) {
47 if (!attrs.hasOwnProperty(att) || att === 'field') {
50 out[att] = attrs[att];
52 out.values = this.values.toJSON();
56 my.SearchQuery = B.Collection.extend({
58 initialize: function () {
59 B.Collection.prototype.initialize.apply(
61 this.on('change', function (facet) {
62 if(!facet.values.isEmpty()) { return; }
67 add: function (values, options) {
68 options || (options = {});
69 if (!(values instanceof Array)) {
73 _(values).each(function (value) {
74 var model = this._prepareModel(value, options);
75 var previous = this.detect(function (facet) {
76 return facet.get('category') === model.get('category')
77 && facet.get('field') === model.get('field');
80 previous.values.add(model.get('values'));
83 B.Collection.prototype.add.call(this, model, options);
87 toggle: function (value, options) {
88 options || (options = {});
90 var facet = this.detect(function (facet) {
91 return facet.get('category') === value.category
92 && facet.get('field') === value.field;
95 return this.add(value, options);
99 _(value.values).each(function (val) {
100 var already_value = facet.values.detect(function (v) {
101 return v.get('value') === val.value
102 && v.get('label') === val.label;
106 facet.values.remove(already_value, {silent: true});
108 facet.values.add(val, {silent: true});
112 // "Commit" changes to values array as a single call, so observers of
113 // change event don't get misled by intermediate incomplete toggling
115 facet.trigger('change', facet);
120 function assert(condition, message) {
122 throw new Error(message);
125 my.InputView = instance.web.Widget.extend({
126 template: 'SearchView.InputView',
128 var p = this._super.apply(this, arguments);
129 this.$element.on('focus', this.proxy('onFocus'));
130 this.$element.on('blur', this.proxy('onBlur'));
131 this.$element.on('keydown', this.proxy('onKeydown'));
134 onFocus: function () {
135 this.trigger('focused', this);
137 onBlur: function () {
138 this.$element.text('');
139 this.trigger('blurred', this);
141 getSelection: function () {
143 var root = this.$element[0].childNodes[0];
144 if (!root || !root.textContent) {
145 // if input does not have a child node, or the child node is an
146 // empty string, then the selection can only be (0, 0)
147 return {start: 0, end: 0};
149 if (window.getSelection) {
150 var domRange = window.getSelection().getRangeAt(0);
151 assert(domRange.startContainer === root,
152 "selection should be in the input view");
153 assert(domRange.endContainer === root,
154 "selection should be in the input view");
156 start: domRange.startOffset,
157 end: domRange.endOffset
159 } else if (document.selection) {
160 var ieRange = document.selection.createRange();
161 var rangeParent = ieRange.parentElement();
162 assert(rangeParent === root,
163 "selection should be in the input view");
164 var offsetRange = document.body.createTextRange();
165 offsetRange = offsetRange.moveToElementText(rangeParent);
166 offsetRange.setEndPoint("EndToStart", ieRange);
167 var start = offsetRange.text.length;
170 end: start + ieRange.text.length
173 throw new Error("Could not get caret position");
175 onKeydown: function (e) {
178 // Do not insert newline, but let it bubble so searchview can use it
179 case $.ui.keyCode.ENTER:
183 // FIXME: may forget content if non-empty but caret at index 0, ok?
184 case $.ui.keyCode.BACKSPACE:
185 sel = this.getSelection();
186 if (sel.start === 0 && sel.start === sel.end) {
188 var preceding = this.getParent().siblingSubview(this, -1);
189 if (preceding && (preceding instanceof my.FacetView)) {
190 preceding.model.destroy();
195 // let left/right events propagate to view if caret is at input border
196 // and not a selection
197 case $.ui.keyCode.LEFT:
198 sel = this.getSelection();
199 if (sel.start !== 0 || sel.start !== sel.end) {
203 case $.ui.keyCode.RIGHT:
204 sel = this.getSelection();
205 var len = this.$element.text().length;
206 if (sel.start !== len || sel.start !== sel.end) {
213 my.FacetView = instance.web.Widget.extend({
214 template: 'SearchView.FacetView',
215 init: function (parent, model) {
218 this.model.on('change', this.model_changed, this);
220 destroy: function () {
221 this.model.off('change', this.model_changed, this);
226 this.$element.on('focus', function () { self.trigger('focused', self); });
227 this.$element.on('blur', function () { self.trigger('blurred', self); });
228 this.$element.on('click', function (e) {
229 if ($(e.target).is('.oe_facet_remove')) {
230 self.model.destroy();
233 self.$element.focus();
236 this.$element.on('keydown', function (e) {
237 var keys = $.ui.keyCode;
241 self.model.destroy();
245 var $e = self.$element.find('> span:last-child');
246 var q = $.when(this._super());
247 return q.pipe(function () {
248 var values = self.model.values.map(function (value) {
249 return new my.FacetValueView(self, value).appendTo($e);
252 return $.when.apply(null, values);
255 model_changed: function () {
256 this.$element.text(this.$element.text() + '*');
259 my.FacetValueView = instance.web.Widget.extend({
260 template: 'SearchView.FacetView.Value',
261 init: function (parent, model) {
264 this.model.on('change', this.model_changed, this);
266 destroy: function () {
267 this.model.off('change', this.model_changed, this);
270 model_changed: function () {
271 this.$element.text(this.$element.text() + '*');
275 instance.web.SearchView = instance.web.Widget.extend(/** @lends instance.web.SearchView# */{
276 template: "SearchView",
278 * @constructs instance.web.SearchView
279 * @extends instance.web.Widget
287 init: function(parent, dataset, view_id, defaults, hidden) {
289 this.dataset = dataset;
290 this.model = dataset.model;
291 this.view_id = view_id;
293 this.defaults = defaults || {};
294 this.has_defaults = !_.isEmpty(this.defaults);
299 this.hidden = !!hidden;
300 this.headless = this.hidden && !this.has_defaults;
302 this.input_subviews = [];
304 this.ready = $.Deferred();
308 var p = this._super();
310 this.setup_global_completion();
311 this.query = new my.SearchQuery()
312 .on('add change reset remove', this.proxy('do_search'))
313 .on('add change reset remove', this.proxy('renderFacets'));
316 this.$element.hide();
319 this.ready.resolve();
321 var load_view = this.rpc("/web/searchview/load", {
323 view_id: this.view_id,
324 context: this.dataset.get_context() });
327 .pipe(this.on_loaded)
329 self.ready.reject.apply(null, arguments);
333 this.$element.on('keydown',
334 '.oe_searchview_input, .oe_searchview_facet', function (e) {
336 case $.ui.keyCode.LEFT:
337 self.focusPreceding(this);
340 case $.ui.keyCode.RIGHT:
341 self.focusFollowing(this);
347 this.$element.on('click', '.oe_searchview_clear', function (e) {
348 e.stopImmediatePropagation();
351 this.$element.on('click', '.oe_searchview_unfold_drawer', function (e) {
352 e.stopImmediatePropagation();
353 self.$element.toggleClass('oe_searchview_open_drawer');
355 // Focus last input if the view itself is clicked (empty section of
357 this.$element.on('click', function (e) {
358 if (e.target === self.$element.find('.oe_searchview_facets')[0]) {
359 self.$element.find('.oe_searchview_input:last').focus();
362 // when the completion list opens/refreshes, automatically select the
363 // first completion item so if the user just hits [RETURN] or [TAB] it
364 // automatically selects it
365 this.$element.on('autocompleteopen', function () {
366 var menu = self.$element.data('autocomplete').menu;
368 $.Event({ type: "mouseenter" }),
369 menu.element.children().first());
372 return $.when(p, this.ready);
375 this.$element.show();
378 this.$element.hide();
381 subviewForRoot: function (subview_root) {
382 return _(this.input_subviews).detect(function (subview) {
383 return subview.$element[0] === subview_root;
386 siblingSubview: function (subview, direction, wrap_around) {
387 var index = _(this.input_subviews).indexOf(subview) + direction;
388 if (wrap_around && index < 0) {
389 index = this.input_subviews.length - 1;
390 } else if (wrap_around && index >= this.input_subviews.length) {
393 return this.input_subviews[index];
395 focusPreceding: function (subview_root) {
396 return this.siblingSubview(
397 this.subviewForRoot(subview_root), -1, true)
400 focusFollowing: function (subview_root) {
401 return this.siblingSubview(
402 this.subviewForRoot(subview_root), +1, true)
407 * Sets up thingie where all the mess is put?
409 select_for_drawer: function () {
410 return _(this.inputs).filter(function (input) {
411 return input.in_drawer();
415 * Sets up search view's view-wide auto-completion widget
417 setup_global_completion: function () {
420 // autocomplete only correctly handles being initialized on the actual
421 // editable element (and only an element with a @value in 1.8 e.g.
422 // input or textarea), cheat by setting val() on $element
423 this.$element.on('keydown', function () {
424 // keydown is triggered *before* the element's value is set, so
425 // delay this. Pray that setTimeout are executed in FIFO (if they
426 // have the same delay) as autocomplete uses the exact same trick.
427 // FIXME: brittle as fuck
428 setTimeout(function () {
429 self.$element.val(self.currentInputValue());
434 this.$element.autocomplete({
435 source: this.proxy('complete_global_search'),
436 select: this.proxy('select_completion'),
437 focus: function (e) { e.preventDefault(); },
441 }).data('autocomplete')._renderItem = function (ul, item) {
442 // item of completion list
443 var $item = $( "<li></li>" )
444 .data( "item.autocomplete", item )
447 if (item.facet !== undefined) {
448 // regular completion item
451 ? $('<a>').html(item.label)
452 : $('<a>').text(item.value));
454 return $item.text(item.label)
456 borderTop: '1px solid #cccccc',
467 * Gets value out of the currently focused "input" (a
468 * div[contenteditable].oe_searchview_input)
470 currentInputValue: function () {
471 return this.$element.find('div.oe_searchview_input:focus').text();
474 * Provide auto-completion result for req.term (an array to `resp`)
476 * @param {Object} req request to complete
477 * @param {String} req.term searched term to complete
478 * @param {Function} resp response callback
480 complete_global_search: function (req, resp) {
481 $.when.apply(null, _(this.inputs).chain()
482 .invoke('complete', req.term)
483 .value()).then(function () {
484 resp(_(_(arguments).compact()).flatten(true));
489 * Action to perform in case of selection: create a facet (model)
490 * and add it to the search collection
492 * @param {Object} e selection event, preventDefault to avoid setting value on object
493 * @param {Object} ui selection information
494 * @param {Object} ui.item selected completion item
496 select_completion: function (e, ui) {
499 var input_index = _(this.input_subviews).indexOf(
501 this.$element.find('div.oe_searchview_input:focus')[0]));
502 this.query.add(ui.item.facet, {at: input_index / 2});
504 childFocused: function () {
505 this.$element.addClass('oe_focused');
507 childBlurred: function () {
508 this.$element.removeClass('oe_focused');
512 * @param {openerp.web.search.SearchQuery | openerp.web.search.Facet} _1
513 * @param {openerp.web.search.Facet} [_2]
514 * @param {Object} [options]
516 renderFacets: function (_1, _2, options) {
517 // _1: model if event=change, otherwise collection
518 // _2: undefined if event=change, otherwise model
521 var $e = this.$element.find('div.oe_searchview_facets');
522 _.invoke(this.input_subviews, 'destroy');
523 this.input_subviews = [];
525 var i = new my.InputView(this);
526 started.push(i.appendTo($e));
527 this.input_subviews.push(i);
528 this.query.each(function (facet) {
529 var f = new my.FacetView(this, facet);
530 started.push(f.appendTo($e));
531 self.input_subviews.push(f);
533 var i = new my.InputView(this);
534 started.push(i.appendTo($e));
535 self.input_subviews.push(i);
537 _.each(this.input_subviews, function (childView) {
538 childView.on('focused', self, self.proxy('childFocused'));
539 childView.on('blurred', self, self.proxy('childBlurred'));
542 $.when.apply(null, started).then(function () {
544 // options.at: facet inserted at given index, focus next input
545 // otherwise just focus last input
546 if (!options || typeof options.at !== 'number') {
547 input_to_focus = _.last(self.input_subviews);
549 input_to_focus = self.input_subviews[(options.at + 1) * 2];
552 input_to_focus.$element.focus();
557 * Builds a list of widget rows (each row is an array of widgets)
559 * @param {Array} items a list of nodes to convert to widgets
560 * @param {Object} fields a mapping of field names to (ORM) field attributes
561 * @param {String} [group_name] name of the group to put the new controls in
563 make_widgets: function (items, fields, group_name) {
564 group_name = group_name || null;
565 if (!(group_name in this.controls)) {
566 this.controls[group_name] = [];
568 var self = this, group = this.controls[group_name];
570 _.each(items, function (item) {
571 if (filters.length && item.tag !== 'filter') {
572 group.push(new instance.web.search.FilterGroup(filters, this));
577 case 'separator': case 'newline':
580 filters.push(new instance.web.search.Filter(item, this));
583 self.make_widgets(item.children, fields, item.attrs.string);
586 group.push(this.make_field(item, fields[item['attrs'].name]));
588 self.make_widgets(item.children, fields, group_name);
593 if (filters.length) {
594 group.push(new instance.web.search.FilterGroup(filters, this));
598 * Creates a field for the provided field descriptor item (which comes
599 * from fields_view_get)
601 * @param {Object} item fields_view_get node for the field
602 * @param {Object} field fields_get result for the field
603 * @returns instance.web.search.Field
605 make_field: function (item, field) {
606 var obj = instance.web.search.fields.get_any( [item.attrs.widget, field.type]);
608 return new (obj) (item, field, this);
610 console.group('Unknown field type ' + field.type);
611 console.error('View node', item);
612 console.info('View field', field);
613 console.info('In view', this);
618 on_loaded: function(data) {
620 this.fields_view = data.fields_view;
621 if (data.fields_view.type !== 'search' ||
622 data.fields_view.arch.tag !== 'search') {
623 throw new Error(_.str.sprintf(
624 "Got non-search view after asking for a search view: type %s, arch root %s",
625 data.fields_view.type, data.fields_view.arch.tag));
629 data.fields_view['arch'].children,
630 data.fields_view.fields);
632 // add Filters to this.inputs, need view.controls filled
633 (new instance.web.search.Filters(this));
634 // add custom filters to this.inputs
635 (new instance.web.search.CustomFilters(this));
636 // add Advanced to this.inputs
637 (new instance.web.search.Advanced(this));
640 var drawer_started = $.when.apply(
641 null, _(this.select_for_drawer()).invoke(
642 'appendTo', this.$element.find('.oe_searchview_drawer')));
645 var defaults_fetched = $.when.apply(null, _(this.inputs).invoke(
646 'facet_for_defaults', this.defaults)).then(function () {
647 self.query.reset(_(arguments).compact(), {preventSearch: true});
650 return $.when(drawer_started, defaults_fetched)
651 .then(function () { self.ready.resolve(); })
654 * Handle event when the user make a selection in the filters management select box.
656 on_filters_management: function(e) {
658 var select = this.$element.find(".oe_search-view-filters-management");
659 var val = select.val();
661 case 'advanced_filter':
662 this.extended_search.on_activate();
664 case 'add_to_dashboard':
665 this.on_add_to_dashboard();
670 if (val.slice(0, 4) == "get:") {
672 val = parseInt(val, 10);
673 var filter = this.managed_filters[val];
674 this.do_clear(false).then(_.bind(function() {
675 select.val('get:' + val);
678 var group_by = filter.context.group_by;
681 group_by instanceof Array ? group_by : group_by.split(','),
682 function (el) { return { group_by: el }; });
685 domains: [filter.domain],
686 contexts: [filter.context],
696 * Extract search data from the view's facets.
698 * Result is an object with 4 (own) properties:
701 * An array of any error generated during data validation and
702 * extraction, contains the validation error objects
708 * Array of domains, in groupby order rather than view order
712 build_search_data: function () {
713 var domains = [], contexts = [], groupbys = [], errors = [];
715 this.query.each(function (facet) {
716 var field = facet.get('field');
718 var domain = field.get_domain(facet);
720 domains.push(domain);
722 var context = field.get_context(facet);
724 contexts.push(context);
726 var group_by = field.get_groupby(facet);
728 groupbys.push.apply(groupbys, group_by);
731 if (e instanceof instance.web.search.Invalid) {
745 * Performs the search view collection of widget data.
747 * If the collection went well (all fields are valid), then triggers
748 * :js:func:`instance.web.SearchView.on_search`.
750 * If at least one field failed its validation, triggers
751 * :js:func:`instance.web.SearchView.on_invalid` instead.
753 * @param e jQuery event object coming from the "Search" button
755 do_search: function (_query, options) {
756 if (options && options.preventSearch) {
759 var search = this.build_search_data();
760 if (!_.isEmpty(search.errors)) {
761 this.on_invalid(search.errors);
764 return this.on_search(search.domains, search.contexts, search.groupbys);
767 * Triggered after the SearchView has collected all relevant domains and
770 * It is provided with an Array of domains and an Array of contexts, which
771 * may or may not be evaluated (each item can be either a valid domain or
772 * context, or a string to evaluate in order in the sequence)
774 * It is also passed an array of contexts used for group_by (they are in
775 * the correct order for group_by evaluation, which contexts may not be)
778 * @param {Array} domains an array of literal domains or domain references
779 * @param {Array} contexts an array of literal contexts or context refs
780 * @param {Array} groupbys ordered contexts which may or may not have group_by keys
782 on_search: function (domains, contexts, groupbys) {
785 * Triggered after a validation error in the SearchView fields.
787 * Error objects have three keys:
788 * * ``field`` is the name of the invalid field
789 * * ``value`` is the invalid value
790 * * ``message`` is the (in)validation message provided by the field
793 * @param {Array} errors a never-empty array of error objects
795 on_invalid: function (errors) {
796 this.do_notify(_t("Invalid Search"), _t("triggered from search view"));
801 * Registry of search fields, called by :js:class:`instance.web.SearchView` to
802 * find and instantiate its field widgets.
804 instance.web.search.fields = new instance.web.Registry({
805 'char': 'instance.web.search.CharField',
806 'text': 'instance.web.search.CharField',
807 'boolean': 'instance.web.search.BooleanField',
808 'integer': 'instance.web.search.IntegerField',
809 'id': 'instance.web.search.IntegerField',
810 'float': 'instance.web.search.FloatField',
811 'selection': 'instance.web.search.SelectionField',
812 'datetime': 'instance.web.search.DateTimeField',
813 'date': 'instance.web.search.DateField',
814 'many2one': 'instance.web.search.ManyToOneField',
815 'many2many': 'instance.web.search.CharField',
816 'one2many': 'instance.web.search.CharField'
818 instance.web.search.Invalid = instance.web.Class.extend( /** @lends instance.web.search.Invalid# */{
820 * Exception thrown by search widgets when they hold invalid values,
821 * which they can not return when asked.
823 * @constructs instance.web.search.Invalid
824 * @extends instance.web.Class
826 * @param field the name of the field holding an invalid value
827 * @param value the invalid value
828 * @param message validation failure message
830 init: function (field, value, message) {
833 this.message = message;
835 toString: function () {
836 return _.str.sprintf(
837 _t("Incorrect value for field %(fieldname)s: [%(value)s] is %(message)s"),
838 {fieldname: this.field, value: this.value, message: this.message}
842 instance.web.search.Widget = instance.web.OldWidget.extend( /** @lends instance.web.search.Widget# */{
845 * Root class of all search widgets
847 * @constructs instance.web.search.Widget
848 * @extends instance.web.OldWidget
850 * @param view the ancestor view of this widget
852 init: function (view) {
857 instance.web.search.add_expand_listener = function($root) {
858 $root.find('a.searchview_group_string').click(function (e) {
859 $root.toggleClass('folded expanded');
864 instance.web.search.Group = instance.web.search.Widget.extend({
865 template: 'SearchView.group',
866 init: function (view_section, view, fields) {
868 this.attrs = view_section.attrs;
869 this.lines = view.make_widgets(
870 view_section.children, fields);
874 instance.web.search.Input = instance.web.search.Widget.extend( /** @lends instance.web.search.Input# */{
877 * @constructs instance.web.search.Input
878 * @extends instance.web.search.Widget
882 init: function (view) {
884 this.view.inputs.push(this);
885 this.style = undefined;
888 * Fetch auto-completion values for the widget.
890 * The completion values should be an array of objects with keys category,
891 * label, value prefixed with an object with keys type=section and label
893 * @param {String} value value to complete
894 * @returns {jQuery.Deferred<null|Array>}
896 complete: function (value) {
900 * Returns a Facet instance for the provided defaults if they apply to
901 * this widget, or null if they don't.
903 * This default implementation will try calling
904 * :js:func:`instance.web.search.Input#facet_for` if the widget's name
905 * matches the input key
907 * @param {Object} defaults
908 * @returns {jQuery.Deferred<null|Object>}
910 facet_for_defaults: function (defaults) {
912 !(this.attrs.name in defaults && defaults[this.attrs.name])) {
915 return this.facet_for(defaults[this.attrs.name]);
917 in_drawer: function () {
918 return !!this._in_drawer;
920 get_context: function () {
922 "get_context not implemented for widget " + this.attrs.type);
924 get_groupby: function () {
926 "get_groupby not implemented for widget " + this.attrs.type);
928 get_domain: function () {
930 "get_domain not implemented for widget " + this.attrs.type);
932 load_attrs: function (attrs) {
933 if (attrs.modifiers) {
934 attrs.modifiers = JSON.parse(attrs.modifiers);
935 attrs.invisible = attrs.modifiers.invisible || false;
936 if (attrs.invisible) {
937 this.style = 'display: none;'
943 instance.web.search.FilterGroup = instance.web.search.Input.extend(/** @lends instance.web.search.FilterGroup# */{
944 template: 'SearchView.filters',
946 completion_label: _lt("Filter on: %s"),
948 * Inclusive group of filters, creates a continuous "button" with clickable
949 * sections (the normal display for filters is to be a self-contained button)
951 * @constructs instance.web.search.FilterGroup
952 * @extends instance.web.search.Input
954 * @param {Array<instance.web.search.Filter>} filters elements of the group
955 * @param {instance.web.SearchView} view view in which the filters are contained
957 init: function (filters, view) {
958 // If all filters are group_by and we're not initializing a GroupbyGroup,
959 // create a GroupbyGroup instead of the current FilterGroup
960 if (!(this instanceof instance.web.search.GroupbyGroup) &&
961 _(filters).all(function (f) {
962 return f.attrs.context && f.attrs.context.group_by; })) {
963 return new instance.web.search.GroupbyGroup(filters, view);
966 this.filters = filters;
967 this.view.query.on('add remove change reset', this.proxy('search_change'));
970 this.$element.on('click', 'li', this.proxy('toggle_filter'));
974 * Handles change of the search query: any of the group's filter which is
975 * in the search query should be visually checked in the drawer
977 search_change: function () {
979 var $filters = this.$element.find('> li').removeClass('oe_selected');
980 var facet = this.view.query.find(_.bind(this.match_facet, this));
981 if (!facet) { return; }
982 facet.values.each(function (v) {
983 var i = _(self.filters).indexOf(v.get('value'));
984 if (i === -1) { return; }
985 $filters.eq(i).addClass('oe_selected');
989 * Matches the group to a facet, in order to find if the group is
990 * represented in the current search query
992 match_facet: function (facet) {
993 return facet.get('field') === this;
995 make_facet: function (values) {
997 category: _t("Filter"),
1003 make_value: function (filter) {
1005 label: filter.attrs.string || filter.attrs.help || filter.attrs.name,
1009 facet_for_defaults: function (defaults) {
1011 var fs = _(this.filters).chain()
1012 .filter(function (f) {
1013 return f.attrs && f.attrs.name && !!defaults[f.attrs.name];
1014 }).map(function (f) {
1015 return self.make_value(f);
1017 if (_.isEmpty(fs)) { return $.when(null); }
1018 return $.when(this.make_facet(fs));
1021 * Fetches contexts for all enabled filters in the group
1023 * @param {openerp.web.search.Facet} facet
1024 * @return {*} combined contexts of the enabled filters in this group
1026 get_context: function (facet) {
1027 var contexts = facet.values.chain()
1028 .map(function (f) { return f.get('value').attrs.context; })
1032 if (!contexts.length) { return; }
1033 if (contexts.length === 1) { return contexts[0]; }
1034 return _.extend(new instance.web.CompoundContext, {
1035 __contexts: contexts
1039 * Fetches group_by sequence for all enabled filters in the group
1041 * @param {VS.model.SearchFacet} facet
1042 * @return {Array} enabled filters in this group
1044 get_groupby: function (facet) {
1045 return facet.values.chain()
1046 .map(function (f) { return f.get('value').attrs.context; })
1051 * Handles domains-fetching for all the filters within it: groups them.
1053 * @param {VS.model.SearchFacet} facet
1054 * @return {*} combined domains of the enabled filters in this group
1056 get_domain: function (facet) {
1057 var domains = facet.values.chain()
1058 .map(function (f) { return f.get('value').attrs.domain; })
1062 if (!domains.length) { return; }
1063 if (domains.length === 1) { return domains[0]; }
1064 for (var i=domains.length; --i;) {
1065 domains.unshift(['|']);
1067 return _.extend(new instance.web.CompoundDomain(), {
1071 toggle_filter: function (e) {
1072 this.toggle(this.filters[$(e.target).index()]);
1074 toggle: function (filter) {
1075 this.view.query.toggle(this.make_facet([this.make_value(filter)]));
1077 complete: function (item) {
1079 item = item.toLowerCase();
1080 var facet_values = _(this.filters).chain()
1081 .filter(function (filter) {
1083 string: filter.attrs.string || '',
1084 help: filter.attrs.help || '',
1085 name: filter.attrs.name || ''
1087 var include = _.str.include;
1088 return include(at.string.toLowerCase(), item)
1089 || include(at.help.toLowerCase(), item)
1090 || include(at.name.toLowerCase(), item);
1092 .map(this.make_value)
1094 if (_(facet_values).isEmpty()) { return $.when(null); }
1095 return $.when(_.map(facet_values, function (facet_value) {
1097 label: _.str.sprintf(self.completion_label.toString(),
1099 facet: self.make_facet([facet_value])
1104 instance.web.search.GroupbyGroup = instance.web.search.FilterGroup.extend({
1106 completion_label: _lt("Group by: %s"),
1107 init: function (filters, view) {
1108 this._super(filters, view);
1109 // Not flanders: facet unicity is handled through the
1110 // (category, field) pair of facet attributes. This is all well and
1111 // good for regular filter groups where a group matches a facet, but for
1112 // groupby we want a single facet. So cheat: add an attribute on the
1113 // view which proxies to the first GroupbyGroup, so it can be used
1114 // for every GroupbyGroup and still provides the various methods needed
1115 // by the search view. Use weirdo name to avoid risks of conflicts
1116 if (!this.getParent()._s_groupby) {
1117 this.getParent()._s_groupby = {
1118 help: "See GroupbyGroup#init",
1119 get_context: this.proxy('get_context'),
1120 get_domain: this.proxy('get_domain'),
1121 get_groupby: this.proxy('get_groupby')
1125 match_facet: function (facet) {
1126 return facet.get('field') === this.getParent()._s_groupby;
1128 make_facet: function (values) {
1130 category: _t("GroupBy"),
1133 field: this.getParent()._s_groupby
1137 instance.web.search.Filter = instance.web.search.Input.extend(/** @lends instance.web.search.Filter# */{
1138 template: 'SearchView.filter',
1140 * Implementation of the OpenERP filters (button with a context and/or
1141 * a domain sent as-is to the search view)
1143 * Filters are only attributes holder, the actual work (compositing
1144 * domains and contexts, converting between facets and filters) is
1145 * performed by the filter group.
1147 * @constructs instance.web.search.Filter
1148 * @extends instance.web.search.Input
1153 init: function (node, view) {
1155 this.load_attrs(node.attrs);
1157 facet_for: function () { return $.when(null); },
1158 get_context: function () { },
1159 get_domain: function () { },
1161 instance.web.search.Field = instance.web.search.Input.extend( /** @lends instance.web.search.Field# */ {
1162 template: 'SearchView.field',
1163 default_operator: '=',
1165 * @constructs instance.web.search.Field
1166 * @extends instance.web.search.Input
1168 * @param view_section
1172 init: function (view_section, field, view) {
1174 this.load_attrs(_.extend({}, field, view_section.attrs));
1176 facet_for: function (value) {
1179 category: this.attrs.string || this.attrs.name,
1180 values: [{label: String(value), value: value}]
1183 value_from: function (facetValue) {
1184 return facetValue.get('value');
1186 get_context: function (facet) {
1188 // A field needs a context to send when active
1189 var context = this.attrs.context;
1190 if (!context || !facet.values.length) {
1193 var contexts = facet.values.map(function (facetValue) {
1194 return new instance.web.CompoundContext(context)
1195 .set_eval_context({self: self.value_from(facetValue)});
1198 if (contexts.length === 1) { return contexts[0]; }
1200 return _.extend(new instance.web.CompoundContext, {
1201 __contexts: contexts
1204 get_groupby: function () { },
1206 * Function creating the returned domain for the field, override this
1207 * methods in children if you only need to customize the field's domain
1208 * without more complex alterations or tests (and without the need to
1209 * change override the handling of filter_domain)
1211 * @param {String} name the field's name
1212 * @param {String} operator the field's operator (either attribute-specified or default operator for the field
1213 * @param {Number|String} value parsed value for the field
1214 * @returns {Array<Array>} domain to include in the resulting search
1216 make_domain: function (name, operator, facet) {
1217 return [[name, operator, this.value_from(facet)]];
1219 get_domain: function (facet) {
1220 if (!facet.values.length) { return; }
1222 var value_to_domain;
1224 var domain = this.attrs['filter_domain'];
1226 value_to_domain = function (facetValue) {
1227 return new instance.web.CompoundDomain(domain)
1228 .set_eval_context({self: self.value_from(facetValue)});
1231 value_to_domain = function (facetValue) {
1232 return self.make_domain(
1234 self.attrs.operator || self.default_operator,
1238 var domains = facet.values.map(value_to_domain);
1240 if (domains.length === 1) { return domains[0]; }
1241 for (var i = domains.length; --i;) {
1242 domains.unshift(['|']);
1245 return _.extend(new instance.web.CompoundDomain, {
1251 * Implementation of the ``char`` OpenERP field type:
1253 * * Default operator is ``ilike`` rather than ``=``
1255 * * The Javascript and the HTML values are identical (strings)
1258 * @extends instance.web.search.Field
1260 instance.web.search.CharField = instance.web.search.Field.extend( /** @lends instance.web.search.CharField# */ {
1261 default_operator: 'ilike',
1262 complete: function (value) {
1263 if (_.isEmpty(value)) { return $.when(null); }
1264 var label = _.str.sprintf(_.str.escapeHTML(
1265 _t("Search %(field)s for: %(value)s")), {
1266 field: '<em>' + this.attrs.string + '</em>',
1267 value: '<strong>' + _.str.escapeHTML(value) + '</strong>'});
1271 category: this.attrs.string,
1273 values: [{label: value, value: value}]
1278 instance.web.search.NumberField = instance.web.search.Field.extend(/** @lends instance.web.search.NumberField# */{
1279 value_from: function () {
1280 if (!this.$element.val()) {
1283 var val = this.parse(this.$element.val()),
1284 check = Number(this.$element.val());
1285 if (isNaN(val) || val !== check) {
1286 this.$element.addClass('error');
1287 throw new instance.web.search.Invalid(
1288 this.attrs.name, this.$element.val(), this.error_message);
1290 this.$element.removeClass('error');
1296 * @extends instance.web.search.NumberField
1298 instance.web.search.IntegerField = instance.web.search.NumberField.extend(/** @lends instance.web.search.IntegerField# */{
1299 error_message: _t("not a valid integer"),
1300 parse: function (value) {
1302 return instance.web.parse_value(value, {'widget': 'integer'});
1310 * @extends instance.web.search.NumberField
1312 instance.web.search.FloatField = instance.web.search.NumberField.extend(/** @lends instance.web.search.FloatField# */{
1313 error_message: _t("not a valid number"),
1314 parse: function (value) {
1316 return instance.web.parse_value(value, {'widget': 'float'});
1324 * Utility function for m2o & selection fields taking a selection/name_get pair
1325 * (value, name) and converting it to a Facet descriptor
1327 * @param {instance.web.search.Field} field holder field
1328 * @param {Array} pair pair value to convert
1330 function facet_from(field, pair) {
1333 category: field['attrs'].string,
1334 values: [{label: pair[1], value: pair[0]}]
1340 * @extends instance.web.search.Field
1342 instance.web.search.SelectionField = instance.web.search.Field.extend(/** @lends instance.web.search.SelectionField# */{
1343 // This implementation is a basic <select> field, but it may have to be
1344 // altered to be more in line with the GTK client, which uses a combo box
1345 // (~ jquery.autocomplete):
1346 // * If an option was selected in the list, behave as currently
1347 // * If something which is not in the list was entered (via the text input),
1348 // the default domain should become (`ilike` string_value) but **any
1349 // ``context`` or ``filter_domain`` becomes falsy, idem if ``@operator``
1350 // is specified. So at least get_domain needs to be quite a bit
1351 // overridden (if there's no @value and there is no filter_domain and
1352 // there is no @operator, return [[name, 'ilike', str_val]]
1353 template: 'SearchView.field.selection',
1355 this._super.apply(this, arguments);
1356 // prepend empty option if there is no empty option in the selection list
1357 this.prepend_empty = !_(this.attrs.selection).detect(function (item) {
1361 complete: function (needle) {
1363 var results = _(this.attrs.selection).chain()
1364 .filter(function (sel) {
1365 var value = sel[0], label = sel[1];
1366 if (!value) { return false; }
1367 return label.toLowerCase().indexOf(needle.toLowerCase()) !== -1;
1369 .map(function (sel) {
1372 facet: facet_from(self, sel)
1375 if (_.isEmpty(results)) { return $.when(null); }
1376 return $.when.call(null, [{
1377 label: this.attrs.string
1378 }].concat(results));
1380 facet_for: function (value) {
1381 var match = _(this.attrs.selection).detect(function (sel) {
1382 return sel[0] === value;
1384 if (!match) { return $.when(null); }
1385 return $.when(facet_from(this, match));
1388 instance.web.search.BooleanField = instance.web.search.SelectionField.extend(/** @lends instance.web.search.BooleanField# */{
1390 * @constructs instance.web.search.BooleanField
1391 * @extends instance.web.search.BooleanField
1394 this._super.apply(this, arguments);
1395 this.attrs.selection = [
1403 * @extends instance.web.search.DateField
1405 instance.web.search.DateField = instance.web.search.Field.extend(/** @lends instance.web.search.DateField# */{
1406 value_from: function (facetValue) {
1407 return instance.web.date_to_str(facetValue.get('value'));
1409 complete: function (needle) {
1410 var d = Date.parse(needle);
1411 if (!d) { return $.when(null); }
1412 var date_string = instance.web.format_value(d, this.attrs);
1413 var label = _.str.sprintf(_.str.escapeHTML(
1414 _t("Search %(field)s at: %(value)s")), {
1415 field: '<em>' + this.attrs.string + '</em>',
1416 value: '<strong>' + date_string + '</strong>'});
1420 category: this.attrs.string,
1422 values: [{label: date_string, value: d}]
1428 * Implementation of the ``datetime`` openerp field type:
1430 * * Uses the same widget as the ``date`` field type (a simple date)
1432 * * Builds a slighly more complex, it's a datetime range (includes time)
1433 * spanning the whole day selected by the date widget
1436 * @extends instance.web.DateField
1438 instance.web.search.DateTimeField = instance.web.search.DateField.extend(/** @lends instance.web.search.DateTimeField# */{
1439 value_from: function (facetValue) {
1440 return instance.web.datetime_to_str(facetValue.get('value'));
1443 instance.web.search.ManyToOneField = instance.web.search.CharField.extend({
1444 default_operator: {},
1445 init: function (view_section, field, view) {
1446 this._super(view_section, field, view);
1447 this.model = new instance.web.Model(this.attrs.relation);
1449 complete: function (needle) {
1452 // FIXME: "concurrent" searches (multiple requests, mis-ordered responses)
1453 return this.model.call('name_search', [], {
1457 }).pipe(function (results) {
1458 if (_.isEmpty(results)) { return null; }
1459 return [{label: self.attrs.string}].concat(
1460 _(results).map(function (result) {
1463 facet: facet_from(self, result)
1468 facet_for: function (value) {
1470 if (value instanceof Array) {
1471 return $.when(facet_from(this, value));
1473 return this.model.call('name_get', [value], {}).pipe(function (names) {
1474 if (_(names).isEmpty()) { return null; }
1475 return facet_from(self, names[0]);
1478 value_from: function (facetValue) {
1479 return facetValue.get('label');
1481 make_domain: function (name, operator, facetValue) {
1482 if (operator === this.default_operator) {
1483 return [[name, '=', facetValue.get('value')]];
1485 return this._super(name, operator, facetValue);
1489 instance.web.search.CustomFilters = instance.web.search.Input.extend({
1490 template: 'SearchView.CustomFilters',
1492 start: function () {
1494 this.model = new instance.web.Model('ir.filters');
1497 .on('remove', function (facet) {
1498 if (!facet.get('is_custom_filter')) {
1501 self.clear_selection();
1503 .on('reset', this.proxy('clear_selection'));
1504 this.$element.on('submit', 'form', this.proxy('save_current'));
1505 this.$element.on('click', 'h4', function () {
1506 self.$element.toggleClass('oe_opened');
1508 // FIXME: local eval of domain and context to get rid of special endpoint
1509 return this.rpc('/web/searchview/get_filters', {
1510 model: this.view.model
1511 }).pipe(this.proxy('set_filters'));
1513 clear_selection: function () {
1514 this.$element.find('li.oe_selected').removeClass('oe_selected');
1516 append_filter: function (filter) {
1518 var key = _.str.sprintf('(%s)%s', filter.user_id, filter.name);
1521 if (key in this.filters) {
1522 $filter = this.filters[key];
1525 $filter = this.filters[key] = $('<li></li>')
1526 .appendTo(this.$element.find('.oe_searchview_custom_list'))
1527 .addClass(filter.user_id ? 'oe_searchview_custom_private'
1528 : 'oe_searchview_custom_public')
1531 $('<a class="oe_searchview_custom_delete">x</a>')
1532 .click(function (e) {
1533 e.stopPropagation();
1534 self.model.call('unlink', [id]).then(function () {
1541 $filter.unbind('click').click(function () {
1542 self.view.query.reset([{
1543 category: _("Custom Filter"),
1546 get_context: function () { return filter.context; },
1547 get_groupby: function () { return [filter.context]; },
1548 get_domain: function () { return filter.domain; }
1550 is_custom_filter: true,
1551 values: [{label: filter.name, value: null}]
1553 $filter.addClass('oe_selected');
1556 set_filters: function (filters) {
1557 _(filters).map(_.bind(this.append_filter, this));
1559 save_current: function () {
1561 var $name = this.$element.find('input:first');
1562 var private_filter = !this.$element.find('input:last').prop('checked');
1564 var search = this.view.build_search_data();
1565 this.rpc('/web/session/eval_domain_and_context', {
1566 domains: search.domains,
1567 contexts: search.contexts,
1568 group_by_seq: search.groupbys || []
1569 }).then(function (results) {
1570 if (!_.isEmpty(results.group_by)) {
1571 results.context.group_by = results.group_by;
1575 user_id: private_filter ? instance.connection.uid : false,
1576 model_id: self.view.model,
1577 context: results.context,
1578 domain: results.domain
1580 // FIXME: current context?
1581 return self.model.call('create_or_replace', [filter]).then(function (id) {
1583 self.append_filter(filter);
1585 .removeClass('oe_opened')
1586 .find('form')[0].reset();
1593 instance.web.search.Filters = instance.web.search.Input.extend({
1594 template: 'SearchView.Filters',
1596 start: function () {
1598 var running_count = 0;
1599 // get total filters count
1600 var is_group = function (i) { return i instanceof instance.web.search.FilterGroup; };
1601 var filters_count = _(this.view.controls).chain()
1604 .map(function (i) { return i.filters.length; })
1608 var col1 = [], col2 = _(this.view.controls).map(function (inputs, group) {
1609 var filters = _(inputs).filter(is_group);
1611 name: group === 'null' ? "<span class='oe_i'>q</span> " + _t("Filters") : "<span class='oe_i'>w</span> " + group,
1613 length: _(filters).chain().map(function (i) {
1614 return i.filters.length; }).sum().value()
1618 while (col2.length) {
1619 // col1 + group should be smaller than col2 + group
1620 if ((running_count + col2[0].length) <= (filters_count - running_count)) {
1621 running_count += col2[0].length;
1622 col1.push(col2.shift());
1629 this.render_column(col1, $('<div>').appendTo(this.$element)),
1630 this.render_column(col2, $('<div>').appendTo(this.$element)));
1632 render_column: function (column, $el) {
1633 return $.when.apply(null, _(column).map(function (group) {
1634 $('<h3>').html(group.name).appendTo($el);
1635 return $.when.apply(null,
1636 _(group.filters).invoke('appendTo', $el));
1640 instance.web.search.AddToDashboard = instance.web.search.Input.extend({
1641 template: 'SearchView.addtodashboard',
1643 start: function () {
1645 this.data_loaded = $.Deferred();
1646 this.dashboard_data =[];
1648 .on('click', 'h4', this.proxy('show_option'))
1649 .on('submit', 'form', function (e) {e.preventDefault(); self.add_dashboard();});
1650 return $.when(this.load_data(),this.data_loaded).pipe(this.proxy("render_data"));
1652 load_data:function(){
1653 // get from database if dashboard position change than also works(from Reporting to else).
1655 ir_actions_act_window = new instance.web.Model('ir.actions.act_window',{},[['res_model','=',"board.board"],['view_id','!=',false]])
1656 .query(['name','id']),
1657 map_data = function(){
1658 var ir_actions_values = arguments[0],ir_values = arguments[1];
1659 _(ir_values).each(function(res){
1660 var get_name = _.detect(ir_actions_values,function(name){ return name.id == parseInt((res.value).split(",")[1]);});
1661 self.dashboard_data.push({"res_id":res.res_id,"name":get_name.name})
1663 self.data_loaded.resolve();
1665 make_domain = function(result){
1667 _(result).map(function(value,key){
1668 domain.push(["value","=","ir.actions.act_window,"+value.id]);
1669 ((result.length)- 1 !== key)?domain.unshift("|"):false;
1673 return ir_actions_act_window._execute().then(function(ir_actions_values){
1674 if(!ir_actions_values.length) {self.data_loaded.resolve();return;}
1675 var ir_value = new instance.web.Model('ir.values',{},make_domain(ir_actions_values)).query(['res_id','value']);
1676 ir_value._execute().done(function(ir_values){
1677 map_data(ir_actions_values,ir_values);
1681 //===============================get from instance.webclient.menu (with less rpc call)
1682 var self = this,dashbaord_menu = instance.webclient.menu.data.data.children,
1683 ir_model_data = new instance.web.Model('ir.model.data',{},[['name','=','menu_reporting_dashboard']]).query(['res_id']),
1684 map_data = function(result){
1685 _.detect(dashbaord_menu,function(dash){
1686 var id = _.pluck(dash.children, "id"),indexof = _.indexOf(id, result.res_id);
1688 self.dashboard_data = dash.children[indexof].children
1689 self.data_loaded.resolve();
1694 return ir_model_data._execute().done(function(result){map_data(result[0])});
1697 render_data: function(){
1699 var selection = instance.web.qweb.render("SearchView.addtodashboard.selection",{selections:this.dashboard_data});
1700 this.$element.find("input").before(selection)
1702 add_dashboard:function(){
1703 var self = this,getParent = this.getParent(),view_parent = this.view.getParent();
1704 if(!view_parent.action || !this.$element.find("select").val())
1705 return this.do_warn("Can't find dashboard action");
1706 data = getParent.build_search_data(),
1707 context = new instance.web.CompoundContext(getParent.dataset.get_context() || []),
1708 domain = new instance.web.CompoundDomain(getParent.dataset.get_domain() || []);
1709 _.each(data.contexts, function(x) {context.add(x);});
1710 _.each(data.domains, function(x) {domain.add(x);});
1711 this.rpc('/web/searchview/add_to_dashboard', {
1712 menu_id: this.$element.find("select").val(),
1713 action_id: view_parent.action.id,
1714 context_to_save: context,
1716 view_mode: view_parent.active_view,
1717 name: this.$element.find("input").val()
1720 self.do_warn("Could not add filter to dashboard");
1722 self.$element.toggleClass('oe_opened');
1723 self.do_notify("Filter added to dashboard", '');
1727 show_option:function(){
1728 this.$element.toggleClass('oe_opened');
1729 if (! this.$element.hasClass('oe_opened'))
1731 this.$element.find("input").val(this.getParent().fields_view.name || "" );
1734 instance.web.search.Advanced = instance.web.search.Input.extend({
1735 template: 'SearchView.advanced',
1737 start: function () {
1740 .on('keypress keydown keyup', function (e) { e.stopPropagation(); })
1741 .on('click', 'h4', function () {
1742 self.$element.toggleClass('oe_opened');
1743 }).on('click', 'button.oe_add_condition', function () {
1744 self.append_proposition();
1745 }).on('submit', 'form', function (e) {
1747 self.commit_search();
1751 this.rpc("/web/searchview/fields_get", {model: this.view.model}, function(data) {
1752 self.fields = _.extend({
1753 id: { string: 'ID', type: 'id' }
1755 })).then(function () {
1756 self.append_proposition();
1759 append_proposition: function () {
1760 return (new instance.web.search.ExtendedSearchProposition(this, this.fields))
1761 .appendTo(this.$element.find('ul'));
1763 commit_search: function () {
1765 // Get domain sections from all propositions
1766 var children = this.getChildren();
1767 var propositions = _.invoke(children, 'get_proposition');
1768 var domain = _(propositions).pluck('value');
1769 for (var i = domain.length; --i;) {
1770 domain.unshift('|');
1773 this.view.query.add({
1774 category: _t("Advanced"),
1775 values: propositions,
1777 get_context: function () { },
1778 get_domain: function () { return domain;},
1779 get_groupby: function () { }
1783 // remove all propositions
1784 _.invoke(children, 'destroy');
1785 // add new empty proposition
1786 this.append_proposition();
1787 // TODO: API on searchview
1788 this.view.$element.removeClass('oe_searchview_open_drawer');
1792 instance.web.search.ExtendedSearchProposition = instance.web.OldWidget.extend(/** @lends instance.web.search.ExtendedSearchProposition# */{
1793 template: 'SearchView.extended_search.proposition',
1795 * @constructs instance.web.search.ExtendedSearchProposition
1796 * @extends instance.web.OldWidget
1801 init: function (parent, fields) {
1802 this._super(parent);
1803 this.fields = _(fields).chain()
1804 .map(function(val, key) { return _.extend({}, val, {'name': key}); })
1805 .sortBy(function(field) {return field.string;})
1807 this.attrs = {_: _, fields: this.fields, selected: null};
1810 start: function () {
1812 this.$element.find(".searchview_extended_prop_field").change(function() {
1815 this.$element.find('.searchview_extended_delete_prop').click(function () {
1820 changed: function() {
1821 var nval = this.$element.find(".searchview_extended_prop_field").val();
1822 if(this.attrs.selected == null || nval != this.attrs.selected.name) {
1823 this.select_field(_.detect(this.fields, function(x) {return x.name == nval;}));
1827 * Selects the provided field object
1829 * @param field a field descriptor object (as returned by fields_get, augmented by the field name)
1831 select_field: function(field) {
1833 if(this.attrs.selected != null) {
1834 this.value.destroy();
1836 this.$element.find('.searchview_extended_prop_op').html('');
1838 this.attrs.selected = field;
1843 var type = field.type;
1844 var Field = instance.web.search.custom_filters.get_object(type);
1846 Field = instance.web.search.custom_filters.get_object("char");
1848 this.value = new Field(this, field);
1849 _.each(this.value.operators, function(operator) {
1850 $('<option>', {value: operator.value})
1851 .text(String(operator.text))
1852 .appendTo(self.$element.find('.searchview_extended_prop_op'));
1854 var $value_loc = this.$element.find('.searchview_extended_prop_value').empty();
1855 this.value.appendTo($value_loc);
1858 get_proposition: function() {
1859 if ( this.attrs.selected == null)
1861 var field = this.attrs.selected;
1862 var op = this.$element.find('.searchview_extended_prop_op')[0];
1863 var operator = op.options[op.selectedIndex];
1865 label: _.str.sprintf(_t('%(field)s %(operator)s "%(value)s"'), {
1866 field: field.string,
1867 // According to spec, HTMLOptionElement#label should return
1868 // HTMLOptionElement#text when not defined/empty, but it does
1869 // not in older Webkit (between Safari 5.1.5 and Chrome 17) and
1870 // Gecko (pre Firefox 7) browsers, so we need a manual fallback
1872 operator: operator.label || operator.text,
1873 value: this.value}),
1874 value: [field.name, operator.value, this.value.get_value()]
1879 instance.web.search.ExtendedSearchProposition.Field = instance.web.Widget.extend({
1880 init: function (parent, field) {
1881 this._super(parent);
1885 * Returns a human-readable version of the value, in case the "logical"
1886 * and the "semantic" values of a field differ (as for selection fields,
1889 * The default implementation simply returns the value itself.
1891 * @return {String} human-readable version of the value
1893 toString: function () {
1894 return this.get_value();
1897 instance.web.search.ExtendedSearchProposition.Char = instance.web.search.ExtendedSearchProposition.Field.extend({
1898 template: 'SearchView.extended_search.proposition.char',
1900 {value: "ilike", text: _lt("contains")},
1901 {value: "not ilike", text: _lt("doesn't contain")},
1902 {value: "=", text: _lt("is equal to")},
1903 {value: "!=", text: _lt("is not equal to")}
1905 get_value: function() {
1906 return this.$element.val();
1909 instance.web.search.ExtendedSearchProposition.DateTime = instance.web.search.ExtendedSearchProposition.Field.extend({
1910 template: 'SearchView.extended_search.proposition.empty',
1912 {value: "=", text: _lt("is equal to")},
1913 {value: "!=", text: _lt("is not equal to")},
1914 {value: ">", text: _lt("greater than")},
1915 {value: "<", text: _lt("less than")},
1916 {value: ">=", text: _lt("greater or equal than")},
1917 {value: "<=", text: _lt("less or equal than")}
1920 * Date widgets live in view_form which is not yet loaded when this is
1923 widget: function () { return instance.web.DateTimeWidget; },
1924 get_value: function() {
1925 return this.datewidget.get_value();
1928 var ready = this._super();
1929 this.datewidget = new (this.widget())(this);
1930 this.datewidget.appendTo(this.$element);
1934 instance.web.search.ExtendedSearchProposition.Date = instance.web.search.ExtendedSearchProposition.DateTime.extend({
1935 widget: function () { return instance.web.DateWidget; }
1937 instance.web.search.ExtendedSearchProposition.Integer = instance.web.search.ExtendedSearchProposition.Field.extend({
1938 template: 'SearchView.extended_search.proposition.integer',
1940 {value: "=", text: _lt("is equal to")},
1941 {value: "!=", text: _lt("is not equal to")},
1942 {value: ">", text: _lt("greater than")},
1943 {value: "<", text: _lt("less than")},
1944 {value: ">=", text: _lt("greater or equal than")},
1945 {value: "<=", text: _lt("less or equal than")}
1947 toString: function () {
1948 return this.$element.val();
1950 get_value: function() {
1952 return instance.web.parse_value(this.$element.val(), {'widget': 'integer'});
1958 instance.web.search.ExtendedSearchProposition.Id = instance.web.search.ExtendedSearchProposition.Integer.extend({
1959 operators: [{value: "=", text: _lt("is")}]
1961 instance.web.search.ExtendedSearchProposition.Float = instance.web.search.ExtendedSearchProposition.Field.extend({
1962 template: 'SearchView.extended_search.proposition.float',
1964 {value: "=", text: _lt("is equal to")},
1965 {value: "!=", text: _lt("is not equal to")},
1966 {value: ">", text: _lt("greater than")},
1967 {value: "<", text: _lt("less than")},
1968 {value: ">=", text: _lt("greater or equal than")},
1969 {value: "<=", text: _lt("less or equal than")}
1971 toString: function () {
1972 return this.$element.val();
1974 get_value: function() {
1976 return instance.web.parse_value(this.$element.val(), {'widget': 'float'});
1982 instance.web.search.ExtendedSearchProposition.Selection = instance.web.search.ExtendedSearchProposition.Field.extend({
1983 template: 'SearchView.extended_search.proposition.selection',
1985 {value: "=", text: _lt("is")},
1986 {value: "!=", text: _lt("is not")}
1988 toString: function () {
1989 var select = this.$element[0];
1990 var option = select.options[select.selectedIndex];
1991 return option.label || option.text;
1993 get_value: function() {
1994 return this.$element.val();
1997 instance.web.search.ExtendedSearchProposition.Boolean = instance.web.search.ExtendedSearchProposition.Field.extend({
1998 template: 'SearchView.extended_search.proposition.empty',
2000 {value: "=", text: _lt("is true")},
2001 {value: "!=", text: _lt("is false")}
2003 toString: function () { return ''; },
2004 get_value: function() {
2009 instance.web.search.custom_filters = new instance.web.Registry({
2010 'char': 'instance.web.search.ExtendedSearchProposition.Char',
2011 'text': 'instance.web.search.ExtendedSearchProposition.Char',
2012 'one2many': 'instance.web.search.ExtendedSearchProposition.Char',
2013 'many2one': 'instance.web.search.ExtendedSearchProposition.Char',
2014 'many2many': 'instance.web.search.ExtendedSearchProposition.Char',
2016 'datetime': 'instance.web.search.ExtendedSearchProposition.DateTime',
2017 'date': 'instance.web.search.ExtendedSearchProposition.Date',
2018 'integer': 'instance.web.search.ExtendedSearchProposition.Integer',
2019 'float': 'instance.web.search.ExtendedSearchProposition.Float',
2020 'boolean': 'instance.web.search.ExtendedSearchProposition.Boolean',
2021 'selection': 'instance.web.search.ExtendedSearchProposition.Selection',
2023 'id': 'instance.web.search.ExtendedSearchProposition.Id'
2028 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: