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.

341 lines
14 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 (typeof this.options.create === "undefined" ||
  87. typeof this.options.create_edit === "undefined") {
  88. create_rights = new instance.web.Model(this.field.relation).call(
  89. "check_access_rights", ["create", false]);
  90. }
  91. $.when(search_result, create_rights).then(function (data, can_create) {
  92. self.can_create = can_create; // for ``.show_error_displayer()``
  93. self.last_search = data;
  94. // possible selections for the m2o
  95. var values = _.map(data, function (x) {
  96. x[1] = x[1].split("\n")[0];
  97. return {
  98. label: _.str.escapeHTML(x[1]),
  99. value: x[1],
  100. name: x[1],
  101. id: x[0],
  102. };
  103. });
  104. // Search result value colors
  105. if (self.colors && self.field_color) {
  106. var value_ids = [];
  107. for (var index in values) {
  108. value_ids.push(values[index].id);
  109. }
  110. // RPC request to get field_color from Objects
  111. Objects.query([self.field_color])
  112. .filter([['id', 'in', value_ids]])
  113. .all().done(function (objects) {
  114. for (var index in objects) {
  115. for (var index_value in values) {
  116. if (values[index_value].id == objects[index].id) {
  117. // Find value in values by comparing ids
  118. var value = values[index_value];
  119. // Find color with field value as key
  120. var color = self.colors[objects[index][self.field_color]] || 'black';
  121. value.label = '<span style="color:'+color+'">'+value.label+'</span>';
  122. break;
  123. }
  124. }
  125. }
  126. def.resolve(values);
  127. });
  128. }
  129. // search more... if more results that max
  130. if (values.length > self.limit || self.search_more) {
  131. values = values.slice(0, self.limit);
  132. values.push({
  133. label: _t("Search More..."),
  134. action: function () {
  135. dataset.name_search(
  136. search_val, self.build_domain(),
  137. 'ilike', false).done(function (data) {
  138. self._search_create_popup("search", data);
  139. });
  140. },
  141. classname: 'oe_m2o_dropdown_option'
  142. });
  143. }
  144. // quick create
  145. var raw_result = _(data.result).map(function (x) {
  146. return x[1];
  147. });
  148. if ((_.isUndefined(self.options.create) && _.isUndefined(self.view.ir_options['web_m2x_options.create']) && can_create) ||
  149. (_.isUndefined(self.options.create) && self.view.ir_options['web_m2x_options.create'] == "True") ||
  150. self.options.create) {
  151. if (search_val.length > 0 &&
  152. !_.include(raw_result, search_val)) {
  153. values.push({
  154. label: _.str.sprintf(
  155. _t('Create "<strong>%s</strong>"'),
  156. $('<span />').text(search_val).html()),
  157. action: function () {
  158. self._quick_create(search_val);
  159. },
  160. classname: 'oe_m2o_dropdown_option'
  161. });
  162. }
  163. }
  164. // create...
  165. if ((_.isUndefined(self.options.create_edit) && _.isUndefined(self.view.ir_options['web_m2x_options.create_edit']) && can_create) ||
  166. (_.isUndefined(self.options.create) && self.view.ir_options['web_m2x_options.create_edit'] == "True") ||
  167. self.options.create_edit) {
  168. values.push({
  169. label: _t("Create and Edit..."),
  170. action: function () {
  171. self._search_create_popup(
  172. "form", undefined,
  173. self._create_context(search_val));
  174. },
  175. classname: 'oe_m2o_dropdown_option'
  176. });
  177. }
  178. // Check if colors specified to wait for RPC
  179. if (!(self.field_color && self.colors)){
  180. def.resolve(values);
  181. }
  182. });
  183. return def;
  184. }
  185. });
  186. instance.web.form.FieldMany2ManyTags.include({
  187. show_error_displayer: function () {
  188. if ((typeof this.options.m2o_dialog === 'undefined' && this.can_create) ||
  189. this.options.m2o_dialog) {
  190. new instance.web.form.M2ODialog(this).open();
  191. }
  192. },
  193. start: function() {
  194. this._super.apply(this, arguments);
  195. return this.get_options();
  196. },
  197. get_options: function() {
  198. var self = this;
  199. if (_.isUndefined(this.view.ir_options_loaded)) {
  200. this.view.ir_options_loaded = $.Deferred();
  201. this.view.ir_options = {};
  202. (new instance.web.Model("ir.config_parameter"))
  203. .query(["key", "value"]).filter([['key', 'in', OPTIONS]])
  204. .all().then(function(records) {
  205. _(records).each(function(record) {
  206. self.view.ir_options[record.key] = record.value;
  207. });
  208. self.view.ir_options_loaded.resolve();
  209. });
  210. }
  211. return this.view.ir_options_loaded;
  212. },
  213. /**
  214. * Call this method to search using a string.
  215. */
  216. get_search_result: function(search_val) {
  217. var self = this;
  218. // add options limit used to change number of selections record
  219. // returned.
  220. if (!_.isUndefined(this.view.ir_options['web_m2x_options.limit'])) {
  221. this.limit = parseInt(this.view.ir_options['web_m2x_options.limit']);
  222. }
  223. if (typeof this.options.limit === 'number') {
  224. this.limit = this.options.limit;
  225. }
  226. var dataset = new instance.web.DataSet(this, this.field.relation, self.build_context());
  227. var blacklist = this.get_search_blacklist();
  228. this.last_query = search_val;
  229. return this.orderer.add(dataset.name_search(
  230. search_val, new instance.web.CompoundDomain(self.build_domain(), [["id", "not in", blacklist]]),
  231. 'ilike', this.limit + 1, self.build_context())).then(function(data) {
  232. self.last_search = data;
  233. // possible selections for the m2o
  234. var values = _.map(data, function(x) {
  235. x[1] = x[1].split("\n")[0];
  236. return {
  237. label: _.str.escapeHTML(x[1]),
  238. value: x[1],
  239. name: x[1],
  240. id: x[0],
  241. };
  242. });
  243. // search more... if more results that max
  244. if (values.length > self.limit) {
  245. values = values.slice(0, self.limit);
  246. values.push({
  247. label: _t("Search More..."),
  248. action: function() {
  249. dataset.name_search(search_val, self.build_domain(), 'ilike', false).done(function(data) {
  250. self._search_create_popup("search", data);
  251. });
  252. },
  253. classname: 'oe_m2o_dropdown_option'
  254. });
  255. }
  256. // quick create
  257. if ((_.isUndefined(self.options.create) && _.isUndefined(self.view.ir_options['web_m2x_options.create'])) ||
  258. (_.isUndefined(self.options.create) && self.view.ir_options['web_m2x_options.create'] == 'True') ||
  259. self.options.create) {
  260. var raw_result = _(data.result).map(function(x) {return x[1];});
  261. if (search_val.length > 0 && !_.include(raw_result, search_val)) {
  262. values.push({
  263. label: _.str.sprintf(_t('Create "<strong>%s</strong>"'),
  264. $('<span />').text(search_val).html()),
  265. action: function() {
  266. self._quick_create(search_val);
  267. },
  268. classname: 'oe_m2o_dropdown_option'
  269. });
  270. }
  271. }
  272. // create...
  273. if ((_.isUndefined(self.options.create_edit === 'undefined') && _.isUndefined(self.view.ir_options['web_m2x_options.create_edit'])) ||
  274. (_.isUndefined(self.options.create) && self.view.ir_options['web_m2x_options.create_edit'] == 'True') ||
  275. self.options.create_edit) {
  276. values.push({
  277. label: _t("Create and Edit..."),
  278. action: function() {
  279. self._search_create_popup("form", undefined, self._create_context(search_val));
  280. },
  281. classname: 'oe_m2o_dropdown_option'
  282. });
  283. }
  284. return values;
  285. })
  286. },
  287. });
  288. };