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.

141 lines
5.8 KiB

  1. (function(){
  2. "use strict";
  3. var instance = openerp;
  4. var QWeb = instance.web.qweb, _t = instance.web._t;
  5. instance.web_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. var readonly_bypass = instance.web_readonly_bypass;
  60. instance.web.BufferedDataSet.include({
  61. init : function() {
  62. this._super.apply(this, arguments);
  63. },
  64. /**
  65. * Creates Overriding
  66. *
  67. * @param {Object} data field values to set on the new record
  68. * @param {Object} options Dictionary that can contain the following keys:
  69. * - readonly_fields: Values from readonly fields that were updated by
  70. * on_changes. Only used by the BufferedDataSet to make the o2m work correctly.
  71. * @returns super {$.Deferred}
  72. */
  73. create : function(data, options) {
  74. var self = this;
  75. var context = instance.web.pyeval.eval('contexts',
  76. self.context.__eval_context);
  77. readonly_bypass.ignore_readonly(data, options, true, context);
  78. return self._super(data,options);
  79. },
  80. /**
  81. * Creates Overriding
  82. *
  83. * @param {Object} data field values to set on the new record
  84. * @param {Object} options Dictionary that can contain the following keys:
  85. * - readonly_fields: Values from readonly fields that were updated by
  86. * on_changes. Only used by the BufferedDataSet to make the o2m work correctly.
  87. * @returns super {$.Deferred}
  88. */
  89. write : function(id, data, options) {
  90. var self = this;
  91. var context = instance.web.pyeval.eval('contexts',
  92. self.context.__eval_context);
  93. readonly_bypass.ignore_readonly(data, options, false, context);
  94. return self._super(id,data,options);
  95. },
  96. });
  97. instance.web.DataSet.include({
  98. /*
  99. BufferedDataSet: case of 'add an item' into a form view
  100. */
  101. init : function() {
  102. this._super.apply(this, arguments);
  103. },
  104. /**
  105. * Creates Overriding
  106. *
  107. * @param {Object} data field values to set on the new record
  108. * @param {Object} options Dictionary that can contain the following keys:
  109. * - readonly_fields: Values from readonly fields that were updated by
  110. * on_changes. Only used by the BufferedDataSet to make the o2m work correctly.
  111. * @returns super {$.Deferred}
  112. */
  113. create : function(data, options) {
  114. var self = this;
  115. readonly_bypass.ignore_readonly(data, options, true, self.context);
  116. return self._super(data,options);
  117. },
  118. /**
  119. * Creates Overriding
  120. *
  121. * @param {Object} data field values to set on the new record
  122. * @param {Object} options Dictionary that can contain the following keys:
  123. * - readonly_fields: Values from readonly fields that were updated by
  124. * on_changes. Only used by the BufferedDataSet to make the o2m work correctly.
  125. * @returns super {$.Deferred}
  126. */
  127. write : function(id, data, options) {
  128. var self = this;
  129. readonly_bypass.ignore_readonly(data, options, false, self.context);
  130. return self._super(id,data,options);
  131. },
  132. });
  133. })();