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.

180 lines
7.3 KiB

  1. odoo.define('web_readonly_bypass', function(require) {
  2. 'use strict';
  3. var data = require('web.data'),
  4. pyeval = require('web.pyeval');
  5. var readonly_bypass = {
  6. /**
  7. * ignore readonly: place options['readonly_fields'] into the data
  8. * if nothing is specified into the context
  9. *
  10. * create mode: remove read-only keys having a 'false' value
  11. *
  12. * @param {Object} data field values to possibly be updated
  13. * @param {Object} options Dictionary that can contain the following keys:
  14. * - readonly_fields: Values from readonly fields to merge into the data object
  15. * @param boolean mode: True case of create, false case of write
  16. * @param {Object} context->readonly_by_pass
  17. */
  18. ignore_readonly: function(data, options, mode, context){
  19. var readonly_by_pass_fields = this.retrieve_readonly_by_pass_fields(
  20. options, context);
  21. if(mode){
  22. $.each( readonly_by_pass_fields, function( key, value ) {
  23. if(value==false){
  24. delete(readonly_by_pass_fields[key]);
  25. }
  26. });
  27. }
  28. data = $.extend(data, readonly_by_pass_fields);
  29. },
  30. /**
  31. * retrieve_readonly_by_pass_fields: retrieve readonly fields to save
  32. * according context.
  33. *
  34. * @param {Object} options Dictionary that can contain the following keys:
  35. * - readonly_fields: all values from readonly fields
  36. * @param {Object} context->readonly_by_pass: Can be true if all
  37. * all readonly fields should be saved or an array of field name to
  38. * save ie: ['readonly_field_1', 'readonly_field_2']
  39. * @returns {Object}: readonly key/value fields to save according context
  40. */
  41. retrieve_readonly_by_pass_fields: function(options, context){
  42. var readonly_by_pass_fields = {};
  43. if (options && 'readonly_fields' in options &&
  44. options['readonly_fields'] && context &&
  45. 'readonly_by_pass' in context && context['readonly_by_pass']){
  46. if (_.isArray(context['readonly_by_pass'])){
  47. $.each( options.readonly_fields, function( key, value ) {
  48. if(_.contains(context['readonly_by_pass'], key)){
  49. readonly_by_pass_fields[key] = value;
  50. }
  51. });
  52. }else{
  53. readonly_by_pass_fields = options.readonly_fields;
  54. }
  55. }
  56. return readonly_by_pass_fields;
  57. },
  58. };
  59. data.BufferedDataSet.include({
  60. init : function() {
  61. this._super.apply(this, arguments);
  62. },
  63. /**
  64. * Create Overriding
  65. *
  66. * @param {Object} data field values to set on the new record
  67. * @param {Object} options Dictionary that can contain the following keys:
  68. * - readonly_fields: Values from readonly fields that were updated by
  69. * on_changes. Only used by the BufferedDataSet to make the o2m work correctly.
  70. * @returns super {$.Deferred}
  71. */
  72. create : function(data, options) {
  73. var self = this;
  74. var context = pyeval.eval('contexts', self.context.get_eval_context());
  75. readonly_bypass.ignore_readonly(data, options, true, context);
  76. return self._super(data,options);
  77. },
  78. /**
  79. * Write Overriding
  80. *
  81. * @param {Object} data field values to set on the new record
  82. * @param {Object} options Dictionary that can contain the following keys:
  83. * - readonly_fields: Values from readonly fields that were updated by
  84. * on_changes. Only used by the BufferedDataSet to make the o2m work correctly.
  85. * @returns super {$.Deferred}
  86. */
  87. write : function(id, data, options) {
  88. var self = this;
  89. var context = pyeval.eval('contexts', self.context.get_eval_context());
  90. readonly_bypass.ignore_readonly(data, options, false, context);
  91. return self._super(id,data,options);
  92. },
  93. });
  94. data.DataSet.include({
  95. /*
  96. BufferedDataSet: case of 'add an item' into a form view
  97. */
  98. init : function() {
  99. this._super.apply(this, arguments);
  100. },
  101. /**
  102. * Creates Overriding
  103. *
  104. * @param {Object} data field values to set on the new record
  105. * @param {Object} options Dictionary that can contain the following keys:
  106. * - readonly_fields: Values from readonly fields that were updated by
  107. * on_changes. Only used by the BufferedDataSet to make the o2m work correctly.
  108. * @returns super {$.Deferred}
  109. */
  110. create : function(data, options) {
  111. var self = this;
  112. readonly_bypass.ignore_readonly(data, options, true, self.context);
  113. return self._super(data,options);
  114. },
  115. /**
  116. * Creates Overriding
  117. *
  118. * @param {Object} data field values to set on the new record
  119. * @param {Object} options Dictionary that can contain the following keys:
  120. * - readonly_fields: Values from readonly fields that were updated by
  121. * on_changes. Only used by the BufferedDataSet to make the o2m work correctly.
  122. * @returns super {$.Deferred}
  123. */
  124. write : function(id, data, options) {
  125. var self = this;
  126. readonly_bypass.ignore_readonly(data, options, false, self.context);
  127. return self._super(id,data,options);
  128. },
  129. });
  130. data.ProxyDataSet.include({
  131. /*
  132. ProxyDataSet: case of 'pop-up'
  133. */
  134. init : function() {
  135. this._super.apply(this, arguments);
  136. },
  137. /**
  138. * Create Overriding
  139. *
  140. * @param {Object} data field values to set on the new record
  141. * @param {Object} options Dictionary that can contain the following keys:
  142. * - readonly_fields: Values from readonly fields that were updated by
  143. * on_changes. Only used by the BufferedDataSet to make the o2m work correctly.
  144. * @returns super {$.Deferred}
  145. */
  146. create : function(data, options) {
  147. var self = this;
  148. var context = pyeval.eval('contexts', self.context.get_eval_context());
  149. readonly_bypass.ignore_readonly(data, options, true, context);
  150. return self._super(data,options);
  151. },
  152. /**
  153. * Write Overriding
  154. *
  155. * @param {Object} data field values to set on the new record
  156. * @param {Object} options Dictionary that can contain the following keys:
  157. * - readonly_fields: Values from readonly fields that were updated by
  158. * on_changes. Only used by the BufferedDataSet to make the o2m work correctly.
  159. * @returns super {$.Deferred}
  160. */
  161. write : function(id, data, options) {
  162. var self = this;
  163. var context = pyeval.eval('contexts', self.context.get_eval_context());
  164. readonly_bypass.ignore_readonly(data, options, false, context);
  165. return self._super(id,data,options);
  166. },
  167. });
  168. return readonly_bypass;
  169. });