1 openerp.web.search = function(openerp) {
2 var QWeb = openerp.web.qweb,
6 sum: function (obj) { return _.reduce(obj, function (a, b) { return a + b; }, 0); }
9 // Have SearchBox optionally use callback function to produce inputs and facets
10 // (views) set on callbacks.make_facet and callbacks.make_input keys when
11 // initializing VisualSearch
12 var SearchBox_renderFacet = function (facet, position) {
13 var view = new (this.app.options.callbacks['make_facet'] || VS.ui.SearchFacet)({
19 // Input first, facet second.
20 this.renderSearchInput();
21 this.facetViews.push(view);
22 this.$('.VS-search-inner').children().eq(position*2).after(view.render().el);
25 _.defer(_.bind(view.calculateSize, view));
28 }; // warning: will not match
29 // Ensure we're replacing the function we think
30 if (SearchBox_renderFacet.toString() !== VS.ui.SearchBox.prototype.renderFacet.toString().replace(/(VS\.ui\.SearchFacet)/, "(this.app.options.callbacks['make_facet'] || $1)")) {
32 "Trying to replace wrong version of VS.ui.SearchBox#renderFacet. "
33 + "Please fix replacement.");
35 var SearchBox_renderSearchInput = function () {
36 var input = new (this.app.options.callbacks['make_input'] || VS.ui.SearchInput)({position: this.inputViews.length, app: this.app});
37 this.$('.VS-search-inner').append(input.render().el);
38 this.inputViews.push(input);
40 // Ensure we're replacing the function we think
41 if (SearchBox_renderSearchInput.toString() !== VS.ui.SearchBox.prototype.renderSearchInput.toString().replace(/(VS\.ui\.SearchInput)/, "(this.app.options.callbacks['make_input'] || $1)")) {
43 "Trying to replace wrong version of VS.ui.SearchBox#renderSearchInput. "
44 + "Please fix replacement.");
46 var SearchBox_searchEvent = function (e) {
50 this.app.options.callbacks.search(query, this.app.searchQuery);
52 if (SearchBox_searchEvent.toString() !== VS.ui.SearchBox.prototype.searchEvent.toString().replace(
53 /this\.value\(\);\n[ ]{4}this\.focusSearch\(e\);\n[ ]{4}this\.value\(query\)/,
54 'null;\n this.renderFacets();\n this.focusSearch(e)')) {
56 "Trying to replace wrong version of VS.ui.SearchBox#searchEvent. "
57 + "Please fix replacement.");
59 _.extend(VS.ui.SearchBox.prototype, {
60 renderFacet: SearchBox_renderFacet,
61 renderSearchInput: SearchBox_renderSearchInput,
62 searchEvent: SearchBox_searchEvent
64 _.extend(VS.model.SearchFacet.prototype, {
66 if (this.has('json')) {
67 return this.get('json');
69 return this.get('value');
73 openerp.web.SearchView = openerp.web.Widget.extend(/** @lends openerp.web.SearchView# */{
74 template: "SearchView",
76 * @constructs openerp.web.SearchView
77 * @extends openerp.web.OldWidget
85 init: function(parent, dataset, view_id, defaults, hidden) {
87 this.dataset = dataset;
88 this.model = dataset.model;
89 this.view_id = view_id;
91 this.defaults = defaults || {};
92 this.has_defaults = !_.isEmpty(this.defaults);
97 this.hidden = !!hidden;
98 this.headless = this.hidden && !this.has_defaults;
100 this.filter_data = {};
102 this.ready = $.Deferred();
106 var p = this._super();
108 this.setup_global_completion();
110 container: this.$element,
113 make_facet: this.proxy('make_visualsearch_facet'),
114 make_input: this.proxy('make_visualsearch_input'),
115 search: function (query, searchCollection) {
118 facetMatches: function (callback) {
120 valueMatches : function(facet, searchTerm, callback) {
125 var search = function () { self.vs.searchBox.searchEvent({}); };
126 // searchQuery operations
128 .off('add').on('add', search)
129 .off('change').on('change', search)
130 .off('reset').on('reset', search)
131 .off('remove').on('remove', function (record, collection, options) {
132 if (options['trigger_search']) {
138 this.$element.hide();
141 this.ready.resolve();
143 var load_view = this.rpc("/web/searchview/load", {
145 view_id: this.view_id,
146 context: this.dataset.get_context() });
147 // FIXME: local eval of domain and context to get rid of special endpoint
148 var filters = this.rpc('/web/searchview/get_filters', {
150 }).then(function (filters) { self.custom_filters = filters; });
152 $.when(load_view, filters)
153 .pipe(function (load) { return load[0]; })
154 .then(this.on_loaded);
157 this.$element.on('click', '.oe_vs_unfold_drawer', function () {
158 self.$element.toggleClass('oe_searchview_open_drawer');
161 return $.when(p, this.ready);
164 this.$element.show();
167 this.$element.hide();
171 * Sets up thingie where all the mess is put?
173 setup_stuff_drawer: function () {
175 $('<div class="oe_vs_unfold_drawer">').appendTo(this.$element.find('.VS-search-box'));
176 var $drawer = $('<div class="oe_searchview_drawer">').appendTo(this.$element);
177 var $filters = $('<div class="oe_searchview_filters">').appendTo($drawer);
179 var running_count = 0;
180 // get total filters count
181 var is_group = function (i) { return i instanceof openerp.web.search.FilterGroup; };
182 var filters_count = _(this.controls).chain()
185 .map(function (i) { return i.filters.length; })
189 var col1 = [], col2 = _(this.controls).map(function (inputs, group) {
190 var filters = _(inputs).filter(is_group);
192 name: group === 'null' ? _t("Filters") : group,
194 length: _(filters).chain().map(function (i) {
195 return i.filters.length; }).sum().value()
199 while (col2.length) {
200 // col1 + group should be smaller than col2 + group
201 if ((running_count + col2[0].length) <= (filters_count - running_count)) {
202 running_count += col2[0].length;
203 col1.push(col2.shift());
209 // Create a Custom Filter FilterGroup for each custom filter read from
210 // the db, add all of this as a group in the smallest column
211 [].push.call(col1.length <= col2.length ? col1 : col2, {
212 name: _t("Custom Filters"),
213 filters: _.map(this.custom_filters, function (filter) {
214 // FIXME: handling of ``disabled`` being set
215 var f = new openerp.web.search.Filter({attrs: {
217 context: filter.context,
218 domain: filter.domain
220 return new openerp.web.search.FilterGroup([f], self);
226 this.render_column(col1, $('<div>').appendTo($filters)),
227 this.render_column(col2, $('<div>').appendTo($filters)),
228 (new openerp.web.search.Advanced(this).appendTo($drawer)));
230 render_column: function (column, $el) {
231 return $.when.apply(null, _(column).map(function (group) {
232 $('<h3>').text(group.name).appendTo($el);
233 return $.when.apply(null,
234 _(group.filters).invoke('appendTo', $el));
238 * Sets up search view's view-wide auto-completion widget
240 setup_global_completion: function () {
241 // Prevent keydown from within a facet's input from reaching the
242 // auto-completion widget and opening the completion list
243 this.$element.on('keydown', '.search_facet input', function (e) {
244 e.stopImmediatePropagation();
247 this.$element.autocomplete({
248 source: this.proxy('complete_global_search'),
249 select: this.proxy('select_completion'),
250 focus: function (e) { e.preventDefault(); },
254 }).data('autocomplete')._renderItem = function (ul, item) {
255 // item of completion list
256 var $item = $( "<li></li>" )
257 .data( "item.autocomplete", item )
260 if (item.value !== undefined) {
261 // regular completion item
264 ? $('<a>').html(item.label)
265 : $('<a>').text(item.value));
267 return $item.text(item.category)
269 borderTop: '1px solid #cccccc',
280 * Provide auto-completion result for req.term (an array to `resp`)
282 * @param {Object} req request to complete
283 * @param {String} req.term searched term to complete
284 * @param {Function} resp response callback
286 complete_global_search: function (req, resp) {
287 $.when.apply(null, _(this.inputs).chain()
288 .invoke('complete', req.term)
289 .value()).then(function () {
290 resp(_(_(arguments).compact()).flatten(true));
295 * Action to perform in case of selection: create a facet (model)
296 * and add it to the search collection
298 * @param {Object} e selection event, preventDefault to avoid setting value on object
299 * @param {Object} ui selection information
300 * @param {Object} ui.item selected completion item
302 select_completion: function (e, ui) {
304 this.vs.searchQuery.add(new VS.model.SearchFacet(_.extend(
305 {app: this.vs}, ui.item)));
306 this.vs.searchBox.searchEvent({});
310 * Builds the right SearchFacet view based on the facet object to render
311 * (e.g. readonly facets for filters)
313 * @param {Object} options
314 * @param {VS.model.SearchFacet} options.model facet object to render
316 make_visualsearch_facet: function (options) {
317 return new openerp.web.search.FilterGroupFacet(options);
319 // if (options.model.get('field') instanceof openerp.web.search.FilterGroup) {
320 // return new openerp.web.search.FilterGroupFacet(options);
322 // return new VS.ui.SearchFacet(options);
325 * Proxies searches on a SearchInput to the search view's global completion
327 * Also disables SearchInput.autocomplete#_move so search view's
328 * autocomplete can get the corresponding events, or something.
332 make_visualsearch_input: function (options) {
333 var self = this, input = new VS.ui.SearchInput(options);
334 input.setupAutocomplete = function () {
335 _.extend(this.box.autocomplete({
338 search: function () {
339 self.$element.autocomplete('search', input.box.val());
342 }).data('autocomplete'), {
343 _move: function () {},
344 close: function () { self.$element.autocomplete('close'); }
351 * Builds a list of widget rows (each row is an array of widgets)
353 * @param {Array} items a list of nodes to convert to widgets
354 * @param {Object} fields a mapping of field names to (ORM) field attributes
355 * @param {String} [group_name] name of the group to put the new controls in
357 make_widgets: function (items, fields, group_name) {
358 group_name = group_name || null;
359 if (!(group_name in this.controls)) {
360 this.controls[group_name] = [];
362 var self = this, group = this.controls[group_name];
364 _.each(items, function (item) {
365 if (filters.length && item.tag !== 'filter') {
366 group.push(new openerp.web.search.FilterGroup(filters, this));
371 case 'separator': case 'newline':
374 filters.push(new openerp.web.search.Filter(item, this));
377 self.make_widgets(item.children, fields, item.attrs.string);
380 group.push(this.make_field(item, fields[item['attrs'].name]));
382 self.make_widgets(item.children, fields, group_name);
387 if (filters.length) {
388 group.push(new openerp.web.search.FilterGroup(filters, this));
392 * Creates a field for the provided field descriptor item (which comes
393 * from fields_view_get)
395 * @param {Object} item fields_view_get node for the field
396 * @param {Object} field fields_get result for the field
397 * @returns openerp.web.search.Field
399 make_field: function (item, field) {
400 var obj = openerp.web.search.fields.get_any( [item.attrs.widget, field.type]);
402 return new (obj) (item, field, this);
404 console.group('Unknown field type ' + field.type);
405 console.error('View node', item);
406 console.info('View field', field);
407 console.info('In view', this);
412 on_loaded: function(data) {
414 this.fields_view = data.fields_view;
415 if (data.fields_view.type !== 'search' ||
416 data.fields_view.arch.tag !== 'search') {
417 throw new Error(_.str.sprintf(
418 "Got non-search view after asking for a search view: type %s, arch root %s",
419 data.fields_view.type, data.fields_view.arch.tag));
423 data.fields_view['arch'].children,
424 data.fields_view.fields);
428 this.setup_stuff_drawer(),
429 $.when.apply(null, _(this.inputs).invoke('facet_for_defaults', this.defaults))
431 self.vs.searchQuery.reset(_(arguments).compact(), {silent: true});
432 self.vs.searchBox.renderFacets();
434 .then(function () { self.ready.resolve(); })
437 * Handle event when the user make a selection in the filters management select box.
439 on_filters_management: function(e) {
441 var select = this.$element.find(".oe_search-view-filters-management");
442 var val = select.val();
444 case 'advanced_filter':
445 this.extended_search.on_activate();
447 case 'add_to_dashboard':
448 this.on_add_to_dashboard();
450 case 'manage_filters':
452 res_model: 'ir.filters',
453 views: [[false, 'list'], [false, 'form']],
454 type: 'ir.actions.act_window',
455 context: {"search_default_user_id": this.session.uid,
456 "search_default_model_id": this.dataset.model},
462 var data = this.build_search_data();
463 var context = new openerp.web.CompoundContext();
464 _.each(data.contexts, function(x) {
467 var domain = new openerp.web.CompoundDomain();
468 _.each(data.domains, function(x) {
471 var groupbys = _.pluck(data.groupbys, "group_by").join();
472 context.add({"group_by": groupbys});
473 var dial_html = QWeb.render("SearchView.managed-filters.add");
474 var $dial = $(dial_html);
475 openerp.web.dialog($dial, {
477 title: _t("Filter Entry"),
479 {text: _t("Cancel"), click: function() {
480 $(this).dialog("close");
482 {text: _t("OK"), click: function() {
483 $(this).dialog("close");
484 var name = $(this).find("input").val();
485 self.rpc('/web/searchview/save_filter', {
486 model: self.dataset.model,
487 context_to_save: context,
491 self.reload_managed_filters();
500 if (val.slice(0, 4) == "get:") {
502 val = parseInt(val, 10);
503 var filter = this.managed_filters[val];
504 this.do_clear(false).then(_.bind(function() {
505 select.val('get:' + val);
508 var group_by = filter.context.group_by;
511 group_by instanceof Array ? group_by : group_by.split(','),
512 function (el) { return { group_by: el }; });
515 domains: [filter.domain],
516 contexts: [filter.context],
525 on_add_to_dashboard: function() {
526 this.$element.find(".oe_search-view-filters-management")[0].selectedIndex = 0;
528 menu = openerp.webclient.menu,
529 $dialog = $(QWeb.render("SearchView.add_to_dashboard", {
530 dashboards : menu.data.data.children,
531 selected_menu_id : menu.$element.find('a.active').data('menu')
533 $dialog.find('input').val(this.fields_view.name);
534 openerp.web.dialog($dialog, {
536 title: _t("Add to Dashboard"),
538 {text: _t("Cancel"), click: function() {
539 $(this).dialog("close");
541 {text: _t("OK"), click: function() {
542 $(this).dialog("close");
543 var menu_id = $(this).find("select").val(),
544 title = $(this).find("input").val(),
545 data = self.build_search_data(),
546 context = new openerp.web.CompoundContext(),
547 domain = new openerp.web.CompoundDomain();
548 _.each(data.contexts, function(x) {
551 _.each(data.domains, function(x) {
554 self.rpc('/web/searchview/add_to_dashboard', {
556 action_id: self.getParent().action.id,
557 context_to_save: context,
559 view_mode: self.getParent().active_view,
563 self.do_warn("Could not add filter to dashboard");
565 self.do_notify("Filter added to dashboard", '');
573 * Performs the search view collection of widget data.
575 * If the collection went well (all fields are valid), then triggers
576 * :js:func:`openerp.web.SearchView.on_search`.
578 * If at least one field failed its validation, triggers
579 * :js:func:`openerp.web.SearchView.on_invalid` instead.
581 * @param e jQuery event object coming from the "Search" button
583 do_search: function () {
584 var domains = [], contexts = [], groupbys = [], errors = [];
586 this.vs.searchQuery.each(function (facet) {
587 var field = facet.get('field');
589 var domain = field.get_domain(facet);
591 domains.push(domain);
593 var context = field.get_context(facet);
595 contexts.push(context);
597 var group_by = field.get_groupby(facet);
599 groupbys.push.apply(groupbys, group_by);
602 if (e instanceof openerp.web.search.Invalid) {
610 if (!_.isEmpty(errors)) {
611 this.on_invalid(errors);
614 return this.on_search(domains, contexts, groupbys);
617 * Triggered after the SearchView has collected all relevant domains and
620 * It is provided with an Array of domains and an Array of contexts, which
621 * may or may not be evaluated (each item can be either a valid domain or
622 * context, or a string to evaluate in order in the sequence)
624 * It is also passed an array of contexts used for group_by (they are in
625 * the correct order for group_by evaluation, which contexts may not be)
628 * @param {Array} domains an array of literal domains or domain references
629 * @param {Array} contexts an array of literal contexts or context refs
630 * @param {Array} groupbys ordered contexts which may or may not have group_by keys
632 on_search: function (domains, contexts, groupbys) {
635 * Triggered after a validation error in the SearchView fields.
637 * Error objects have three keys:
638 * * ``field`` is the name of the invalid field
639 * * ``value`` is the invalid value
640 * * ``message`` is the (in)validation message provided by the field
643 * @param {Array} errors a never-empty array of error objects
645 on_invalid: function (errors) {
646 this.do_notify(_t("Invalid Search"), _t("triggered from search view"));
651 openerp.web.search = {};
653 openerp.web.search.FilterGroupFacet = VS.ui.SearchFacet.extend({
655 'click': 'selectFacet'
656 }, VS.ui.SearchFacet.prototype.events),
658 render: function () {
659 this.setMode('not', 'editing');
660 this.setMode('not', 'selected');
662 var value = this.model.get('value');
663 this.$el.html(QWeb.render('SearchView.filters.facet', {
666 // virtual input so SearchFacet code has something to play with
667 this.box = $('<input>').val(value);
671 enableEdit: function () {
674 keydown: function (e) {
675 var key = VS.app.hotkeys.key(e);
676 if (key !== 'right') {
677 return VS.ui.SearchFacet.prototype.keydown.call(this, e);
680 this.deselectFacet();
681 this.options.app.searchBox.focusNextFacet(this, 1);
685 * Registry of search fields, called by :js:class:`openerp.web.SearchView` to
686 * find and instantiate its field widgets.
688 openerp.web.search.fields = new openerp.web.Registry({
689 'char': 'openerp.web.search.CharField',
690 'text': 'openerp.web.search.CharField',
691 'boolean': 'openerp.web.search.BooleanField',
692 'integer': 'openerp.web.search.IntegerField',
693 'id': 'openerp.web.search.IntegerField',
694 'float': 'openerp.web.search.FloatField',
695 'selection': 'openerp.web.search.SelectionField',
696 'datetime': 'openerp.web.search.DateTimeField',
697 'date': 'openerp.web.search.DateField',
698 'many2one': 'openerp.web.search.ManyToOneField',
699 'many2many': 'openerp.web.search.CharField',
700 'one2many': 'openerp.web.search.CharField'
702 openerp.web.search.Invalid = openerp.web.Class.extend( /** @lends openerp.web.search.Invalid# */{
704 * Exception thrown by search widgets when they hold invalid values,
705 * which they can not return when asked.
707 * @constructs openerp.web.search.Invalid
708 * @extends openerp.web.Class
710 * @param field the name of the field holding an invalid value
711 * @param value the invalid value
712 * @param message validation failure message
714 init: function (field, value, message) {
717 this.message = message;
719 toString: function () {
720 return _.str.sprintf(
721 _t("Incorrect value for field %(fieldname)s: [%(value)s] is %(message)s"),
722 {fieldname: this.field, value: this.value, message: this.message}
726 openerp.web.search.Widget = openerp.web.OldWidget.extend( /** @lends openerp.web.search.Widget# */{
729 * Root class of all search widgets
731 * @constructs openerp.web.search.Widget
732 * @extends openerp.web.OldWidget
734 * @param view the ancestor view of this widget
736 init: function (view) {
741 openerp.web.search.add_expand_listener = function($root) {
742 $root.find('a.searchview_group_string').click(function (e) {
743 $root.toggleClass('folded expanded');
748 openerp.web.search.Group = openerp.web.search.Widget.extend({
749 template: 'SearchView.group',
750 init: function (view_section, view, fields) {
752 this.attrs = view_section.attrs;
753 this.lines = view.make_widgets(
754 view_section.children, fields);
758 openerp.web.search.Input = openerp.web.search.Widget.extend( /** @lends openerp.web.search.Input# */{
760 * @constructs openerp.web.search.Input
761 * @extends openerp.web.search.Widget
765 init: function (view) {
767 this.view.inputs.push(this);
768 this.style = undefined;
771 * Fetch auto-completion values for the widget.
773 * The completion values should be an array of objects with keys category,
774 * label, value prefixed with an object with keys type=section and label
776 * @param {String} value value to complete
777 * @returns {jQuery.Deferred<null|Array>}
779 complete: function (value) {
783 * Returns a VS.model.SearchFacet instance for the provided defaults if
784 * they apply to this widget, or null if they don't.
786 * This default implementation will try calling
787 * :js:func:`openerp.web.search.Input#facet_for` if the widget's name
788 * matches the input key
790 * @param {Object} defaults
791 * @returns {jQuery.Deferred<null|Object>}
793 facet_for_defaults: function (defaults) {
795 !(this.attrs.name in defaults && defaults[this.attrs.name])) {
798 return this.facet_for(defaults[this.attrs.name]);
800 get_context: function () {
802 "get_context not implemented for widget " + this.attrs.type);
804 get_groupby: function () {
806 "get_groupby not implemented for widget " + this.attrs.type);
808 get_domain: function () {
810 "get_domain not implemented for widget " + this.attrs.type);
812 load_attrs: function (attrs) {
813 if (attrs.modifiers) {
814 attrs.modifiers = JSON.parse(attrs.modifiers);
815 attrs.invisible = attrs.modifiers.invisible || false;
816 if (attrs.invisible) {
817 this.style = 'display: none;'
823 openerp.web.search.FilterGroup = openerp.web.search.Input.extend(/** @lends openerp.web.search.FilterGroup# */{
824 template: 'SearchView.filters',
826 * Inclusive group of filters, creates a continuous "button" with clickable
827 * sections (the normal display for filters is to be a self-contained button)
829 * @constructs openerp.web.search.FilterGroup
830 * @extends openerp.web.search.Input
832 * @param {Array<openerp.web.search.Filter>} filters elements of the group
833 * @param {openerp.web.SearchView} view view in which the filters are contained
835 init: function (filters, view) {
837 this.filters = filters;
840 this.$element.on('click', 'li', this.proxy('toggle_filter'));
843 facet_for_defaults: function (defaults) {
844 var fs = _(this.filters).filter(function (f) {
845 return f.attrs && f.attrs.name && !!defaults[f.attrs.name];
847 if (_.isEmpty(fs)) { return $.when(null); }
848 return $.when(new VS.model.SearchFacet({
849 category: _t("Filter"),
850 value: _(fs).map(function (f) {
851 return f.attrs.string || f.attrs.name }).join(' | '),
858 * Fetches contexts for all enabled filters in the group
860 * @param {VS.model.SearchFacet} facet
861 * @return {*} combined contexts of the enabled filters in this group
863 get_context: function (facet) {
864 var contexts = _(facet.get('json')).chain()
865 .map(function (filter) { return filter.attrs.context; })
869 if (!contexts.length) { return; }
870 if (contexts.length === 1) { return contexts[0]; }
871 return _.extend(new openerp.web.CompoundContext, {
876 * Fetches group_by sequence for all enabled filters in the group
878 * @param {VS.model.SearchFacet} facet
879 * @return {Array} enabled filters in this group
881 get_groupby: function (facet) {
882 return _(facet.get('json')).chain()
883 .map(function (filter) { return filter.attrs.context; })
888 * Handles domains-fetching for all the filters within it: groups them.
890 * @param {VS.model.SearchFacet} facet
891 * @return {*} combined domains of the enabled filters in this group
893 get_domain: function (facet) {
894 var domains = _(facet.get('json')).chain()
895 .map(function (filter) { return filter.attrs.domain; })
899 if (!domains.length) { return; }
900 if (domains.length === 1) { return domains[0]; }
901 for (var i=domains.length; --i;) {
902 domains.unshift(['|']);
904 return _.extend(new openerp.web.CompoundDomain(), {
908 toggle_filter: function (e) {
909 this.toggle(this.filters[$(e.target).index()]);
911 toggle: function (filter) {
912 // FIXME: oh god, my eyes, they hurt
914 var facet = this.view.vs.searchQuery.detect(function (f) {
915 return f.get('field') === self; });
917 fs = facet.get('json');
919 if (_.include(fs, filter)) {
920 fs = _.without(fs, filter);
924 if (_(fs).isEmpty()) {
925 this.view.vs.searchQuery.remove(facet, {trigger_search: true});
929 value: _(fs).map(function (f) {
930 return f.attrs.string || f.attrs.name }).join(' | ')
938 this.view.vs.searchQuery.add({
939 category: _t("Filter"),
940 value: _(fs).map(function (f) {
941 return f.attrs.string || f.attrs.name }).join(' | '),
948 openerp.web.search.Filter = openerp.web.search.Input.extend(/** @lends openerp.web.search.Filter# */{
949 template: 'SearchView.filter',
951 * Implementation of the OpenERP filters (button with a context and/or
952 * a domain sent as-is to the search view)
954 * Filters are only attributes holder, the actual work (compositing
955 * domains and contexts, converting between facets and filters) is
956 * performed by the filter group.
958 * @constructs openerp.web.search.Filter
959 * @extends openerp.web.search.Input
964 init: function (node, view) {
966 this.load_attrs(node.attrs);
968 facet_for: function () { return $.when(null); },
969 get_context: function () { },
970 get_domain: function () { },
972 openerp.web.search.Field = openerp.web.search.Input.extend( /** @lends openerp.web.search.Field# */ {
973 template: 'SearchView.field',
974 default_operator: '=',
976 * @constructs openerp.web.search.Field
977 * @extends openerp.web.search.Input
979 * @param view_section
983 init: function (view_section, field, view) {
985 this.load_attrs(_.extend({}, field, view_section.attrs));
987 facet_for: function (value) {
988 return $.when(new VS.model.SearchFacet({
989 category: this.attrs.string || this.attrs.name,
990 value: String(value),
996 get_value: function (facet) {
997 return facet.value();
999 get_context: function (facet) {
1000 var val = this.get_value(facet);
1001 // A field needs a value to be "active", and a context to send when
1003 var has_value = (val !== null && val !== '');
1004 var context = this.attrs.context;
1005 if (!(has_value && context)) {
1008 return new openerp.web.CompoundContext(context)
1009 .set_eval_context({self: val});
1011 get_groupby: function () { },
1013 * Function creating the returned domain for the field, override this
1014 * methods in children if you only need to customize the field's domain
1015 * without more complex alterations or tests (and without the need to
1016 * change override the handling of filter_domain)
1018 * @param {String} name the field's name
1019 * @param {String} operator the field's operator (either attribute-specified or default operator for the field
1020 * @param {Number|String} value parsed value for the field
1021 * @returns {Array<Array>} domain to include in the resulting search
1023 make_domain: function (name, operator, facet) {
1024 return [[name, operator, this.get_value(facet)]];
1026 get_domain: function (facet) {
1027 var val = this.get_value(facet);
1028 if (val === null || val === '') {
1032 var domain = this.attrs['filter_domain'];
1034 return this.make_domain(
1036 this.attrs.operator || this.default_operator,
1039 return new openerp.web.CompoundDomain(domain)
1040 .set_eval_context({self: val});
1044 * Implementation of the ``char`` OpenERP field type:
1046 * * Default operator is ``ilike`` rather than ``=``
1048 * * The Javascript and the HTML values are identical (strings)
1051 * @extends openerp.web.search.Field
1053 openerp.web.search.CharField = openerp.web.search.Field.extend( /** @lends openerp.web.search.CharField# */ {
1054 default_operator: 'ilike',
1055 complete: function (value) {
1056 if (_.isEmpty(value)) { return $.when(null); }
1057 var label = _.str.sprintf(_.str.escapeHTML(
1058 _t("Search %(field)s for: %(value)s")), {
1059 field: '<em>' + this.attrs.string + '</em>',
1060 value: '<strong>' + _.str.escapeHTML(value) + '</strong>'});
1062 category: this.attrs.string,
1069 openerp.web.search.NumberField = openerp.web.search.Field.extend(/** @lends openerp.web.search.NumberField# */{
1070 get_value: function () {
1071 if (!this.$element.val()) {
1074 var val = this.parse(this.$element.val()),
1075 check = Number(this.$element.val());
1076 if (isNaN(val) || val !== check) {
1077 this.$element.addClass('error');
1078 throw new openerp.web.search.Invalid(
1079 this.attrs.name, this.$element.val(), this.error_message);
1081 this.$element.removeClass('error');
1087 * @extends openerp.web.search.NumberField
1089 openerp.web.search.IntegerField = openerp.web.search.NumberField.extend(/** @lends openerp.web.search.IntegerField# */{
1090 error_message: _t("not a valid integer"),
1091 parse: function (value) {
1093 return openerp.web.parse_value(value, {'widget': 'integer'});
1101 * @extends openerp.web.search.NumberField
1103 openerp.web.search.FloatField = openerp.web.search.NumberField.extend(/** @lends openerp.web.search.FloatField# */{
1104 error_message: _t("not a valid number"),
1105 parse: function (value) {
1107 return openerp.web.parse_value(value, {'widget': 'float'});
1115 * @extends openerp.web.search.Field
1117 openerp.web.search.SelectionField = openerp.web.search.Field.extend(/** @lends openerp.web.search.SelectionField# */{
1118 // This implementation is a basic <select> field, but it may have to be
1119 // altered to be more in line with the GTK client, which uses a combo box
1120 // (~ jquery.autocomplete):
1121 // * If an option was selected in the list, behave as currently
1122 // * If something which is not in the list was entered (via the text input),
1123 // the default domain should become (`ilike` string_value) but **any
1124 // ``context`` or ``filter_domain`` becomes falsy, idem if ``@operator``
1125 // is specified. So at least get_domain needs to be quite a bit
1126 // overridden (if there's no @value and there is no filter_domain and
1127 // there is no @operator, return [[name, 'ilike', str_val]]
1128 template: 'SearchView.field.selection',
1130 this._super.apply(this, arguments);
1131 // prepend empty option if there is no empty option in the selection list
1132 this.prepend_empty = !_(this.attrs.selection).detect(function (item) {
1136 complete: function (needle) {
1138 var results = _(this.attrs.selection).chain()
1139 .filter(function (sel) {
1140 var value = sel[0], label = sel[1];
1141 if (!value) { return false; }
1142 return label.toLowerCase().indexOf(needle.toLowerCase()) !== -1;
1144 .map(function (sel) {
1146 category: self.attrs.string,
1152 if (_.isEmpty(results)) { return $.when(null); }
1153 return $.when.apply(null, [{
1154 category: this.attrs.string
1155 }].concat(results));
1157 facet_for: function (value) {
1158 var match = _(this.attrs.selection).detect(function (sel) {
1159 return sel[0] === value;
1161 if (!match) { return $.when(null); }
1162 return $.when(new VS.model.SearchFacet({
1163 category: this.attrs.string,
1170 get_value: function (facet) {
1171 return facet.get('json');
1174 openerp.web.search.BooleanField = openerp.web.search.SelectionField.extend(/** @lends openerp.web.search.BooleanField# */{
1176 * @constructs openerp.web.search.BooleanField
1177 * @extends openerp.web.search.BooleanField
1180 this._super.apply(this, arguments);
1181 this.attrs.selection = [
1182 ['true', _t("Yes")],
1186 get_value: function (facet) {
1187 switch (this._super(facet)) {
1188 case 'false': return false;
1189 case 'true': return true;
1190 default: return null;
1196 * @extends openerp.web.search.DateField
1198 openerp.web.search.DateField = openerp.web.search.Field.extend(/** @lends openerp.web.search.DateField# */{
1199 get_value: function (facet) {
1200 return openerp.web.date_to_str(facet.get('json'));
1202 complete: function (needle) {
1203 var d = Date.parse(needle);
1204 if (!d) { return $.when(null); }
1205 var value = openerp.web.format_value(d, this.attrs);
1206 var label = _.str.sprintf(_.str.escapeHTML(
1207 _t("Search %(field)s at: %(value)s")), {
1208 field: '<em>' + this.attrs.string + '</em>',
1209 value: '<strong>' + value + '</strong>'});
1211 category: this.attrs.string,
1220 * Implementation of the ``datetime`` openerp field type:
1222 * * Uses the same widget as the ``date`` field type (a simple date)
1224 * * Builds a slighly more complex, it's a datetime range (includes time)
1225 * spanning the whole day selected by the date widget
1228 * @extends openerp.web.DateField
1230 openerp.web.search.DateTimeField = openerp.web.search.DateField.extend(/** @lends openerp.web.search.DateTimeField# */{
1231 get_value: function (facet) {
1232 return openerp.web.datetime_to_str(facet.get('json'));
1235 openerp.web.search.ManyToOneField = openerp.web.search.CharField.extend({
1236 init: function (view_section, field, view) {
1237 this._super(view_section, field, view);
1238 this.model = new openerp.web.Model(this.attrs.relation);
1240 complete: function (needle) {
1243 // FIXME: "concurrent" searches (multiple requests, mis-ordered responses)
1244 return this.model.call('name_search', [], {
1248 }).pipe(function (results) {
1249 if (_.isEmpty(results)) { return null; }
1250 return [{category: self.attrs.string}].concat(
1251 _(results).map(function (result) {
1253 category: self.attrs.string,
1261 facet_for: function (value) {
1263 if (value instanceof Array) {
1264 return $.when(new VS.model.SearchFacet({
1265 category: this.attrs.string,
1272 return this.model.call('name_get', [value], {}).pipe(function (names) {
1273 return new VS.model.SearchFacet({
1274 category: self.attrs.string,
1282 make_domain: function (name, operator, facet) {
1283 // ``json`` -> actual auto-completed id
1284 if (facet.get('json')) {
1285 return [[name, '=', facet.get('json')]];
1288 return this._super(name, operator, facet);
1292 openerp.web.search.Advanced = openerp.web.search.Input.extend({
1293 template: 'SearchView.advanced',
1294 start: function () {
1297 .on('keypress keydown keyup', function (e) { e.stopPropagation(); })
1298 .on('click', 'h4', function () {
1299 self.$element.toggleClass('oe_opened');
1300 }).on('click', 'button.oe_add_condition', function () {
1301 self.append_proposition();
1302 }).on('submit', 'form', function (e) {
1304 self.commit_search();
1308 this.rpc("/web/searchview/fields_get", {model: this.view.model}, function(data) {
1309 self.fields = _.extend({
1310 id: { string: 'ID', type: 'id' }
1312 })).then(function () {
1313 self.append_proposition();
1316 append_proposition: function () {
1317 return (new openerp.web.search.ExtendedSearchProposition(this, this.fields))
1318 .appendTo(this.$element.find('ul'));
1320 commit_search: function () {
1322 // Get domain sections from all propositions
1323 var children = this.getChildren(),
1324 domain = _.invoke(children, 'get_proposition');
1325 var filters = _(domain).map(function (section) {
1326 return new openerp.web.search.Filter({attrs: {
1327 string: _.str.sprintf('%s(%s)%s',
1328 section[0], section[1], section[2]),
1332 // Create Filter (& FilterGroup around it) with that domain
1333 var f = new openerp.web.search.FilterGroup(filters, this.view);
1334 // add group to query
1335 this.view.vs.searchQuery.add({
1336 category: _t("Advanced"),
1337 value: _(filters).map(function (f) {
1338 return f.attrs.string || f.attrs.name }).join(' | '),
1343 // remove all propositions
1344 _.invoke(children, 'destroy');
1345 // add new empty proposition
1346 this.append_proposition();
1347 // TODO: API on searchview
1348 this.view.$element.removeClass('oe_searchview_open_drawer');
1352 openerp.web.search.ExtendedSearchProposition = openerp.web.OldWidget.extend(/** @lends openerp.web.search.ExtendedSearchProposition# */{
1353 template: 'SearchView.extended_search.proposition',
1355 * @constructs openerp.web.search.ExtendedSearchProposition
1356 * @extends openerp.web.OldWidget
1361 init: function (parent, fields) {
1362 this._super(parent);
1363 this.fields = _(fields).chain()
1364 .map(function(val, key) { return _.extend({}, val, {'name': key}); })
1365 .sortBy(function(field) {return field.string;})
1367 this.attrs = {_: _, fields: this.fields, selected: null};
1370 start: function () {
1372 this.$element.find(".searchview_extended_prop_field").change(function() {
1375 this.$element.find('.searchview_extended_delete_prop').click(function () {
1380 changed: function() {
1381 var nval = this.$element.find(".searchview_extended_prop_field").val();
1382 if(this.attrs.selected == null || nval != this.attrs.selected.name) {
1383 this.select_field(_.detect(this.fields, function(x) {return x.name == nval;}));
1387 * Selects the provided field object
1389 * @param field a field descriptor object (as returned by fields_get, augmented by the field name)
1391 select_field: function(field) {
1393 if(this.attrs.selected != null) {
1394 this.value.destroy();
1396 this.$element.find('.searchview_extended_prop_op').html('');
1398 this.attrs.selected = field;
1403 var type = field.type;
1404 var obj = openerp.web.search.custom_filters.get_object(type);
1406 console.log('Unknow field type ' + e.key);
1407 obj = openerp.web.search.custom_filters.get_object("char");
1409 this.value = new (obj) (this);
1410 if(this.value.set_field) {
1411 this.value.set_field(field);
1413 _.each(this.value.operators, function(operator) {
1414 $('<option>', {value: operator.value})
1415 .text(String(operator.text))
1416 .appendTo(self.$element.find('.searchview_extended_prop_op'));
1418 this.$element.find('.searchview_extended_prop_value').html(
1419 this.value.render({}));
1423 get_proposition: function() {
1424 if ( this.attrs.selected == null)
1426 var field = this.attrs.selected.name;
1427 var op = this.$element.find('.searchview_extended_prop_op').val();
1428 var value = this.value.get_value();
1429 return [field, op, value];
1433 openerp.web.search.ExtendedSearchProposition.Field = openerp.web.OldWidget.extend({
1434 start: function () {
1435 this.$element = $("#" + this.element_id);
1438 openerp.web.search.ExtendedSearchProposition.Char = openerp.web.search.ExtendedSearchProposition.Field.extend({
1439 template: 'SearchView.extended_search.proposition.char',
1441 {value: "ilike", text: _lt("contains")},
1442 {value: "not ilike", text: _lt("doesn't contain")},
1443 {value: "=", text: _lt("is equal to")},
1444 {value: "!=", text: _lt("is not equal to")}
1446 get_value: function() {
1447 return this.$element.val();
1450 openerp.web.search.ExtendedSearchProposition.DateTime = openerp.web.search.ExtendedSearchProposition.Field.extend({
1451 template: 'SearchView.extended_search.proposition.empty',
1453 {value: "=", text: _lt("is equal to")},
1454 {value: "!=", text: _lt("is not equal to")},
1455 {value: ">", text: _lt("greater than")},
1456 {value: "<", text: _lt("less than")},
1457 {value: ">=", text: _lt("greater or equal than")},
1458 {value: "<=", text: _lt("less or equal than")}
1460 get_value: function() {
1461 return this.datewidget.get_value();
1465 this.datewidget = new openerp.web.DateTimeWidget(this);
1466 this.datewidget.prependTo(this.$element);
1469 openerp.web.search.ExtendedSearchProposition.Date = openerp.web.search.ExtendedSearchProposition.Field.extend({
1470 template: 'SearchView.extended_search.proposition.empty',
1472 {value: "=", text: _lt("is equal to")},
1473 {value: "!=", text: _lt("is not equal to")},
1474 {value: ">", text: _lt("greater than")},
1475 {value: "<", text: _lt("less than")},
1476 {value: ">=", text: _lt("greater or equal than")},
1477 {value: "<=", text: _lt("less or equal than")}
1479 get_value: function() {
1480 return this.datewidget.get_value();
1484 this.datewidget = new openerp.web.DateWidget(this);
1485 this.datewidget.prependTo(this.$element);
1488 openerp.web.search.ExtendedSearchProposition.Integer = openerp.web.search.ExtendedSearchProposition.Field.extend({
1489 template: 'SearchView.extended_search.proposition.integer',
1491 {value: "=", text: _lt("is equal to")},
1492 {value: "!=", text: _lt("is not equal to")},
1493 {value: ">", text: _lt("greater than")},
1494 {value: "<", text: _lt("less than")},
1495 {value: ">=", text: _lt("greater or equal than")},
1496 {value: "<=", text: _lt("less or equal than")}
1498 get_value: function() {
1500 return openerp.web.parse_value(this.$element.val(), {'widget': 'integer'});
1506 openerp.web.search.ExtendedSearchProposition.Id = openerp.web.search.ExtendedSearchProposition.Integer.extend({
1507 operators: [{value: "=", text: _lt("is")}]
1509 openerp.web.search.ExtendedSearchProposition.Float = openerp.web.search.ExtendedSearchProposition.Field.extend({
1510 template: 'SearchView.extended_search.proposition.float',
1512 {value: "=", text: _lt("is equal to")},
1513 {value: "!=", text: _lt("is not equal to")},
1514 {value: ">", text: _lt("greater than")},
1515 {value: "<", text: _lt("less than")},
1516 {value: ">=", text: _lt("greater or equal than")},
1517 {value: "<=", text: _lt("less or equal than")}
1519 get_value: function() {
1521 return openerp.web.parse_value(this.$element.val(), {'widget': 'float'});
1527 openerp.web.search.ExtendedSearchProposition.Selection = openerp.web.search.ExtendedSearchProposition.Field.extend({
1528 template: 'SearchView.extended_search.proposition.selection',
1530 {value: "=", text: _lt("is")},
1531 {value: "!=", text: _lt("is not")}
1533 set_field: function(field) {
1536 get_value: function() {
1537 return this.$element.val();
1540 openerp.web.search.ExtendedSearchProposition.Boolean = openerp.web.search.ExtendedSearchProposition.Field.extend({
1541 template: 'SearchView.extended_search.proposition.boolean',
1543 {value: "=", text: _lt("is true")},
1544 {value: "!=", text: _lt("is false")}
1546 get_value: function() {
1551 openerp.web.search.custom_filters = new openerp.web.Registry({
1552 'char': 'openerp.web.search.ExtendedSearchProposition.Char',
1553 'text': 'openerp.web.search.ExtendedSearchProposition.Char',
1554 'one2many': 'openerp.web.search.ExtendedSearchProposition.Char',
1555 'many2one': 'openerp.web.search.ExtendedSearchProposition.Char',
1556 'many2many': 'openerp.web.search.ExtendedSearchProposition.Char',
1558 'datetime': 'openerp.web.search.ExtendedSearchProposition.DateTime',
1559 'date': 'openerp.web.search.ExtendedSearchProposition.Date',
1560 'integer': 'openerp.web.search.ExtendedSearchProposition.Integer',
1561 'float': 'openerp.web.search.ExtendedSearchProposition.Float',
1562 'boolean': 'openerp.web.search.ExtendedSearchProposition.Boolean',
1563 'selection': 'openerp.web.search.ExtendedSearchProposition.Selection',
1565 'id': 'openerp.web.search.ExtendedSearchProposition.Id'
1570 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: