PageRenderTime 63ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/web_client/ea_web-github/addons/web/static/src/js/views.js

https://gitlab.com/deneros/LibrERP
JavaScript | 1370 lines | 1188 code | 42 blank | 140 comment | 185 complexity | 10b554fd016f25eacdaae2eb3e97551f MD5 | raw file
Possible License(s): AGPL-3.0, Apache-2.0, BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. /*---------------------------------------------------------
  2. * OpenERP web library
  3. *---------------------------------------------------------*/
  4. openerp.web.views = function(session) {
  5. var QWeb = session.web.qweb,
  6. _t = session.web._t;
  7. /**
  8. * Registry for all the client actions key: tag value: widget
  9. */
  10. session.web.client_actions = new session.web.Registry();
  11. /**
  12. * Registry for all the main views
  13. */
  14. session.web.views = new session.web.Registry();
  15. session.web.ActionManager = session.web.OldWidget.extend({
  16. init: function(parent) {
  17. this._super(parent);
  18. this.inner_action = null;
  19. this.inner_viewmanager = null;
  20. this.dialog = null;
  21. this.dialog_viewmanager = null;
  22. this.client_widget = null;
  23. },
  24. render: function() {
  25. return '<div id="' + this.element_id + '" style="height: 100%;"></div>';
  26. },
  27. dialog_stop: function () {
  28. if (this.dialog) {
  29. this.dialog_viewmanager.stop();
  30. this.dialog_viewmanager = null;
  31. this.dialog.stop();
  32. this.dialog = null;
  33. }
  34. },
  35. content_stop: function () {
  36. if (this.inner_viewmanager) {
  37. this.inner_viewmanager.stop();
  38. this.inner_viewmanager = null;
  39. }
  40. if (this.client_widget) {
  41. this.client_widget.stop();
  42. this.client_widget = null;
  43. }
  44. },
  45. do_push_state: function(state) {
  46. if (this.widget_parent && this.widget_parent.do_push_state) {
  47. if (this.inner_action) {
  48. state['title'] = this.inner_action.name;
  49. state['model'] = this.inner_action.res_model;
  50. if (this.inner_action.id) {
  51. state['action_id'] = this.inner_action.id;
  52. }
  53. }
  54. this.widget_parent.do_push_state(state);
  55. }
  56. },
  57. do_load_state: function(state, warm) {
  58. var self = this,
  59. action_loaded;
  60. if (state.action_id) {
  61. var run_action = (!this.inner_viewmanager) || this.inner_viewmanager.action.id !== state.action_id;
  62. if (run_action) {
  63. this.null_action();
  64. action_loaded = this.do_action(state.action_id);
  65. }
  66. } else if (state.model && state.id) {
  67. // TODO handle context & domain ?
  68. this.null_action();
  69. var action = {
  70. res_model: state.model,
  71. res_id: state.id,
  72. type: 'ir.actions.act_window',
  73. views: [[false, 'page'], [false, 'form']]
  74. };
  75. action_loaded = this.do_action(action);
  76. } else if (state.sa) {
  77. // load session action
  78. var self = this;
  79. this.null_action();
  80. action_loaded = this.rpc('/web/session/get_session_action', {key: state.sa}).pipe(function(action) {
  81. if (action) {
  82. return self.do_action(action);
  83. }
  84. });
  85. } else if (state.client_action) {
  86. this.null_action();
  87. this.ir_actions_client(state.client_action);
  88. }
  89. $.when(action_loaded || null).then(function() {
  90. if (self.inner_viewmanager) {
  91. self.inner_viewmanager.do_load_state(state, warm);
  92. }
  93. });
  94. },
  95. do_action: function(action, on_close) {
  96. if (_.isNumber(action)) {
  97. var self = this;
  98. return self.rpc("/web/action/load", { action_id: action }, function(result) {
  99. self.do_action(result.result, on_close);
  100. });
  101. }
  102. if (!action.type) {
  103. console.error("No type for action", action);
  104. return;
  105. }
  106. var type = action.type.replace(/\./g,'_');
  107. var popup = action.target === 'new';
  108. action.flags = _.extend({
  109. views_switcher : !popup,
  110. search_view : !popup,
  111. action_buttons : !popup,
  112. sidebar : !popup,
  113. pager : !popup,
  114. display_title : !popup
  115. }, action.flags || {});
  116. if (!(type in this)) {
  117. console.error("Action manager can't handle action of type " + action.type, action);
  118. return;
  119. }
  120. return this[type](action, on_close);
  121. },
  122. null_action: function() {
  123. this.dialog_stop();
  124. },
  125. ir_actions_act_window: function (action, on_close) {
  126. var self = this;
  127. if (_(['base.module.upgrade', 'base.setup.installer'])
  128. .contains(action.res_model)) {
  129. var old_close = on_close;
  130. on_close = function () {
  131. session.webclient.do_reload().then(old_close);
  132. };
  133. }
  134. if (action.target === 'new') {
  135. if (this.dialog == null) {
  136. this.dialog = new session.web.Dialog(this, { width: '80%' });
  137. if(on_close)
  138. this.dialog.on_close.add(on_close);
  139. } else {
  140. this.dialog_viewmanager.stop();
  141. }
  142. this.dialog.dialog_title = action.name;
  143. this.dialog_viewmanager = new session.web.ViewManagerAction(this, action);
  144. this.dialog_viewmanager.appendTo(this.dialog.$element);
  145. this.dialog.open();
  146. } else {
  147. if(action.menu_id) {
  148. return this.widget_parent.do_action(action, function () {
  149. session.webclient.menu.open_menu(action.menu_id);
  150. });
  151. }
  152. this.dialog_stop();
  153. this.inner_action = action;
  154. this.inner_viewmanager = new session.web.ViewManagerAction(this, action);
  155. if (action.flags.without_tab) {
  156. this.inner_viewmanager.appendTo(this.$element);
  157. } else {
  158. tab = this.new_tab(action.name);
  159. this.inner_viewmanager.appendTo(tab);
  160. this._rename_tab_id();
  161. this._activate_tab()
  162. }
  163. }
  164. },
  165. new_tab: function(tab_name) {
  166. this.new_tab_panel(tab_name);
  167. return $('<div id="' + this.element_id + '"/>').appendTo('#oe_app').addClass("ui-tabs-panel ui-widget-content ui-corner-bottom");
  168. },
  169. new_tab_panel: function(tab_name) {
  170. $('#tab_navigator').children().each(
  171. function(){
  172. $(this).removeClass("ui-tabs-selected ui-state-active");
  173. }
  174. );
  175. $('#oe_app').children('div').each(
  176. function(){
  177. $(this).addClass("ui-tabs-hide");
  178. }
  179. );
  180. $('<li><a href="#' + this.element_id + '">' + tab_name + '</a></li>').appendTo('#tab_navigator').addClass("ui-state-default ui-corner-top ui-tabs-selected ui-state-active");
  181. },
  182. _rename_tab_id: function() {
  183. this.element_id = _.uniqueId('tab-'); // TODO This method fix bug (or maybe feature :D ) -- all widgets have the same id -- "#widget-15"
  184. $('#' + this.element_id).addClass("ui-tabs-panel ui-widget-content ui-corner-bottom");
  185. },
  186. _activate_tab: function () {
  187. var tabs = $('#oe_app').tabs({
  188. closable: true,
  189. closableClick: this.check_unsaved
  190. })
  191. tabs.find( ".ui-tabs-nav" ).sortable({
  192. axis: "x",
  193. stop: function() {
  194. tabs.tabs( "refresh" );
  195. }
  196. });
  197. },
  198. check_unsaved: function(event, ui) {
  199. if ($(ui.panel).find('.oe_form_dirty').length > 0) {
  200. if (confirm (_t("There is unsaved data, you will lose all the changes.\nDo you really want to continue?"))) {
  201. return true;
  202. } else {
  203. return false;
  204. };
  205. } else {
  206. return true;
  207. };
  208. },
  209. ir_actions_act_window_close: function (action, on_closed) {
  210. if (!this.dialog && on_closed) {
  211. on_closed();
  212. }
  213. this.dialog_stop();
  214. },
  215. ir_actions_server: function (action, on_closed) {
  216. var self = this;
  217. this.rpc('/web/action/run', {
  218. action_id: action.id,
  219. context: action.context || {}
  220. }).then(function (action) {
  221. self.do_action(action, on_closed)
  222. });
  223. },
  224. ir_actions_client: function (action) {
  225. this.content_stop();
  226. this.dialog_stop();
  227. var ClientWidget = session.web.client_actions.get_object(action.tag);
  228. (this.client_widget = new ClientWidget(this, action.params)).appendTo(this);
  229. },
  230. ir_actions_report_xml: function(action, on_closed) {
  231. var self = this;
  232. $.blockUI();
  233. self.rpc("/web/session/eval_domain_and_context", {
  234. contexts: [action.context],
  235. domains: []
  236. }).then(function(res) {
  237. action = _.clone(action);
  238. action.context = res.context;
  239. self.session.get_file({
  240. url: '/web/report',
  241. data: {action: JSON.stringify(action)},
  242. complete: $.unblockUI,
  243. success: function(){
  244. if (!self.dialog && on_closed) {
  245. on_closed();
  246. }
  247. self.dialog_stop();
  248. },
  249. error: session.webclient.crashmanager.on_rpc_error
  250. })
  251. });
  252. },
  253. ir_actions_act_url: function (action) {
  254. window.open(action.url, action.target === 'self' ? '_self' : '_blank');
  255. },
  256. ir_ui_menu: function (action) {
  257. this.widget_parent.do_action(action);
  258. }
  259. });
  260. session.web.ViewManager = session.web.OldWidget.extend(/** @lends session.web.ViewManager# */{
  261. template: "ViewManager",
  262. /**
  263. * @constructs session.web.ViewManager
  264. * @extends session.web.OldWidget
  265. *
  266. * @param parent
  267. * @param dataset
  268. * @param views
  269. */
  270. init: function(parent, dataset, views, flags) {
  271. this._super(parent);
  272. this.model = dataset ? dataset.model : undefined;
  273. this.dataset = dataset;
  274. this.searchview = null;
  275. this.active_view = null;
  276. this.views_src = _.map(views, function(x) {
  277. if (x instanceof Array) {
  278. var View = session.web.views.get_object(x[1], true);
  279. return {
  280. view_id: x[0],
  281. view_type: x[1],
  282. label: View ? View.prototype.display_name : (void 'nope')
  283. };
  284. } else {
  285. return x;
  286. }
  287. });
  288. this.views = {};
  289. this.flags = flags || {};
  290. this.registry = session.web.views;
  291. this.views_history = [];
  292. },
  293. render: function() {
  294. return session.web.qweb.render(this.template, {
  295. self: this,
  296. prefix: this.element_id,
  297. views: this.views_src});
  298. },
  299. /**
  300. * @returns {jQuery.Deferred} initial view loading promise
  301. */
  302. start: function() {
  303. this._super();
  304. var self = this;
  305. this.$element.find('.oe_vm_switch button').click(function() {
  306. self.on_mode_switch($(this).data('view-type'));
  307. });
  308. this.$element.find('.oe_vm_switch_reload').click(function() {
  309. self.on_mode_reload();
  310. });
  311. var views_ids = {};
  312. _.each(this.views_src, function(view) {
  313. self.views[view.view_type] = $.extend({}, view, {
  314. deferred : $.Deferred(),
  315. controller : null,
  316. options : _.extend({
  317. sidebar_id : self.element_id + '_sidebar_' + view.view_type,
  318. action : self.action,
  319. action_views_ids : views_ids
  320. }, self.flags, self.flags[view.view_type] || {}, view.options || {})
  321. });
  322. views_ids[view.view_type] = view.view_id;
  323. });
  324. if (this.flags.views_switcher === false) {
  325. this.$element.find('.oe_vm_switch').hide();
  326. }
  327. // If no default view defined, switch to the first one in sequence
  328. var default_view = this.flags.default_view || this.views_src[0].view_type;
  329. return this.on_mode_switch(default_view);
  330. },
  331. on_mode_reload: function() {
  332. this.$element.find('.oe_vm_switch button:disabled').first().trigger('click');
  333. },
  334. /**
  335. * Asks the view manager to switch visualization mode.
  336. *
  337. * @param {String} view_type type of view to display
  338. * @param {Boolean} [no_store=false] don't store the view being switched to on the switch stack
  339. * @returns {jQuery.Deferred} new view loading promise
  340. */
  341. on_mode_switch: function(view_type, no_store) {
  342. var self = this,
  343. view = this.views[view_type],
  344. view_promise;
  345. if (view_type && !this.check_unsaved()) {
  346. return view_promise;
  347. }
  348. if(!view)
  349. return $.Deferred().reject();
  350. if (!no_store) {
  351. this.views_history.push(view_type);
  352. }
  353. this.active_view = view_type;
  354. if (!view.controller) {
  355. // Lazy loading of views
  356. var controllerclass = this.registry.get_object(view_type);
  357. var controller = new controllerclass(this, this.dataset, view.view_id, view.options);
  358. if (view.embedded_view) {
  359. controller.set_embedded_view(view.embedded_view);
  360. }
  361. controller.do_switch_view.add_last(this.on_mode_switch);
  362. controller.do_prev_view.add_last(this.on_prev_view);
  363. var container = $("#" + this.element_id + '_view_' + view_type);
  364. view_promise = controller.appendTo(container);
  365. this.views[view_type].controller = controller;
  366. this.views[view_type].deferred.resolve(view_type);
  367. $.when(view_promise).then(function() {
  368. self.on_controller_inited(view_type, controller);
  369. if (self.searchview
  370. && self.flags.auto_search
  371. && view.controller.searchable !== false) {
  372. self.searchview.ready.then(self.searchview.do_search);
  373. }
  374. });
  375. } else if (this.searchview
  376. && self.flags.auto_search
  377. && view.controller.searchable !== false) {
  378. this.searchview.ready.then(this.searchview.do_search);
  379. }
  380. if (this.searchview) {
  381. this.searchview[(view.controller.searchable === false || this.searchview.hidden) ? 'hide' : 'show']();
  382. }
  383. this.$element
  384. .find('.oe_vm_switch button').removeAttr('disabled')
  385. .filter('[data-view-type="' + view_type + '"]')
  386. .attr('disabled', true);
  387. $.when(view_promise).then(function () {
  388. _.each(_.keys(self.views), function(view_name) {
  389. var controller = self.views[view_name].controller;
  390. if (controller) {
  391. if (view_name === view_type) {
  392. controller.do_show();
  393. } else {
  394. controller.do_hide();
  395. }
  396. }
  397. });
  398. self.$element.find('.oe_view_title_text:first').text(
  399. self.display_title());
  400. });
  401. return view_promise;
  402. },
  403. check_unsaved: function() {
  404. dirty = this.$element.find('.oe_form_dirty')
  405. if (dirty.length > 0) {
  406. if (confirm (_t("There is unsaved data, you will lose all the changes.\nDo you really want to continue?"))) {
  407. dirty.removeClass('oe_form_dirty');
  408. return true;
  409. } else {
  410. return false;
  411. };
  412. } else {
  413. return true;
  414. };
  415. },
  416. /**
  417. * Returns to the view preceding the caller view in this manager's
  418. * navigation history (the navigation history is appended to via
  419. * on_mode_switch)
  420. *
  421. * @param {Object} [options]
  422. * @param {Boolean} [options.created=false] resource was created
  423. * @param {String} [options.default=null] view to switch to if no previous view
  424. * @returns {$.Deferred} switching end signal
  425. */
  426. on_prev_view: function (options) {
  427. options = options || {};
  428. var current_view = this.views_history.pop();
  429. var previous_view = this.views_history[this.views_history.length - 1] || options['default'];
  430. if (options.created && current_view === 'form' && previous_view === 'list') {
  431. // APR special case: "If creation mode from list (and only from a list),
  432. // after saving, go to page view (don't come back in list)"
  433. return this.on_mode_switch('page');
  434. } else if (options.created && !previous_view && this.action && this.action.flags.default_view === 'form') {
  435. // APR special case: "If creation from dashboard, we have no previous view
  436. return this.on_mode_switch('page');
  437. }
  438. return this.on_mode_switch(previous_view, true);
  439. },
  440. /**
  441. * Sets up the current viewmanager's search view.
  442. *
  443. * @param {Number|false} view_id the view to use or false for a default one
  444. * @returns {jQuery.Deferred} search view startup deferred
  445. */
  446. setup_search_view: function(view_id, search_defaults) {
  447. var self = this;
  448. if (this.searchview) {
  449. this.searchview.stop();
  450. }
  451. this.searchview = new session.web.SearchView(
  452. this, this.dataset,
  453. view_id, search_defaults, this.flags.search_view === false);
  454. this.searchview.on_search.add(this.do_searchview_search);
  455. return this.searchview.appendTo($("#" + this.element_id + "_search"));
  456. },
  457. do_searchview_search: function(domains, contexts, groupbys) {
  458. var self = this,
  459. controller = this.views[this.active_view].controller,
  460. action_context = this.action.context || {};
  461. this.rpc('/web/session/eval_domain_and_context', {
  462. domains: [this.action.domain || []].concat(domains || []),
  463. contexts: [action_context].concat(contexts || []),
  464. group_by_seq: groupbys || []
  465. }, function (results) {
  466. self.dataset.context = results.context;
  467. self.dataset.domain = results.domain;
  468. var groupby = results.group_by.length
  469. ? results.group_by
  470. : action_context.group_by;
  471. if (_.isString(groupby)) {
  472. groupby = [groupby];
  473. }
  474. controller.do_search(results.domain, results.context, groupby || []);
  475. });
  476. },
  477. /**
  478. * Event launched when a controller has been inited.
  479. *
  480. * @param {String} view_type type of view
  481. * @param {String} view the inited controller
  482. */
  483. on_controller_inited: function(view_type, view) {
  484. },
  485. /**
  486. * Called when one of the view want to execute an action
  487. */
  488. on_action: function(action) {
  489. },
  490. on_create: function() {
  491. },
  492. on_remove: function() {
  493. },
  494. on_edit: function() {
  495. },
  496. /**
  497. * Called by children view after executing an action
  498. */
  499. on_action_executed: function () {
  500. },
  501. display_title: function () {
  502. var view = this.views[this.active_view];
  503. if (view) {
  504. // ick
  505. return view.controller.fields_view.arch.attrs.string;
  506. }
  507. return '';
  508. }
  509. });
  510. session.web.ViewManagerAction = session.web.ViewManager.extend(/** @lends oepnerp.web.ViewManagerAction# */{
  511. template:"ViewManagerAction",
  512. /**
  513. * @constructs session.web.ViewManagerAction
  514. * @extends session.web.ViewManager
  515. *
  516. * @param {session.web.ActionManager} parent parent object/widget
  517. * @param {Object} action descriptor for the action this viewmanager needs to manage its views.
  518. */
  519. init: function(parent, action) {
  520. // dataset initialization will take the session from ``this``, so if we
  521. // do not have it yet (and we don't, because we've not called our own
  522. // ``_super()``) rpc requests will blow up.
  523. var flags = action.flags || {};
  524. if (!('auto_search' in flags)) {
  525. flags.auto_search = action.auto_search !== false;
  526. }
  527. if (action.res_model == 'board.board' && action.view_mode === 'form') {
  528. // Special case for Dashboards
  529. _.extend(flags, {
  530. views_switcher : false,
  531. display_title : false,
  532. search_view : false,
  533. pager : false,
  534. sidebar : false,
  535. action_buttons : false
  536. });
  537. }
  538. this._super(parent, null, action.views, flags);
  539. this.session = parent.session;
  540. this.action = action;
  541. var dataset = new session.web.DataSetSearch(this, action.res_model, action.context, action.domain);
  542. if (action.res_id) {
  543. dataset.ids.push(action.res_id);
  544. dataset.index = 0;
  545. }
  546. this.dataset = dataset;
  547. // setup storage for session-wise menu hiding
  548. if (this.session.hidden_menutips) {
  549. return;
  550. }
  551. this.session.hidden_menutips = {}
  552. },
  553. /**
  554. * Initializes the ViewManagerAction: sets up the searchview (if the
  555. * searchview is enabled in the manager's action flags), calls into the
  556. * parent to initialize the primary view and (if the VMA has a searchview)
  557. * launches an initial search after both views are done rendering.
  558. */
  559. start: function() {
  560. var self = this,
  561. searchview_loaded,
  562. search_defaults = {};
  563. _.each(this.action.context, function (value, key) {
  564. var match = /^search_default_(.*)$/.exec(key);
  565. if (match) {
  566. search_defaults[match[1]] = value;
  567. }
  568. });
  569. // init search view
  570. var searchview_id = this.action['search_view_id'] && this.action['search_view_id'][0];
  571. searchview_loaded = this.setup_search_view(searchview_id || false, search_defaults);
  572. var main_view_loaded = this._super();
  573. var manager_ready = $.when(searchview_loaded, main_view_loaded);
  574. this.$element.find('.oe_debug_view').change(this.on_debug_changed);
  575. if (this.action.help && !this.flags.low_profile) {
  576. var Users = new session.web.DataSet(self, 'res.users'),
  577. $tips = this.$element.find('.oe_view_manager_menu_tips');
  578. $tips.delegate('blockquote button', 'click', function() {
  579. var $this = $(this);
  580. //noinspection FallthroughInSwitchStatementJS
  581. switch ($this.attr('name')) {
  582. case 'disable':
  583. Users.write(self.session.uid, {menu_tips:false});
  584. case 'hide':
  585. $this.closest('blockquote').hide();
  586. self.session.hidden_menutips[self.action.id] = true;
  587. }
  588. });
  589. if (!(self.action.id in self.session.hidden_menutips)) {
  590. Users.read_ids([this.session.uid], ['menu_tips']).then(function(users) {
  591. var user = users[0];
  592. if (!(user && user.id === self.session.uid)) {
  593. return;
  594. }
  595. $tips.find('blockquote').toggle(user.menu_tips);
  596. });
  597. }
  598. }
  599. var $res_logs = this.$element.find('.oe-view-manager-logs:first');
  600. $res_logs.delegate('a.oe-more-logs', 'click', function () {
  601. $res_logs.removeClass('oe-folded');
  602. return false;
  603. }).delegate('a.oe-remove-everything', 'click', function () {
  604. $res_logs.removeClass('oe-has-more').find('ul').empty();
  605. $res_logs.css('display','none');
  606. return false;
  607. });
  608. $res_logs.css('display','none');
  609. return manager_ready;
  610. },
  611. on_debug_changed: function (evt) {
  612. var self = this,
  613. $sel = $(evt.currentTarget),
  614. $option = $sel.find('option:selected'),
  615. val = $sel.val(),
  616. current_view = this.views[this.active_view].controller;
  617. switch (val) {
  618. case 'fvg':
  619. var dialog = new session.web.Dialog(this, { title: _t("Fields View Get"), width: '95%' }).open();
  620. $('<pre>').text(session.web.json_node_to_xml(current_view.fields_view.arch, true)).appendTo(dialog.$element);
  621. break;
  622. case 'perm_read':
  623. var ids = current_view.get_selected_ids();
  624. if (ids.length === 1) {
  625. this.dataset.call('perm_read', [ids]).then(function(result) {
  626. var dialog = new session.web.Dialog(this, {
  627. title: _.str.sprintf(_t("View Log (%s)"), self.dataset.model),
  628. width: 400
  629. }, QWeb.render('ViewManagerDebugViewLog', {
  630. perm : result[0],
  631. format : session.web.format_value
  632. })).open();
  633. });
  634. }
  635. break;
  636. case 'fields':
  637. this.dataset.call_and_eval(
  638. 'fields_get', [false, {}], null, 1).then(function (fields) {
  639. var $root = $('<dl>');
  640. _(fields).each(function (attributes, name) {
  641. $root.append($('<dt>').append($('<h4>').text(name)));
  642. var $attrs = $('<dl>').appendTo(
  643. $('<dd>').appendTo($root));
  644. _(attributes).each(function (def, name) {
  645. if (def instanceof Object) {
  646. def = JSON.stringify(def);
  647. }
  648. $attrs
  649. .append($('<dt>').text(name))
  650. .append($('<dd style="white-space: pre-wrap;">').text(def));
  651. });
  652. });
  653. new session.web.Dialog(self, {
  654. title: _.str.sprintf(_t("Model %s fields"),
  655. self.dataset.model),
  656. width: '95%'}, $root).open();
  657. });
  658. break;
  659. case 'manage_views':
  660. if (current_view.fields_view && current_view.fields_view.arch) {
  661. var view_editor = new session.web.ViewEditor(current_view, current_view.$element, this.dataset, current_view.fields_view.arch);
  662. view_editor.start();
  663. } else {
  664. this.do_warn(_t("Manage Views"),
  665. _t("Could not find current view declaration"));
  666. }
  667. break;
  668. case 'edit_workflow':
  669. return this.do_action({
  670. res_model : 'workflow',
  671. domain : [['osv', '=', this.dataset.model]],
  672. views: [[false, 'list'], [false, 'form'], [false, 'diagram']],
  673. type : 'ir.actions.act_window',
  674. view_type : 'list',
  675. view_mode : 'list'
  676. });
  677. break;
  678. case 'edit':
  679. this.do_edit_resource($option.data('model'), $option.data('id'), { name : $option.text() });
  680. break;
  681. default:
  682. if (val) {
  683. console.log("No debug handler for ", val);
  684. }
  685. }
  686. evt.currentTarget.selectedIndex = 0;
  687. },
  688. do_edit_resource: function(model, id, action) {
  689. var action = _.extend({
  690. res_model : model,
  691. res_id : id,
  692. type : 'ir.actions.act_window',
  693. view_type : 'form',
  694. view_mode : 'form',
  695. views : [[false, 'form']],
  696. target : 'new',
  697. flags : {
  698. action_buttons : true,
  699. form : {
  700. resize_textareas : true
  701. }
  702. }
  703. }, action || {});
  704. this.do_action(action);
  705. },
  706. on_mode_switch: function (view_type, no_store) {
  707. var self = this;
  708. return $.when(this._super(view_type, no_store)).then(function () {
  709. self.shortcut_check(self.views[view_type]);
  710. self.$element.find('.oe-view-manager-logs:first').addClass('oe-folded').removeClass('oe-has-more').css('display','none').find('ul').empty();
  711. var controller = self.views[self.active_view].controller,
  712. fvg = controller.fields_view,
  713. view_id = (fvg && fvg.view_id) || '--';
  714. self.$element.find('.oe_debug_view').html(QWeb.render('ViewManagerDebug', {
  715. view: controller,
  716. view_manager: self
  717. }));
  718. if (!self.action.name && fvg) {
  719. self.$element.find('.oe_view_title_text').text(fvg.arch.attrs.string || fvg.name);
  720. }
  721. var $title = self.$element.find('.oe_view_title_text'),
  722. $search_prefix = $title.find('span.oe_searchable_view');
  723. if (controller.searchable !== false && self.flags.search_view !== false) {
  724. if (!$search_prefix.length) {
  725. $title.prepend('<span class="oe_searchable_view">' + _t("Search: ") + '</span>');
  726. }
  727. } else {
  728. $search_prefix.remove();
  729. }
  730. });
  731. },
  732. do_push_state: function(state) {
  733. if (this.widget_parent && this.widget_parent.do_push_state) {
  734. state["view_type"] = this.active_view;
  735. this.widget_parent.do_push_state(state);
  736. }
  737. },
  738. do_load_state: function(state, warm) {
  739. var self = this,
  740. defs = [];
  741. if (state.view_type && state.view_type !== this.active_view) {
  742. defs.push(
  743. this.views[this.active_view].deferred.pipe(function() {
  744. return self.on_mode_switch(state.view_type, true);
  745. })
  746. );
  747. }
  748. $.when(defs).then(function() {
  749. self.views[self.active_view].controller.do_load_state(state, warm);
  750. });
  751. },
  752. shortcut_check : function(view) {
  753. if (!view) {
  754. return;
  755. }
  756. var self = this;
  757. var grandparent = this.widget_parent && this.widget_parent.widget_parent;
  758. // display shortcuts if on the first view for the action
  759. var $shortcut_toggle = this.$element.find('.oe-shortcut-toggle');
  760. if (!this.action.name ||
  761. !(view.view_type === this.views_src[0].view_type
  762. && view.view_id === this.views_src[0].view_id)) {
  763. $shortcut_toggle.hide();
  764. return;
  765. }
  766. $shortcut_toggle.removeClass('oe-shortcut-remove').show();
  767. if (_(this.session.shortcuts).detect(function (shortcut) {
  768. return shortcut.res_id === self.session.active_id; })) {
  769. $shortcut_toggle.addClass("oe-shortcut-remove");
  770. }
  771. this.shortcut_add_remove();
  772. },
  773. shortcut_add_remove: function() {
  774. var self = this;
  775. var $shortcut_toggle = this.$element.find('.oe-shortcut-toggle');
  776. $shortcut_toggle
  777. .unbind("click")
  778. .click(function() {
  779. if ($shortcut_toggle.hasClass("oe-shortcut-remove")) {
  780. $(self.session.shortcuts.binding).trigger('remove-current');
  781. $shortcut_toggle.removeClass("oe-shortcut-remove");
  782. } else {
  783. $(self.session.shortcuts.binding).trigger('add', {
  784. 'user_id': self.session.uid,
  785. 'res_id': self.session.active_id,
  786. 'resource': 'ir.ui.menu',
  787. 'name': self.action.name
  788. });
  789. $shortcut_toggle.addClass("oe-shortcut-remove");
  790. }
  791. });
  792. },
  793. /**
  794. * Intercept do_action resolution from children views
  795. */
  796. on_action_executed: function () {
  797. return new session.web.DataSet(this, 'res.log')
  798. .call('get', [], this.do_display_log);
  799. },
  800. /**
  801. * @param {Array<Object>} log_records
  802. */
  803. do_display_log: function (log_records) {
  804. var self = this;
  805. var cutoff = 3;
  806. var $logs = this.$element.find('.oe-view-manager-logs:first').addClass('oe-folded').css('display', 'block');
  807. var $logs_list = $logs.find('ul').empty();
  808. $logs.toggleClass('oe-has-more', log_records.length > cutoff);
  809. _(log_records.reverse()).each(function (record) {
  810. var context = {};
  811. if (record.context) {
  812. try { context = py.eval(record.context).toJSON(); }
  813. catch (e) { /* TODO: what do I do now? */ }
  814. }
  815. $(_.str.sprintf('<li><a href="#">%s</a></li>', record.name))
  816. .appendTo($logs_list)
  817. .delegate('a', 'click', function () {
  818. self.do_action({
  819. type: 'ir.actions.act_window',
  820. res_model: record.res_model,
  821. res_id: record.res_id,
  822. // TODO: need to have an evaluated context here somehow
  823. context: context,
  824. views: [[context.view_id || false, 'form']]
  825. });
  826. return false;
  827. });
  828. });
  829. },
  830. display_title: function () {
  831. return this.action.name;
  832. }
  833. });
  834. session.web.Sidebar = session.web.OldWidget.extend({
  835. init: function(parent, element_id) {
  836. this._super(parent, element_id);
  837. this.items = {};
  838. this.sections = {};
  839. },
  840. start: function() {
  841. this._super(this);
  842. var self = this;
  843. this.$element.html(session.web.qweb.render('Sidebar'));
  844. this.$element.find(".toggle-sidebar").click(function(e) {
  845. self.do_toggle();
  846. });
  847. },
  848. add_default_sections: function() {
  849. var self = this,
  850. view = this.widget_parent,
  851. view_manager = view.widget_parent,
  852. action = view_manager.action;
  853. if (this.session.uid === 1) {
  854. this.add_section(_t('Customize'), 'customize');
  855. this.add_items('customize', [{
  856. label: _t("Translate"),
  857. callback: view.on_sidebar_translate,
  858. title: _t("Technical translation")
  859. }]);
  860. }
  861. this.add_section(_t('Other Options'), 'other');
  862. this.add_items('other', [
  863. {
  864. label: _t("Import"),
  865. callback: view.on_sidebar_import
  866. }, {
  867. label: _t("Export"),
  868. callback: view.on_sidebar_export
  869. }
  870. ]);
  871. },
  872. add_toolbar: function(toolbar) {
  873. var self = this;
  874. _.each([['print', _t("Reports")], ['action', _t("Actions")], ['relate', _t("Links")]], function(type) {
  875. var items = toolbar[type[0]];
  876. if (items.length) {
  877. for (var i = 0; i < items.length; i++) {
  878. items[i] = {
  879. label: items[i]['name'],
  880. action: items[i],
  881. classname: 'oe_sidebar_' + type[0]
  882. }
  883. }
  884. self.add_section(type[1], type[0]);
  885. self.add_items(type[0], items);
  886. }
  887. });
  888. },
  889. add_section: function(name, code) {
  890. if(!code) code = _.str.underscored(name);
  891. var $section = this.sections[code];
  892. if(!$section) {
  893. var section_id = _.uniqueId(this.element_id + '_section_' + code + '_');
  894. $section = $(session.web.qweb.render("Sidebar.section", {
  895. section_id: section_id,
  896. name: name,
  897. classname: 'oe_sidebar_' + code
  898. }));
  899. $section.appendTo(this.$element.find('div.sidebar-actions'));
  900. this.sections[code] = $section;
  901. }
  902. return $section;
  903. },
  904. /**
  905. * For each item added to the section:
  906. *
  907. * ``label``
  908. * will be used as the item's name in the sidebar
  909. *
  910. * ``action``
  911. * descriptor for the action which will be executed, ``action`` and
  912. * ``callback`` should be exclusive
  913. *
  914. * ``callback``
  915. * function to call when the item is clicked in the sidebar, called
  916. * with the item descriptor as its first argument (so information
  917. * can be stored as additional keys on the object passed to
  918. * ``add_items``)
  919. *
  920. * ``classname`` (optional)
  921. * ``@class`` set on the sidebar serialization of the item
  922. *
  923. * ``title`` (optional)
  924. * will be set as the item's ``@title`` (tooltip)
  925. *
  926. * @param {String} section_code
  927. * @param {Array<{label, action | callback[, classname][, title]}>} items
  928. */
  929. add_items: function(section_code, items) {
  930. var self = this,
  931. $section = this.add_section(_.str.titleize(section_code.replace('_', ' ')), section_code),
  932. section_id = $section.attr('id');
  933. if (items) {
  934. for (var i = 0; i < items.length; i++) {
  935. items[i].element_id = _.uniqueId(section_id + '_item_');
  936. this.items[items[i].element_id] = items[i];
  937. }
  938. var $items = $(session.web.qweb.render("Sidebar.section.items", {items: items}));
  939. $items.find('a.oe_sidebar_action_a').click(function() {
  940. var item = self.items[$(this).attr('id')];
  941. if (item.callback) {
  942. item.callback.apply(self, [item]);
  943. }
  944. if (item.action) {
  945. self.on_item_action_clicked(item);
  946. }
  947. return false;
  948. });
  949. var $ul = $section.find('ul');
  950. if(!$ul.length) {
  951. $ul = $('<ul/>').appendTo($section);
  952. }
  953. $items.appendTo($ul);
  954. }
  955. },
  956. on_item_action_clicked: function(item) {
  957. var self = this;
  958. self.widget_parent.sidebar_context().then(function (context) {
  959. var ids = self.widget_parent.get_selected_ids();
  960. if (ids.length == 0) {
  961. //TODO: make prettier warning?
  962. $("<div />").text(_t("You must choose at least one record.")).dialog({
  963. title: _t("Warning"),
  964. modal: true
  965. });
  966. return false;
  967. }
  968. var additional_context = _.extend({
  969. active_id: ids[0],
  970. active_ids: ids,
  971. active_model: self.widget_parent.dataset.model
  972. }, context);
  973. self.rpc("/web/action/load", {
  974. action_id: item.action.id,
  975. context: additional_context
  976. }, function(result) {
  977. result.result.context = _.extend(result.result.context || {},
  978. additional_context);
  979. result.result.flags = result.result.flags || {};
  980. result.result.flags.new_window = true;
  981. self.do_action(result.result, function () {
  982. // reload view
  983. self.widget_parent.reload();
  984. });
  985. });
  986. });
  987. },
  988. do_fold: function() {
  989. this.$element.addClass('closed-sidebar').removeClass('open-sidebar');
  990. },
  991. do_unfold: function() {
  992. this.$element.addClass('open-sidebar').removeClass('closed-sidebar');
  993. },
  994. do_toggle: function() {
  995. this.$element.toggleClass('open-sidebar closed-sidebar');
  996. }
  997. });
  998. session.web.TranslateDialog = session.web.Dialog.extend({
  999. dialog_title: {toString: function () { return _t("Translations"); }},
  1000. init: function(view) {
  1001. // TODO fme: should add the language to fields_view_get because between the fields view get
  1002. // and the moment the user opens the translation dialog, the user language could have been changed
  1003. this.view_language = view.session.user_context.lang;
  1004. this['on_button_' + _t("Save")] = this.on_btn_save;
  1005. this['on_button_' + _t("Close")] = this.on_btn_close;
  1006. this._super(view, {
  1007. width: '80%',
  1008. height: '80%'
  1009. });
  1010. this.view = view;
  1011. this.view_type = view.fields_view.type || '';
  1012. this.$fields_form = null;
  1013. this.$view_form = null;
  1014. this.$sidebar_form = null;
  1015. this.translatable_fields_keys = _.map(this.view.translatable_fields || [], function(i) { return i.name });
  1016. this.languages = null;
  1017. this.languages_loaded = $.Deferred();
  1018. (new session.web.DataSetSearch(this, 'res.lang', this.view.dataset.get_context(),
  1019. [['translatable', '=', '1']])).read_slice(['code', 'name'], { sort: 'id' }).then(this.on_languages_loaded);
  1020. },
  1021. start: function() {
  1022. var self = this;
  1023. this._super();
  1024. $.when(this.languages_loaded).then(function() {
  1025. self.$element.html(session.web.qweb.render('TranslateDialog', { widget: self }));
  1026. self.$fields_form = self.$element.find('.oe_translation_form');
  1027. self.$fields_form.find('.oe_trad_field').change(function() {
  1028. $(this).toggleClass('touched', ($(this).val() != $(this).attr('data-value')));
  1029. });
  1030. });
  1031. return this;
  1032. },
  1033. on_languages_loaded: function(langs) {
  1034. this.languages = langs;
  1035. this.languages_loaded.resolve();
  1036. },
  1037. do_load_fields_values: function(callback) {
  1038. var self = this,
  1039. deffered = [];
  1040. this.$fields_form.find('.oe_trad_field').val('').removeClass('touched');
  1041. _.each(self.languages, function(lg) {
  1042. var deff = $.Deferred();
  1043. deffered.push(deff);
  1044. var callback = function(values) {
  1045. _.each(self.translatable_fields_keys, function(f) {
  1046. self.$fields_form.find('.oe_trad_field[name="' + lg.code + '-' + f + '"]').val(values[0][f] || '').attr('data-value', values[0][f] || '');
  1047. });
  1048. deff.resolve();
  1049. };
  1050. if (lg.code === self.view_language) {
  1051. var values = {};
  1052. _.each(self.translatable_fields_keys, function(field) {
  1053. values[field] = self.view.fields[field].get_value();
  1054. });
  1055. callback([values]);
  1056. } else {
  1057. self.rpc('/web/dataset/get', {
  1058. model: self.view.dataset.model,
  1059. ids: [self.view.datarecord.id],
  1060. fields: self.translatable_fields_keys,
  1061. context: self.view.dataset.get_context({
  1062. 'lang': lg.code
  1063. })}, callback);
  1064. }
  1065. });
  1066. $.when.apply(null, deffered).then(callback);
  1067. },
  1068. open: function(field) {
  1069. var self = this,
  1070. sup = this._super;
  1071. $.when(this.languages_loaded).then(function() {
  1072. if (self.view.translatable_fields && self.view.translatable_fields.length) {
  1073. self.do_load_fields_values(function() {
  1074. sup.call(self);
  1075. if (field) {
  1076. var $field_input = self.$element.find('tr[data-field="' + field.name + '"] td:nth-child(2) *:first-child');
  1077. self.$element.scrollTo($field_input);
  1078. $field_input.focus();
  1079. }
  1080. });
  1081. } else {
  1082. sup.call(self);
  1083. }
  1084. });
  1085. },
  1086. on_btn_save: function() {
  1087. var trads = {},
  1088. self = this,
  1089. trads_mutex = new $.Mutex();
  1090. self.$fields_form.find('.oe_trad_field.touched').parents('tr').each(function() {
  1091. $(this).find('.oe_trad_field').each(function() {
  1092. var field = $(this).attr('name').split('-');
  1093. if (!trads[field[0]]) {
  1094. trads[field[0]] = {};
  1095. }
  1096. trads[field[0]][field[1]] = $(this).val();
  1097. });
  1098. });
  1099. _.each(trads, function(data, code) {
  1100. if (code === self.view_language) {
  1101. _.each(data, function(value, field) {
  1102. self.view.fields[field].set_value(value);
  1103. });
  1104. }
  1105. trads_mutex.exec(function() {
  1106. return self.view.dataset.write(self.view.datarecord.id, data, { context : { 'lang': code } });
  1107. });
  1108. });
  1109. this.close();
  1110. },
  1111. on_btn_close: function() {
  1112. this.close();
  1113. }
  1114. });
  1115. session.web.View = session.web.Widget.extend(/** @lends session.web.View# */{
  1116. template: "EmptyComponent",
  1117. // name displayed in view switchers
  1118. display_name: '',
  1119. init: function(parent, dataset, view_id, options) {
  1120. this._super(parent);
  1121. this.dataset = dataset;
  1122. this.view_id = view_id;
  1123. this.set_default_options(options);
  1124. },
  1125. set_default_options: function(options) {
  1126. this.options = options || {};
  1127. _.defaults(this.options, {
  1128. // All possible views options should be defaulted here
  1129. sidebar_id: null,
  1130. sidebar: true,
  1131. action: null,
  1132. action_views_ids: {}
  1133. });
  1134. },
  1135. open_translate_dialog: function(field) {
  1136. if (!this.translate_dialog) {
  1137. this.translate_dialog = new session.web.TranslateDialog(this).start();
  1138. }
  1139. this.translate_dialog.open(field);
  1140. },
  1141. /**
  1142. * Fetches and executes the action identified by ``action_data``.
  1143. *
  1144. * @param {Object} action_data the action descriptor data
  1145. * @param {String} action_data.name the action name, used to uniquely identify the action to find and execute it
  1146. * @param {String} [action_data.special=null] special action handlers (currently: only ``'cancel'``)
  1147. * @param {String} [action_data.type='workflow'] the action type, if present, one of ``'object'``, ``'action'`` or ``'workflow'``
  1148. * @param {Object} [action_data.context=null] additional action context, to add to the current context
  1149. * @param {session.web.DataSet} dataset a dataset object used to communicate with the server
  1150. * @param {Object} [record_id] the identifier of the object on which the action is to be applied
  1151. * @param {Function} on_closed callback to execute when dialog is closed or when the action does not generate any result (no new action)
  1152. */
  1153. do_execute_action: function (action_data, dataset, record_id, on_closed) {
  1154. var self = this;
  1155. var result_handler = function () {
  1156. if (on_closed) { on_closed.apply(null, arguments); }
  1157. if (self.widget_parent && self.widget_parent.on_action_executed) {
  1158. return self.widget_parent.on_action_executed.apply(null, arguments);
  1159. }
  1160. };
  1161. var context = new session.web.CompoundContext(dataset.get_context(), action_data.context || {});
  1162. var handler = function (r) {
  1163. var action = r.result;
  1164. if (action && action.constructor == Object) {
  1165. var ncontext = new session.web.CompoundContext(context);
  1166. if (record_id) {
  1167. ncontext.add({
  1168. active_id: record_id,
  1169. active_ids: [record_id],
  1170. active_model: dataset.model
  1171. });
  1172. }
  1173. ncontext.add(action.context || {});
  1174. return self.rpc('/web/session/eval_domain_and_context', {
  1175. contexts: [ncontext],
  1176. domains: []
  1177. }).pipe(function (results) {
  1178. action.context = results.context;
  1179. /* niv: previously we were overriding once more with action_data.context,
  1180. * I assumed this was not a correct behavior and removed it
  1181. */
  1182. return self.do_action(action, result_handler);
  1183. }, null);
  1184. } else {
  1185. return result_handler();
  1186. }
  1187. };
  1188. if (action_data.special) {
  1189. return handler({result: {"type":"ir.actions.act_window_close"}});
  1190. } else if (action_data.type=="object") {
  1191. var args = [[record_id]], additional_args = [];
  1192. if (action_data.args) {
  1193. try {
  1194. // Warning: quotes and double quotes problem due to json and xml clash
  1195. // Maybe we should force escaping in xml or do a better parse of the args array
  1196. additional_args = JSON.parse(action_data.args.replace(/'/g, '"'));
  1197. args = args.concat(additional_args);
  1198. } catch(e) {
  1199. console.error("Could not JSON.parse arguments", action_data.args);
  1200. }
  1201. }
  1202. args.push(context);
  1203. return dataset.call_button(action_data.name, args, handler);
  1204. } else if (action_data.type=="action") {
  1205. return this.rpc('/web/action/load', {
  1206. action_id: parseInt(action_data.name, 10),
  1207. context: context,
  1208. do_not_eval: true
  1209. }).then(handler);
  1210. } else {
  1211. return dataset.exec_workflow(record_id, action_data.name, handler);
  1212. }
  1213. },
  1214. /**
  1215. * Directly set a view to use instead of calling fields_view_get. This method must
  1216. * be called before start(). When an embedded view is set, underlying implementations
  1217. * of session.web.View must use the provided view instead of any other one.
  1218. *
  1219. * @param embedded_view A view.
  1220. */
  1221. set_embedded_view: funct

Large files files are truncated, but you can click here to view the full file