1 openerp.web.search = function(openerp) {
2 var QWeb = openerp.web.qweb,
6 openerp.web.SearchView = openerp.web.Widget.extend(/** @lends openerp.web.SearchView# */{
7 template: "EmptyComponent",
9 * @constructs openerp.web.SearchView
10 * @extends openerp.web.Widget
18 init: function(parent, dataset, view_id, defaults, hidden) {
20 this.dataset = dataset;
21 this.model = dataset.model;
22 this.view_id = view_id;
24 this.defaults = defaults || {};
25 this.has_defaults = !_.isEmpty(this.defaults);
28 this.enabled_filters = [];
30 this.has_focus = false;
32 this.hidden = !!hidden;
33 this.headless = this.hidden && !this.has_defaults;
35 this.ready = $.Deferred();
45 this.rpc("/web/searchview/load", {
47 view_id: this.view_id,
48 context: this.dataset.get_context()
51 return this.ready.promise();
60 * Builds a list of widget rows (each row is an array of widgets)
62 * @param {Array} items a list of nodes to convert to widgets
63 * @param {Object} fields a mapping of field names to (ORM) field attributes
66 make_widgets: function (items, fields) {
71 _.each(items, function (item) {
72 if (filters.length && item.tag !== 'filter') {
74 new openerp.web.search.FilterGroup(
79 if (item.tag === 'newline') {
82 } else if (item.tag === 'filter') {
83 if (!this.has_focus) {
84 item.attrs.default_focus = '1';
85 this.has_focus = true;
88 new openerp.web.search.Filter(
90 } else if (item.tag === 'separator') {
91 // a separator is a no-op
93 if (item.tag === 'group') {
94 // TODO: group and field should be fetched from registries, maybe even filters
96 new openerp.web.search.Group(
98 } else if (item.tag === 'field') {
99 if (!this.has_focus) {
100 item.attrs.default_focus = '1';
101 this.has_focus = true;
105 item, fields[item['attrs'].name]));
109 if (filters.length) {
110 row.push(new openerp.web.search.FilterGroup(filters, this));
116 * Creates a field for the provided field descriptor item (which comes
117 * from fields_view_get)
119 * @param {Object} item fields_view_get node for the field
120 * @param {Object} field fields_get result for the field
121 * @returns openerp.web.search.Field
123 make_field: function (item, field) {
125 return new (openerp.web.search.fields.get_any(
126 [item.attrs.widget, field.type]))
129 if (! e instanceof openerp.web.KeyNotFound) {
132 // KeyNotFound means unknown field type
133 console.group('Unknown field type ' + field.type);
134 console.error('View node', item);
135 console.info('View field', field);
136 console.info('In view', this);
141 on_loaded: function(data) {
142 this.fields_view = data.fields_view;
143 if (data.fields_view.type !== 'search' ||
144 data.fields_view.arch.tag !== 'search') {
145 throw new Error(_.str.sprintf(
146 "Got non-search view after asking for a search view: type %s, arch root %s",
147 data.fields_view.type, data.fields_view.arch.tag));
150 lines = this.make_widgets(
151 data.fields_view['arch'].children,
152 data.fields_view.fields);
154 // for extended search view
155 var ext = new openerp.web.search.ExtendedSearch(this, this.model);
157 this.inputs.push(ext);
158 this.extended_search = ext;
160 var render = QWeb.render("SearchView", {
161 'view': data.fields_view['arch'],
163 'defaults': this.defaults
166 this.$element.html(render);
168 var f = this.$element.find('form');
169 this.$element.find('form')
170 .submit(this.do_search)
171 .bind('reset', this.do_clear);
172 // start() all the widgets
173 var widget_starts = _(lines).chain().flatten().map(function (widget) {
174 return widget.start();
177 $.when.apply(null, widget_starts).then(function () {
178 self.ready.resolve();
181 this.reload_managed_filters();
183 reload_managed_filters: function() {
185 return this.rpc('/web/searchview/get_filters', {
186 model: this.dataset.model
187 }).then(function(result) {
188 self.managed_filters = result;
189 var filters = self.$element.find(".oe_search-view-filters-management");
190 filters.html(QWeb.render("SearchView.managed-filters", {filters: result}));
191 filters.change(self.on_filters_management);
195 * Handle event when the user make a selection in the filters management select box.
197 on_filters_management: function(e) {
199 var select = this.$element.find(".oe_search-view-filters-management");
200 var val = select.val();
202 case 'advanced_filter':
203 this.extended_search.on_activate();
205 case 'add_to_dashboard':
206 this.on_add_to_dashboard();
208 case 'manage_filters':
210 res_model: 'ir.filters',
211 views: [[false, 'list'], [false, 'form']],
212 type: 'ir.actions.act_window',
213 context: {"search_default_user_id": this.session.uid,
214 "search_default_model_id": this.dataset.model},
220 var data = this.build_search_data();
221 var context = new openerp.web.CompoundContext();
222 _.each(data.contexts, function(x) {
225 var domain = new openerp.web.CompoundDomain();
226 _.each(data.domains, function(x) {
229 var groupbys = _.pluck(data.groupbys, "group_by").join();
230 context.add({"group_by": groupbys});
231 var dial_html = QWeb.render("SearchView.managed-filters.add");
232 var $dial = $(dial_html);
235 title: _t("Filter Entry"),
237 {text: _t("Cancel"), click: function() {
238 $(this).dialog("close");
240 {text: _t("OK"), click: function() {
241 $(this).dialog("close");
242 var name = $(this).find("input").val();
243 self.rpc('/web/searchview/save_filter', {
244 model: self.dataset.model,
245 context_to_save: context,
249 self.reload_managed_filters();
256 if (val.slice(0, 4) == "get:") {
258 val = parseInt(val, 10);
259 var filter = this.managed_filters[val];
260 this.do_clear().then(_.bind(function() {
261 var groupbys = _.map(filter.context.group_by.split(","), function(el) {
262 return {"group_by": el};
264 this.on_search([filter.domain], [filter.context], groupbys);
270 on_add_to_dashboard: function() {
271 this.$element.find(".oe_search-view-filters-management")[0].selectedIndex = 0;
273 menu = openerp.webclient.menu,
274 $dialog = $(QWeb.render("SearchView.add_to_dashboard", {
275 dashboards : menu.data.data.children,
276 selected_menu_id : menu.$element.find('a.active').data('menu')
278 $dialog.find('input').val(this.fields_view.name);
281 title: _t("Add to Dashboard"),
283 {text: _t("Cancel"), click: function() {
284 $(this).dialog("close");
286 {text: _t("OK"), click: function() {
287 $(this).dialog("close");
288 var menu_id = $(this).find("select").val(),
289 title = $(this).find("input").val(),
290 data = self.build_search_data(),
291 context = new openerp.web.CompoundContext(),
292 domain = new openerp.web.CompoundDomain();
293 _.each(data.contexts, function(x) {
296 _.each(data.domains, function(x) {
299 self.rpc('/web/searchview/add_to_dashboard', {
301 action_id: self.widget_parent.action.id,
302 context_to_save: context,
304 view_mode: self.widget_parent.active_view,
308 self.do_warn("Could not add filter to dashboard");
310 self.do_notify("Filter added to dashboard", '');
318 * Performs the search view collection of widget data.
320 * If the collection went well (all fields are valid), then triggers
321 * :js:func:`openerp.web.SearchView.on_search`.
323 * If at least one field failed its validation, triggers
324 * :js:func:`openerp.web.SearchView.on_invalid` instead.
326 * @param e jQuery event object coming from the "Search" button
328 do_search: function (e) {
329 if (this.headless && !this.has_defaults) {
330 return this.on_search([], [], []);
332 // reset filters management
333 var select = this.$element.find(".oe_search-view-filters-management");
334 select.val("_filters");
336 if (e && e.preventDefault) { e.preventDefault(); }
338 var data = this.build_search_data();
340 if (data.errors.length) {
341 this.on_invalid(data.errors);
345 this.on_search(data.domains, data.contexts, data.groupbys);
347 build_search_data: function() {
352 _.each(this.inputs, function (input) {
354 var domain = input.get_domain();
356 domains.push(domain);
359 var context = input.get_context();
361 contexts.push(context);
364 if (e instanceof openerp.web.search.Invalid) {
372 // TODO: do we need to handle *fields* with group_by in their context?
373 var groupbys = _(this.enabled_filters)
375 .map(function (filter) { return filter.get_context();})
378 return {domains: domains, contexts: contexts, errors: errors, groupbys: groupbys};
381 * Triggered after the SearchView has collected all relevant domains and
384 * It is provided with an Array of domains and an Array of contexts, which
385 * may or may not be evaluated (each item can be either a valid domain or
386 * context, or a string to evaluate in order in the sequence)
388 * It is also passed an array of contexts used for group_by (they are in
389 * the correct order for group_by evaluation, which contexts may not be)
392 * @param {Array} domains an array of literal domains or domain references
393 * @param {Array} contexts an array of literal contexts or context refs
394 * @param {Array} groupbys ordered contexts which may or may not have group_by keys
396 on_search: function (domains, contexts, groupbys) {
399 * Triggered after a validation error in the SearchView fields.
401 * Error objects have three keys:
402 * * ``field`` is the name of the invalid field
403 * * ``value`` is the invalid value
404 * * ``message`` is the (in)validation message provided by the field
407 * @param {Array} errors a never-empty array of error objects
409 on_invalid: function (errors) {
410 this.do_notify(_t("Invalid Search"), _t("triggered from search view"));
412 do_clear: function () {
413 this.$element.find('.filter_label, .filter_icon').removeClass('enabled');
414 this.enabled_filters.splice(0);
415 var string = $('a.searchview_group_string');
416 _.each(string, function(str){
417 $(str).closest('div.searchview_group').removeClass("expanded").addClass('folded');
419 this.$element.find('table:last').hide();
421 $('.searchview_extended_groups_list').empty();
422 _.each(this.inputs, function (input) {
423 if(input.datewidget && input.datewidget.value) {
424 input.datewidget.set_value(false);
427 return $.async_when().pipe(this.on_clear);
430 * Triggered when the search view gets cleared
434 on_clear: function () {
438 * Called by a filter propagating its state changes
440 * @param {openerp.web.search.Filter} filter a filter which got toggled
441 * @param {Boolean} default_enabled filter got enabled through the default values, at render time.
443 do_toggle_filter: function (filter, default_enabled) {
444 if (default_enabled || filter.is_enabled()) {
445 this.enabled_filters.push(filter);
447 this.enabled_filters = _.without(
448 this.enabled_filters, filter);
451 if (!default_enabled) {
452 // selecting a filter after initial loading automatically
454 this.$element.find('form').submit();
460 openerp.web.search = {};
462 * Registry of search fields, called by :js:class:`openerp.web.SearchView` to
463 * find and instantiate its field widgets.
465 openerp.web.search.fields = new openerp.web.Registry({
466 'char': 'openerp.web.search.CharField',
467 'text': 'openerp.web.search.CharField',
468 'boolean': 'openerp.web.search.BooleanField',
469 'integer': 'openerp.web.search.IntegerField',
470 'float': 'openerp.web.search.FloatField',
471 'selection': 'openerp.web.search.SelectionField',
472 'datetime': 'openerp.web.search.DateTimeField',
473 'date': 'openerp.web.search.DateField',
474 'many2one': 'openerp.web.search.ManyToOneField',
475 'many2many': 'openerp.web.search.CharField',
476 'one2many': 'openerp.web.search.CharField'
478 openerp.web.search.Invalid = openerp.web.Class.extend( /** @lends openerp.web.search.Invalid# */{
480 * Exception thrown by search widgets when they hold invalid values,
481 * which they can not return when asked.
483 * @constructs openerp.web.search.Invalid
484 * @extends openerp.web.Class
486 * @param field the name of the field holding an invalid value
487 * @param value the invalid value
488 * @param message validation failure message
490 init: function (field, value, message) {
493 this.message = message;
495 toString: function () {
496 return _.str.sprintf(
497 _t("Incorrect value for field %(fieldname)s: [%(value)s] is %(message)s"),
498 {fieldname: this.field, value: this.value, message: this.message}
502 openerp.web.search.Widget = openerp.web.Widget.extend( /** @lends openerp.web.search.Widget# */{
505 * Root class of all search widgets
507 * @constructs openerp.web.search.Widget
508 * @extends openerp.web.Widget
510 * @param view the ancestor view of this widget
512 init: function (view) {
517 * Sets and returns a globally unique identifier for the widget.
519 * If a prefix is specified, the identifier will be appended to it.
521 * @params prefix prefix sections, empty/falsy sections will be removed
523 make_id: function () {
524 this.element_id = _.uniqueId(
526 _.compact(_.toArray(arguments)),
528 return this.element_id;
531 * "Starts" the widgets. Called at the end of the rendering, this allows
532 * widgets to hook themselves to their view sections.
534 * On widgets, if they kept a reference to a view and have an element_id,
535 * will fetch and set their root element on $element.
539 if (this.view && this.element_id) {
540 // id is unique, and no getElementById on elements
541 this.$element = $(document.getElementById(
546 * "Stops" the widgets. Called when the view destroys itself, this
547 * lets the widgets clean up after themselves.
553 render: function (defaults) {
555 return this._super(_.extend(this, {defaults: defaults}));
558 openerp.web.search.add_expand_listener = function($root) {
559 $root.find('a.searchview_group_string').click(function (e) {
560 $root.toggleClass('folded expanded');
565 openerp.web.search.Group = openerp.web.search.Widget.extend({
566 template: 'SearchView.group',
567 init: function (view_section, view, fields) {
569 this.attrs = view_section.attrs;
570 this.lines = view.make_widgets(
571 view_section.children, fields);
572 this.make_id('group');
576 openerp.web.search.add_expand_listener(this.$element);
577 var widget_starts = _(this.lines).chain().flatten()
578 .map(function (widget) { return widget.start(); })
580 return $.when.apply(null, widget_starts);
584 openerp.web.search.Input = openerp.web.search.Widget.extend( /** @lends openerp.web.search.Input# */{
586 * @constructs openerp.web.search.Input
587 * @extends openerp.web.search.Widget
591 init: function (view) {
593 this.view.inputs.push(this);
594 this.style = undefined;
596 get_context: function () {
598 "get_context not implemented for widget " + this.attrs.type);
600 get_domain: function () {
602 "get_domain not implemented for widget " + this.attrs.type);
604 load_attrs: function (attrs) {
605 if (attrs.modifiers) {
606 attrs.modifiers = JSON.parse(attrs.modifiers);
607 attrs.invisible = attrs.modifiers.invisible || false;
608 if (attrs.invisible) {
609 this.style = 'display: none;'
615 openerp.web.search.FilterGroup = openerp.web.search.Input.extend(/** @lends openerp.web.search.FilterGroup# */{
616 template: 'SearchView.filters',
618 * Inclusive group of filters, creates a continuous "button" with clickable
619 * sections (the normal display for filters is to be a self-contained button)
621 * @constructs openerp.web.search.FilterGroup
622 * @extends openerp.web.search.Input
624 * @param {Array<openerp.web.search.Filter>} filters elements of the group
625 * @param {openerp.web.SearchView} view view in which the filters are contained
627 init: function (filters, view) {
629 this.filters = filters;
630 this.length = filters.length;
634 _.each(this.filters, function (filter) {
638 get_context: function () { },
640 * Handles domains-fetching for all the filters within it: groups them.
642 get_domain: function () {
643 var domains = _(this.filters).chain()
644 .filter(function (filter) { return filter.is_enabled(); })
645 .map(function (filter) { return filter.attrs.domain; })
649 if (!domains.length) { return; }
650 if (domains.length === 1) { return domains[0]; }
651 for (var i=domains.length; --i;) {
652 domains.unshift(['|']);
654 return _.extend(new openerp.web.CompoundDomain(), {
659 openerp.web.search.Filter = openerp.web.search.Input.extend(/** @lends openerp.web.search.Filter# */{
660 template: 'SearchView.filter',
662 * Implementation of the OpenERP filters (button with a context and/or
663 * a domain sent as-is to the search view)
665 * @constructs openerp.web.search.Filter
666 * @extends openerp.web.search.Input
671 init: function (node, view) {
673 this.load_attrs(node.attrs);
674 this.classes = [this.attrs.string ? 'filter_label' : 'filter_icon'];
675 this.make_id('filter', this.attrs.name);
680 this.$element.click(function (e) {
681 $(this).toggleClass('enabled');
682 self.view.do_toggle_filter(self);
686 * Returns whether the filter is currently enabled (in use) or not.
690 is_enabled:function () {
691 return this.$element.hasClass('enabled');
694 * If the filter is present in the defaults (and has a truthy value),
697 * @param {Object} defaults the search view's default values
699 render: function (defaults) {
700 if (this.attrs.name && defaults[this.attrs.name]) {
701 this.classes.push('enabled');
702 this.view.do_toggle_filter(this, true);
704 return this._super(defaults);
706 get_context: function () {
707 if (!this.is_enabled()) {
710 return this.attrs.context;
713 * Does not return anything: filter domain is handled at the FilterGroup
716 get_domain: function () { }
718 openerp.web.search.Field = openerp.web.search.Input.extend( /** @lends openerp.web.search.Field# */ {
719 template: 'SearchView.field',
720 default_operator: '=',
722 * @constructs openerp.web.search.Field
723 * @extends openerp.web.search.Input
725 * @param view_section
729 init: function (view_section, field, view) {
731 this.load_attrs(_.extend({}, field, view_section.attrs));
732 this.filters = new openerp.web.search.FilterGroup(_.compact(_.map(
733 view_section.children, function (filter_node) {
734 if (filter_node.attrs.string &&
735 typeof console !== 'undefined' && console.debug) {
736 console.debug("Filter-in-field with a 'string' attribute "
739 delete filter_node.attrs.string;
740 return new openerp.web.search.Filter(
743 this.make_id('input', field.type, this.attrs.name);
747 this.filters.start();
749 get_context: function () {
750 var val = this.get_value();
751 // A field needs a value to be "active", and a context to send when
753 var has_value = (val !== null && val !== '');
754 var context = this.attrs.context;
755 if (!(has_value && context)) {
760 {own_values: {self: val}});
763 * Function creating the returned domain for the field, override this
764 * methods in children if you only need to customize the field's domain
765 * without more complex alterations or tests (and without the need to
766 * change override the handling of filter_domain)
768 * @param {String} name the field's name
769 * @param {String} operator the field's operator (either attribute-specified or default operator for the field
770 * @param {Number|String} value parsed value for the field
771 * @returns {Array<Array>} domain to include in the resulting search
773 make_domain: function (name, operator, value) {
774 return [[name, operator, value]];
776 get_domain: function () {
777 var val = this.get_value();
778 if (val === null || val === '') {
782 var domain = this.attrs['filter_domain'];
784 return this.make_domain(
786 this.attrs.operator || this.default_operator,
789 return _.extend({}, domain, {own_values: {self: val}});
793 * Implementation of the ``char`` OpenERP field type:
795 * * Default operator is ``ilike`` rather than ``=``
797 * * The Javascript and the HTML values are identical (strings)
800 * @extends openerp.web.search.Field
802 openerp.web.search.CharField = openerp.web.search.Field.extend( /** @lends openerp.web.search.CharField# */ {
803 default_operator: 'ilike',
804 get_value: function () {
805 return this.$element.val();
808 openerp.web.search.NumberField = openerp.web.search.Field.extend(/** @lends openerp.web.search.NumberField# */{
809 get_value: function () {
810 if (!this.$element.val()) {
813 var val = this.parse(this.$element.val()),
814 check = Number(this.$element.val());
815 if (isNaN(val) || val !== check) {
816 this.$element.addClass('error');
817 throw new openerp.web.search.Invalid(
818 this.attrs.name, this.$element.val(), this.error_message);
820 this.$element.removeClass('error');
826 * @extends openerp.web.search.NumberField
828 openerp.web.search.IntegerField = openerp.web.search.NumberField.extend(/** @lends openerp.web.search.IntegerField# */{
829 error_message: _t("not a valid integer"),
830 parse: function (value) {
832 return openerp.web.parse_value(value, {'widget': 'integer'});
840 * @extends openerp.web.search.NumberField
842 openerp.web.search.FloatField = openerp.web.search.NumberField.extend(/** @lends openerp.web.search.FloatField# */{
843 error_message: _t("not a valid number"),
844 parse: function (value) {
846 return openerp.web.parse_value(value, {'widget': 'float'});
854 * @extends openerp.web.search.Field
856 openerp.web.search.SelectionField = openerp.web.search.Field.extend(/** @lends openerp.web.search.SelectionField# */{
857 // This implementation is a basic <select> field, but it may have to be
858 // altered to be more in line with the GTK client, which uses a combo box
859 // (~ jquery.autocomplete):
860 // * If an option was selected in the list, behave as currently
861 // * If something which is not in the list was entered (via the text input),
862 // the default domain should become (`ilike` string_value) but **any
863 // ``context`` or ``filter_domain`` becomes falsy, idem if ``@operator``
864 // is specified. So at least get_domain needs to be quite a bit
865 // overridden (if there's no @value and there is no filter_domain and
866 // there is no @operator, return [[name, 'ilike', str_val]]
867 template: 'SearchView.field.selection',
869 this._super.apply(this, arguments);
870 // prepend empty option if there is no empty option in the selection list
871 this.prepend_empty = !_(this.attrs.selection).detect(function (item) {
875 get_value: function () {
876 var index = parseInt(this.$element.val(), 10);
877 if (isNaN(index)) { return null; }
878 var value = this.attrs.selection[index][0];
879 if (value === false) { return null; }
883 * The selection field needs a default ``false`` value in case none is
884 * provided, so that selector options with a ``false`` value (convention
885 * for explicitly empty options) get selected by default rather than the
886 * first (value-holding) option in the selection.
888 * @param {Object} defaults search default values
890 render: function (defaults) {
891 if (!defaults[this.attrs.name]) {
892 defaults[this.attrs.name] = false;
894 return this._super(defaults);
897 openerp.web.search.BooleanField = openerp.web.search.SelectionField.extend(/** @lends openerp.web.search.BooleanField# */{
899 * @constructs openerp.web.search.BooleanField
900 * @extends openerp.web.search.BooleanField
903 this._super.apply(this, arguments);
904 this.attrs.selection = [
910 * Search defaults likely to be boolean values (for a boolean field).
912 * In the HTML, we only want/get strings, and our strings here are ``true``
913 * and ``false``, so ensure we use precisely those by truth-testing the
914 * default value (iif there is one in the view's defaults).
916 * @param {Object} defaults default values for this search view
917 * @returns {String} rendered boolean field
919 render: function (defaults) {
920 var name = this.attrs.name;
921 if (name in defaults) {
922 defaults[name] = defaults[name] ? "true" : "false";
924 return this._super(defaults);
926 get_value: function () {
927 switch (this.$element.val()) {
928 case 'false': return false;
929 case 'true': return true;
930 default: return null;
936 * @extends openerp.web.search.DateField
938 openerp.web.search.DateField = openerp.web.search.Field.extend(/** @lends openerp.web.search.DateField# */{
939 template: "SearchView.date",
942 this.datewidget = new openerp.web.DateWidget(this);
943 this.datewidget.prependTo(this.$element);
944 this.datewidget.$element.find("input").attr("size", 15);
945 this.datewidget.$element.find("input").attr("autofocus",
946 this.attrs.default_focus === '1' ? 'autofocus' : undefined);
947 this.datewidget.set_value(this.defaults[this.attrs.name] || false);
949 get_value: function () {
950 return this.datewidget.get_value() || null;
954 * Implementation of the ``datetime`` openerp field type:
956 * * Uses the same widget as the ``date`` field type (a simple date)
958 * * Builds a slighly more complex, it's a datetime range (includes time)
959 * spanning the whole day selected by the date widget
962 * @extends openerp.web.DateField
964 openerp.web.search.DateTimeField = openerp.web.search.DateField.extend(/** @lends openerp.web.search.DateTimeField# */{
965 make_domain: function (name, operator, value) {
966 return ['&', [name, '>=', value + ' 00:00:00'],
967 [name, '<=', value + ' 23:59:59']];
970 openerp.web.search.ManyToOneField = openerp.web.search.CharField.extend({
971 init: function (view_section, field, view) {
972 this._super(view_section, field, view);
974 this.got_name = $.Deferred().then(function () {
975 self.$element.val(self.name);
977 this.dataset = new openerp.web.DataSet(
978 this.view, this.attrs['relation']);
982 this.setup_autocomplete();
983 var started = $.Deferred();
984 this.got_name.then(function () { started.resolve();},
985 function () { started.resolve(); });
986 return started.promise();
988 setup_autocomplete: function () {
990 this.$element.autocomplete({
991 source: function (req, resp) {
992 self.dataset.name_search(
993 req.term, self.attrs.domain, 'ilike', 8, function (data) {
994 resp(_.map(data, function (result) {
995 return {id: result[0], label: result[1]}
999 select: function (event, ui) {
1000 self.id = ui.item.id;
1001 self.name = ui.item.label;
1006 on_name_get: function (name_get) {
1007 if (!name_get.length) {
1009 this.got_name.reject();
1012 this.name = name_get[0][1];
1013 this.got_name.resolve();
1015 render: function (defaults) {
1016 if (defaults[this.attrs.name]) {
1017 this.id = defaults[this.attrs.name];
1018 if (this.id instanceof Array)
1019 this.id = this.id[0];
1020 // TODO: maybe this should not be completely removed
1021 delete defaults[this.attrs.name];
1022 this.dataset.name_get([this.id], $.proxy(this, 'on_name_get'));
1024 this.got_name.reject();
1026 return this._super(defaults);
1028 make_domain: function (name, operator, value) {
1029 if (this.id && this.name) {
1030 if (value === this.name) {
1031 return [[name, '=', this.id]];
1037 return this._super(name, operator, value);
1041 openerp.web.search.ExtendedSearch = openerp.web.OldWidget.extend({
1042 template: 'SearchView.extended_search',
1043 identifier_prefix: 'extended-search',
1044 init: function (parent, model) {
1045 this._super(parent);
1048 add_group: function() {
1049 var group = new openerp.web.search.ExtendedSearchGroup(this, this.fields);
1050 group.appendTo(this.$element.find('.searchview_extended_groups_list'));
1051 this.check_last_element();
1053 start: function () {
1054 this.$element = $("#" + this.element_id);
1055 this.$element.closest("table.oe-searchview-render-line").css("display", "none");
1057 this.rpc("/web/searchview/fields_get",
1058 {"model": this.model}, function(data) {
1059 self.fields = data.fields;
1060 if (!('id' in self.fields)) {
1066 openerp.web.search.add_expand_listener(self.$element);
1067 self.$element.find('.searchview_extended_add_group').click(function (e) {
1072 get_context: function() {
1075 get_domain: function() {
1076 if (!this.$element) {
1077 return null; // not a logical state but sometimes it happens
1079 if(this.$element.closest("table.oe-searchview-render-line").css("display") == "none") {
1082 return _.reduce(this.widget_children,
1083 function(mem, x) { return mem.concat(x.get_domain());}, []);
1085 on_activate: function() {
1087 var table = this.$element.closest("table.oe-searchview-render-line");
1088 table.css("display", "");
1089 if(this.$element.hasClass("folded")) {
1090 this.$element.toggleClass("folded expanded");
1094 var table = this.$element.closest("table.oe-searchview-render-line");
1095 table.css("display", "none");
1096 if(this.$element.hasClass("expanded")) {
1097 this.$element.toggleClass("folded expanded");
1100 check_last_element: function() {
1101 _.each(this.widget_children, function(x) {x.set_last_group(false);});
1102 if (this.widget_children.length >= 1) {
1103 this.widget_children[this.widget_children.length - 1].set_last_group(true);
1108 openerp.web.search.ExtendedSearchGroup = openerp.web.OldWidget.extend({
1109 template: 'SearchView.extended_search.group',
1110 identifier_prefix: 'extended-search-group',
1111 init: function (parent, fields) {
1112 this._super(parent);
1113 this.fields = fields;
1115 add_prop: function() {
1116 var prop = new openerp.web.search.ExtendedSearchProposition(this, this.fields);
1117 var render = prop.render({'index': this.widget_children.length - 1});
1118 this.$element.find('.searchview_extended_propositions_list').append(render);
1121 start: function () {
1122 this.$element = $("#" + this.element_id);
1125 this.$element.find('.searchview_extended_add_proposition').click(function () {
1128 this.$element.find('.searchview_extended_delete_group').click(function () {
1132 get_domain: function() {
1133 var props = _(this.widget_children).chain().map(function(x) {
1134 return x.get_proposition();
1135 }).compact().value();
1136 var choice = this.$element.find(".searchview_extended_group_choice").val();
1137 var op = choice == "all" ? "&" : "|";
1138 return choice == "none" ? ['!'] : [].concat(
1139 _.map(_.range(_.max([0,props.length - 1])), function() { return op; }),
1143 var parent = this.widget_parent;
1144 if (this.widget_parent.widget_children.length == 1)
1145 this.widget_parent.hide();
1147 parent.check_last_element();
1149 set_last_group: function(is_last) {
1150 this.$element.toggleClass('last_group', is_last);
1154 openerp.web.search.ExtendedSearchProposition = openerp.web.OldWidget.extend(/** @lends openerp.web.search.ExtendedSearchProposition# */{
1155 template: 'SearchView.extended_search.proposition',
1156 identifier_prefix: 'extended-search-proposition',
1158 * @constructs openerp.web.search.ExtendedSearchProposition
1159 * @extends openerp.web.OldWidget
1164 init: function (parent, fields) {
1165 this._super(parent);
1166 this.fields = _(fields).chain()
1167 .map(function(val, key) { return _.extend({}, val, {'name': key}); })
1168 .sortBy(function(field) {return field.string;})
1170 this.attrs = {_: _, fields: this.fields, selected: null};
1173 start: function () {
1174 this.$element = $("#" + this.element_id);
1175 this.select_field(this.fields.length > 0 ? this.fields[0] : null);
1177 this.$element.find(".searchview_extended_prop_field").change(function() {
1180 this.$element.find('.searchview_extended_delete_prop').click(function () {
1186 if (this.widget_parent.widget_children.length == 1)
1187 parent = this.widget_parent;
1192 changed: function() {
1193 var nval = this.$element.find(".searchview_extended_prop_field").val();
1194 if(this.attrs.selected == null || nval != this.attrs.selected.name) {
1195 this.select_field(_.detect(this.fields, function(x) {return x.name == nval;}));
1199 * Selects the provided field object
1201 * @param field a field descriptor object (as returned by fields_get, augmented by the field name)
1203 select_field: function(field) {
1205 if(this.attrs.selected != null) {
1208 this.$element.find('.searchview_extended_prop_op').html('');
1210 this.attrs.selected = field;
1215 var type = field.type;
1217 openerp.web.search.custom_filters.get_object(type);
1219 if (! e instanceof openerp.web.KeyNotFound) {
1223 console.log('Unknow field type ' + e.key);
1225 this.value = new (openerp.web.search.custom_filters.get_object(type))
1227 if(this.value.set_field) {
1228 this.value.set_field(field);
1230 _.each(this.value.operators, function(operator) {
1231 $('<option>', {value: operator.value})
1232 .text(String(operator.text))
1233 .appendTo(self.$element.find('.searchview_extended_prop_op'));
1235 this.$element.find('.searchview_extended_prop_value').html(
1236 this.value.render({}));
1240 get_proposition: function() {
1241 if ( this.attrs.selected == null)
1243 var field = this.attrs.selected.name;
1244 var op = this.$element.find('.searchview_extended_prop_op').val();
1245 var value = this.value.get_value();
1246 return [field, op, value];
1250 openerp.web.search.ExtendedSearchProposition.Field = openerp.web.OldWidget.extend({
1251 start: function () {
1252 this.$element = $("#" + this.element_id);
1255 openerp.web.search.ExtendedSearchProposition.Char = openerp.web.search.ExtendedSearchProposition.Field.extend({
1256 template: 'SearchView.extended_search.proposition.char',
1257 identifier_prefix: 'extended-search-proposition-char',
1259 {value: "ilike", text: _lt("contains")},
1260 {value: "not ilike", text: _lt("doesn't contain")},
1261 {value: "=", text: _lt("is equal to")},
1262 {value: "!=", text: _lt("is not equal to")},
1263 {value: ">", text: _lt("greater than")},
1264 {value: "<", text: _lt("less than")},
1265 {value: ">=", text: _lt("greater or equal than")},
1266 {value: "<=", text: _lt("less or equal than")}
1268 get_value: function() {
1269 return this.$element.val();
1272 openerp.web.search.ExtendedSearchProposition.DateTime = openerp.web.search.ExtendedSearchProposition.Field.extend({
1273 template: 'SearchView.extended_search.proposition.empty',
1274 identifier_prefix: 'extended-search-proposition-datetime',
1276 {value: "=", text: _lt("is equal to")},
1277 {value: "!=", text: _lt("is not equal to")},
1278 {value: ">", text: _lt("greater than")},
1279 {value: "<", text: _lt("less than")},
1280 {value: ">=", text: _lt("greater or equal than")},
1281 {value: "<=", text: _lt("less or equal than")}
1283 get_value: function() {
1284 return this.datewidget.get_value();
1288 this.datewidget = new openerp.web.DateTimeWidget(this);
1289 this.datewidget.prependTo(this.$element);
1292 openerp.web.search.ExtendedSearchProposition.Date = openerp.web.search.ExtendedSearchProposition.Field.extend({
1293 template: 'SearchView.extended_search.proposition.empty',
1294 identifier_prefix: 'extended-search-proposition-date',
1296 {value: "=", text: _lt("is equal to")},
1297 {value: "!=", text: _lt("is not equal to")},
1298 {value: ">", text: _lt("greater than")},
1299 {value: "<", text: _lt("less than")},
1300 {value: ">=", text: _lt("greater or equal than")},
1301 {value: "<=", text: _lt("less or equal than")}
1303 get_value: function() {
1304 return this.datewidget.get_value();
1308 this.datewidget = new openerp.web.DateWidget(this);
1309 this.datewidget.prependTo(this.$element);
1312 openerp.web.search.ExtendedSearchProposition.Integer = openerp.web.search.ExtendedSearchProposition.Field.extend({
1313 template: 'SearchView.extended_search.proposition.integer',
1314 identifier_prefix: 'extended-search-proposition-integer',
1316 {value: "=", text: _lt("is equal to")},
1317 {value: "!=", text: _lt("is not equal to")},
1318 {value: ">", text: _lt("greater than")},
1319 {value: "<", text: _lt("less than")},
1320 {value: ">=", text: _lt("greater or equal than")},
1321 {value: "<=", text: _lt("less or equal than")}
1323 get_value: function() {
1325 return openerp.web.parse_value(this.$element.val(), {'widget': 'integer'});
1331 openerp.web.search.ExtendedSearchProposition.Id = openerp.web.search.ExtendedSearchProposition.Integer.extend({
1332 operators: [{value: "=", text: _lt("is")}]
1334 openerp.web.search.ExtendedSearchProposition.Float = openerp.web.search.ExtendedSearchProposition.Field.extend({
1335 template: 'SearchView.extended_search.proposition.float',
1336 identifier_prefix: 'extended-search-proposition-float',
1338 {value: "=", text: _lt("is equal to")},
1339 {value: "!=", text: _lt("is not equal to")},
1340 {value: ">", text: _lt("greater than")},
1341 {value: "<", text: _lt("less than")},
1342 {value: ">=", text: _lt("greater or equal than")},
1343 {value: "<=", text: _lt("less or equal than")}
1345 get_value: function() {
1347 return openerp.web.parse_value(this.$element.val(), {'widget': 'float'});
1353 openerp.web.search.ExtendedSearchProposition.Selection = openerp.web.search.ExtendedSearchProposition.Field.extend({
1354 template: 'SearchView.extended_search.proposition.selection',
1355 identifier_prefix: 'extended-search-proposition-selection',
1357 {value: "=", text: _lt("is")},
1358 {value: "!=", text: _lt("is not")}
1360 set_field: function(field) {
1363 get_value: function() {
1364 return this.$element.val();
1367 openerp.web.search.ExtendedSearchProposition.Boolean = openerp.web.search.ExtendedSearchProposition.Field.extend({
1368 template: 'SearchView.extended_search.proposition.boolean',
1369 identifier_prefix: 'extended-search-proposition-boolean',
1371 {value: "=", text: _lt("is true")},
1372 {value: "!=", text: _lt("is false")}
1374 get_value: function() {
1379 openerp.web.search.custom_filters = new openerp.web.Registry({
1380 'char': 'openerp.web.search.ExtendedSearchProposition.Char',
1381 'text': 'openerp.web.search.ExtendedSearchProposition.Char',
1382 'one2many': 'openerp.web.search.ExtendedSearchProposition.Char',
1383 'many2one': 'openerp.web.search.ExtendedSearchProposition.Char',
1384 'many2many': 'openerp.web.search.ExtendedSearchProposition.Char',
1386 'datetime': 'openerp.web.search.ExtendedSearchProposition.DateTime',
1387 'date': 'openerp.web.search.ExtendedSearchProposition.Date',
1388 'integer': 'openerp.web.search.ExtendedSearchProposition.Integer',
1389 'float': 'openerp.web.search.ExtendedSearchProposition.Float',
1390 'boolean': 'openerp.web.search.ExtendedSearchProposition.Boolean',
1391 'selection': 'openerp.web.search.ExtendedSearchProposition.Selection',
1393 'id': 'openerp.web.search.ExtendedSearchProposition.Id'
1398 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: