You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
8.2 KiB

  1. /*global openerp, _, $ */
  2. openerp.web_m2x_options = function (instance) {
  3. "use strict";
  4. var QWeb = instance.web.qweb,
  5. _t = instance.web._t,
  6. _lt = instance.web._lt;
  7. instance.web.form.FieldMany2One.include({
  8. show_error_displayer: function () {
  9. if ((typeof this.options.m2o_dialog === 'undefined' && this.can_create) ||
  10. this.options.m2o_dialog) {
  11. new instance.web.form.M2ODialog(this).open();
  12. }
  13. },
  14. get_search_result: function (search_val) {
  15. var def = $.Deferred();
  16. var self = this;
  17. // add options limit used to change number of selections record
  18. // returned.
  19. if (typeof this.options.limit === 'number') {
  20. this.limit = this.options.limit;
  21. }
  22. var dataset = new instance.web.DataSet(this, this.field.relation,
  23. self.build_context());
  24. var blacklist = this.get_search_blacklist();
  25. this.last_query = search_val;
  26. var search_result = this.orderer.add(dataset.name_search(
  27. search_val,
  28. new instance.web.CompoundDomain(
  29. self.build_domain(), [["id", "not in", blacklist]]),
  30. 'ilike', this.limit + 1,
  31. self.build_context()));
  32. var create_rights;
  33. if (typeof this.options.create === "undefined" ||
  34. typeof this.options.create_edit === "undefined") {
  35. create_rights = new instance.web.Model(this.field.relation).call(
  36. "check_access_rights", ["create", false]);
  37. }
  38. $.when(search_result, create_rights).then(function (_data, _can_create) {
  39. var data = _data[0];
  40. var can_create = _can_create ? _can_create[0] : null;
  41. self.can_create = can_create; // for ``.show_error_displayer()``
  42. self.last_search = data;
  43. // possible selections for the m2o
  44. var values = _.map(data, function (x) {
  45. x[1] = x[1].split("\n")[0];
  46. return {
  47. label: _.str.escapeHTML(x[1]),
  48. value: x[1],
  49. name: x[1],
  50. id: x[0],
  51. };
  52. });
  53. // search more... if more results that max
  54. if (values.length > self.limit) {
  55. values = values.slice(0, self.limit);
  56. values.push({
  57. label: _t("Search More..."),
  58. action: function () {
  59. dataset.name_search(
  60. search_val, self.build_domain(),
  61. 'ilike', false).done(function (data) {
  62. self._search_create_popup("search", data);
  63. });
  64. },
  65. classname: 'oe_m2o_dropdown_option'
  66. });
  67. }
  68. // quick create
  69. var raw_result = _(data.result).map(function (x) {
  70. return x[1];
  71. });
  72. if ((typeof self.options.create === 'undefined' && can_create) ||
  73. self.options.create) {
  74. if (search_val.length > 0 &&
  75. !_.include(raw_result, search_val)) {
  76. values.push({
  77. label: _.str.sprintf(
  78. _t('Create "<strong>%s</strong>"'),
  79. $('<span />').text(search_val).html()),
  80. action: function () {
  81. self._quick_create(search_val);
  82. },
  83. classname: 'oe_m2o_dropdown_option'
  84. });
  85. }
  86. }
  87. // create...
  88. if ((typeof self.options.create_edit === 'undefined' && can_create) ||
  89. self.options.create_edit) {
  90. values.push({
  91. label: _t("Create and Edit..."),
  92. action: function () {
  93. self._search_create_popup(
  94. "form", undefined,
  95. self._create_context(search_val));
  96. },
  97. classname: 'oe_m2o_dropdown_option'
  98. });
  99. }
  100. def.resolve(values);
  101. });
  102. return def;
  103. }
  104. });
  105. instance.web.form.FieldMany2ManyTags.include({
  106. show_error_displayer: function () {
  107. if ((typeof this.options.m2o_dialog === 'undefined' && this.can_create) ||
  108. this.options.m2o_dialog) {
  109. new instance.web.form.M2ODialog(this).open();
  110. }
  111. },
  112. /**
  113. * Call this method to search using a string.
  114. */
  115. get_search_result: function(search_val) {
  116. var self = this;
  117. // add options limit used to change number of selections record
  118. // returned.
  119. if (typeof this.options.limit === 'number') {
  120. this.limit = this.options.limit;
  121. }
  122. var dataset = new instance.web.DataSet(this, this.field.relation, self.build_context());
  123. var blacklist = this.get_search_blacklist();
  124. this.last_query = search_val;
  125. return this.orderer.add(dataset.name_search(
  126. search_val, new instance.web.CompoundDomain(self.build_domain(), [["id", "not in", blacklist]]),
  127. 'ilike', this.limit + 1, self.build_context())).then(function(data) {
  128. self.last_search = data;
  129. // possible selections for the m2o
  130. var values = _.map(data, function(x) {
  131. x[1] = x[1].split("\n")[0];
  132. return {
  133. label: _.str.escapeHTML(x[1]),
  134. value: x[1],
  135. name: x[1],
  136. id: x[0],
  137. };
  138. });
  139. // search more... if more results that max
  140. if (values.length > self.limit) {
  141. values = values.slice(0, self.limit);
  142. values.push({
  143. label: _t("Search More..."),
  144. action: function() {
  145. dataset.name_search(search_val, self.build_domain(), 'ilike', false).done(function(data) {
  146. self._search_create_popup("search", data);
  147. });
  148. },
  149. classname: 'oe_m2o_dropdown_option'
  150. });
  151. }
  152. // quick create
  153. if ((typeof self.options.create === 'undefined') ||
  154. self.options.create) {
  155. var raw_result = _(data.result).map(function(x) {return x[1];});
  156. if (search_val.length > 0 && !_.include(raw_result, search_val)) {
  157. values.push({
  158. label: _.str.sprintf(_t('Create "<strong>%s</strong>"'),
  159. $('<span />').text(search_val).html()),
  160. action: function() {
  161. self._quick_create(search_val);
  162. },
  163. classname: 'oe_m2o_dropdown_option'
  164. });
  165. }
  166. }
  167. // create...
  168. if ((typeof self.options.create_edit === 'undefined') ||
  169. self.options.create_edit) {
  170. values.push({
  171. label: _t("Create and Edit..."),
  172. action: function() {
  173. self._search_create_popup("form", undefined, self._create_context(search_val));
  174. },
  175. classname: 'oe_m2o_dropdown_option'
  176. });
  177. }
  178. return values;
  179. })
  180. },
  181. });
  182. };