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.

388 lines
16 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. var OPTIONS = ['web_m2x_options.create',
  8. 'web_m2x_options.create_edit',
  9. 'web_m2x_options.limit',
  10. 'web_m2x_options.search_more',
  11. 'web_m2x_options.m2o_dialog',];
  12. instance.web.form.FieldMany2One = instance.web.form.FieldMany2One.extend({
  13. start: function() {
  14. this._super.apply(this, arguments);
  15. return this.get_options();
  16. },
  17. get_options: function() {
  18. var self = this;
  19. if (!_.isUndefined(this.view) && _.isUndefined(this.view.ir_options_loaded)) {
  20. this.view.ir_options_loaded = $.Deferred();
  21. this.view.ir_options = {};
  22. (new instance.web.Model("ir.config_parameter"))
  23. .query(["key", "value"]).filter([['key', 'in', OPTIONS]])
  24. .all().then(function(records) {
  25. _(records).each(function(record) {
  26. self.view.ir_options[record.key] = record.value;
  27. });
  28. self.view.ir_options_loaded.resolve();
  29. });
  30. return this.view.ir_options_loaded;
  31. }
  32. return $.when();
  33. },
  34. is_option_set: function(option) {
  35. if (_.isUndefined(option)) {
  36. return false
  37. }
  38. var is_string = typeof option === 'string'
  39. var is_bool = typeof option === 'boolean'
  40. if (is_string) {
  41. return option === 'true' || option === 'True'
  42. } else if (is_bool) {
  43. return option
  44. }
  45. return false
  46. },
  47. show_error_displayer: function () {
  48. if(this.is_option_set(this.options.m2o_dialog) ||
  49. _.isUndefined(this.options.m2o_dialog) && this.is_option_set(this.view.ir_options['web_m2x_options.m2o_dialog']) ||
  50. this.can_create && _.isUndefined(this.options.m2o_dialog) && _.isUndefined(this.view.ir_options['web_m2x_options.m2o_dialog'])) {
  51. new instance.web.form.M2ODialog(this).open();
  52. }
  53. },
  54. get_search_result: function (search_val) {
  55. var Objects = new instance.web.Model(this.field.relation);
  56. var def = $.Deferred();
  57. var self = this;
  58. // add options limit used to change number of selections record
  59. // returned.
  60. if (_.isUndefined(this.view))
  61. return this._super.apply(this, arguments);
  62. if (!_.isUndefined(this.view.ir_options['web_m2x_options.limit'])) {
  63. this.limit = parseInt(this.view.ir_options['web_m2x_options.limit']);
  64. }
  65. if (typeof this.options.limit === 'number') {
  66. this.limit = this.options.limit;
  67. }
  68. // add options search_more to force enable or disable search_more button
  69. if (this.is_option_set(this.options.search_more) || _.isUndefined(this.options.search_more) && this.is_option_set(self.view.ir_options['web_m2x_options.search_more'])) {
  70. this.search_more = true
  71. }
  72. // add options field_color and colors to color item(s) depending on field_color value
  73. this.field_color = this.options.field_color
  74. this.colors = this.options.colors
  75. var dataset = new instance.web.DataSet(this, this.field.relation,
  76. self.build_context());
  77. var blacklist = this.get_search_blacklist();
  78. this.last_query = search_val;
  79. var search_result = this.orderer.add(dataset.name_search(
  80. search_val,
  81. new instance.web.CompoundDomain(
  82. self.build_domain(), [["id", "not in", blacklist]]),
  83. 'ilike', this.limit + 1,
  84. self.build_context()));
  85. var create_rights;
  86. if (!(self.options && (self.options.no_create || self.options.no_create_edit))) {
  87. create_rights = new instance.web.Model(this.field.relation).call(
  88. "check_access_rights", ["create", false]);
  89. }
  90. $.when(search_result, create_rights).then(function (data, can_create) {
  91. self.can_create = can_create; // for ``.show_error_displayer()``
  92. self.last_search = data;
  93. // possible selections for the m2o
  94. var values = _.map(data, function (x) {
  95. x[1] = x[1].split("\n")[0];
  96. return {
  97. label: _.str.escapeHTML(x[1]),
  98. value: x[1],
  99. name: x[1],
  100. id: x[0],
  101. };
  102. });
  103. // Search result value colors
  104. if (self.colors && self.field_color) {
  105. var value_ids = [];
  106. for (var index in values) {
  107. value_ids.push(values[index].id);
  108. }
  109. // RPC request to get field_color from Objects
  110. Objects.query([self.field_color])
  111. .filter([['id', 'in', value_ids]])
  112. .all().done(function (objects) {
  113. for (var index in objects) {
  114. for (var index_value in values) {
  115. if (values[index_value].id == objects[index].id) {
  116. // Find value in values by comparing ids
  117. var value = values[index_value];
  118. // Find color with field value as key
  119. var color = self.colors[objects[index][self.field_color]] || 'black';
  120. value.label = '<span style="color:'+color+'">'+value.label+'</span>';
  121. break;
  122. }
  123. }
  124. }
  125. def.resolve(values);
  126. });
  127. }
  128. // search more... if more results that max
  129. if (values.length > self.limit || self.search_more) {
  130. values = values.slice(0, self.limit);
  131. values.push({
  132. label: _t("Search More..."),
  133. action: function () {
  134. // limit = 80 for improving performance, similar
  135. // to Odoo implementation here:
  136. // https://github.com/odoo/odoo/commit/8c3cdce539d87775b59b3f2d5ceb433f995821bf
  137. dataset.name_search(
  138. search_val, self.build_domain(),
  139. 'ilike', 80).done(function (data) {
  140. self._search_create_popup("search", data);
  141. });
  142. },
  143. classname: 'oe_m2o_dropdown_option'
  144. });
  145. }
  146. // quick create
  147. var raw_result = _(data.result).map(function (x) {
  148. return x[1];
  149. });
  150. var no_quick_create = (
  151. self.options && (self.options.no_create ||
  152. self.options.no_quick_create)
  153. )
  154. var m2x_create_undef = _.isUndefined(self.view.ir_options['web_m2x_options.create'])
  155. var m2x_create = self.view.ir_options['web_m2x_options.create'] == "True"
  156. if (!no_quick_create && ((m2x_create_undef && can_create) ||
  157. m2x_create)) {
  158. if (search_val.length > 0 &&
  159. !_.include(raw_result, search_val)) {
  160. values.push({
  161. label: _.str.sprintf(
  162. _t('Create "<strong>%s</strong>"'),
  163. $('<span />').text(search_val).html()),
  164. action: function () {
  165. self._quick_create(search_val);
  166. },
  167. classname: 'oe_m2o_dropdown_option'
  168. });
  169. }
  170. }
  171. // create...
  172. var no_create_edit = (
  173. self.options && (self.options.no_create ||
  174. self.options.no_create_edit)
  175. )
  176. var m2x_create_edit_undef = _.isUndefined(self.view.ir_options['web_m2x_options.create_edit'])
  177. var m2x_create_edit = self.view.ir_options['web_m2x_options.create_edit'] == "True"
  178. if (!no_create_edit && ((m2x_create_edit_undef && can_create) ||
  179. m2x_create_edit)) {
  180. values.push({
  181. label: _t("Create and Edit..."),
  182. action: function () {
  183. self._search_create_popup(
  184. "form", undefined,
  185. self._create_context(search_val));
  186. },
  187. classname: 'oe_m2o_dropdown_option'
  188. });
  189. }
  190. // Check if colors specified to wait for RPC
  191. if (!(self.field_color && self.colors)){
  192. def.resolve(values);
  193. }
  194. });
  195. return def;
  196. }
  197. });
  198. instance.web.form.FieldMany2ManyTags.include({
  199. show_error_displayer: function () {
  200. if ((typeof this.options.m2o_dialog === 'undefined' && this.can_create) ||
  201. this.options.m2o_dialog) {
  202. new instance.web.form.M2ODialog(this).open();
  203. }
  204. },
  205. start: function() {
  206. this._super.apply(this, arguments);
  207. return this.get_options();
  208. },
  209. get_options: function() {
  210. var self = this;
  211. if (_.isUndefined(this.view.ir_options_loaded)) {
  212. this.view.ir_options_loaded = $.Deferred();
  213. this.view.ir_options = {};
  214. (new instance.web.Model("ir.config_parameter"))
  215. .query(["key", "value"]).filter([['key', 'in', OPTIONS]])
  216. .all().then(function(records) {
  217. _(records).each(function(record) {
  218. self.view.ir_options[record.key] = record.value;
  219. });
  220. self.view.ir_options_loaded.resolve();
  221. });
  222. }
  223. return this.view.ir_options_loaded;
  224. },
  225. /**
  226. * Call this method to search using a string.
  227. */
  228. get_search_result: function(search_val) {
  229. var self = this;
  230. // add options limit used to change number of selections record
  231. // returned.
  232. if (!_.isUndefined(this.view.ir_options['web_m2x_options.limit'])) {
  233. this.limit = parseInt(this.view.ir_options['web_m2x_options.limit']);
  234. }
  235. if (typeof this.options.limit === 'number') {
  236. this.limit = this.options.limit;
  237. }
  238. var dataset = new instance.web.DataSet(this, this.field.relation, self.build_context());
  239. var blacklist = this.get_search_blacklist();
  240. this.last_query = search_val;
  241. return this.orderer.add(dataset.name_search(
  242. search_val, new instance.web.CompoundDomain(self.build_domain(), [["id", "not in", blacklist]]),
  243. 'ilike', this.limit + 1, self.build_context())).then(function(data) {
  244. self.last_search = data;
  245. // possible selections for the m2o
  246. var values = _.map(data, function(x) {
  247. x[1] = x[1].split("\n")[0];
  248. return {
  249. label: _.str.escapeHTML(x[1]),
  250. value: x[1],
  251. name: x[1],
  252. id: x[0],
  253. };
  254. });
  255. // search more... if more results that max
  256. if (values.length > self.limit) {
  257. values = values.slice(0, self.limit);
  258. values.push({
  259. label: _t("Search More..."),
  260. action: function() {
  261. // limit = 80 for improving performance, similar
  262. // to Odoo implementation here:
  263. // https://github.com/odoo/odoo/commit/8c3cdce539d87775b59b3f2d5ceb433f995821bf
  264. dataset.name_search(search_val, self.build_domain(), 'ilike', 80).done(function(data) {
  265. self._search_create_popup("search", data);
  266. });
  267. },
  268. classname: 'oe_m2o_dropdown_option'
  269. });
  270. }
  271. // quick create
  272. var no_quick_create = (
  273. self.options && (self.options.no_create ||
  274. self.options.no_quick_create)
  275. )
  276. var m2x_create_undef = _.isUndefined(self.view.ir_options['web_m2x_options.create'])
  277. var m2x_create = self.view.ir_options['web_m2x_options.create'] == "True"
  278. if (!no_quick_create && (m2x_create_undef || m2x_create)) {
  279. var raw_result = _(data.result).map(function(x) {return x[1];});
  280. if (search_val.length > 0 && !_.include(raw_result, search_val)) {
  281. values.push({
  282. label: _.str.sprintf(_t('Create "<strong>%s</strong>"'),
  283. $('<span />').text(search_val).html()),
  284. action: function() {
  285. self._quick_create(search_val);
  286. },
  287. classname: 'oe_m2o_dropdown_option'
  288. });
  289. }
  290. }
  291. // create...
  292. var no_create_edit = (
  293. self.options && (self.options.no_create ||
  294. self.options.no_create_edit)
  295. )
  296. var m2x_create_edit_undef = _.isUndefined(self.view.ir_options['web_m2x_options.create_edit'])
  297. var m2x_create_edit = self.view.ir_options['web_m2x_options.create_edit'] == "True"
  298. if (!no_create_edit && (m2x_create_edit_undef || m2x_create_edit)) {
  299. values.push({
  300. label: _t("Create and Edit..."),
  301. action: function() {
  302. self._search_create_popup("form", undefined, self._create_context(search_val));
  303. },
  304. classname: 'oe_m2o_dropdown_option'
  305. });
  306. }
  307. return values;
  308. })
  309. },
  310. render_value: function()
  311. {
  312. var self = this;
  313. return jQuery.when(this._super.apply(this, arguments))
  314. .then(function()
  315. {
  316. if(self.options.open)
  317. {
  318. self.$el.find('.oe_tag')
  319. .css('cursor', 'pointer')
  320. .click(function(e)
  321. {
  322. var id = parseInt(jQuery(this).attr('data-id'));
  323. self.do_action({
  324. type: 'ir.actions.act_window',
  325. res_model: self.field.relation,
  326. views: [[false, 'form']],
  327. res_id: id,
  328. });
  329. });
  330. }
  331. });
  332. },
  333. });
  334. };