FieldDecorationView.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /**
  2. * DO NOT EDIT THIS FILE.
  3. * See the following change record for more information,
  4. * https://www.drupal.org/node/2815083
  5. * @preserve
  6. **/
  7. (function ($, Backbone, Drupal) {
  8. Drupal.quickedit.FieldDecorationView = Backbone.View.extend({
  9. _widthAttributeIsEmpty: null,
  10. events: {
  11. 'mouseenter.quickedit': 'onMouseEnter',
  12. 'mouseleave.quickedit': 'onMouseLeave',
  13. click: 'onClick',
  14. 'tabIn.quickedit': 'onMouseEnter',
  15. 'tabOut.quickedit': 'onMouseLeave'
  16. },
  17. initialize: function initialize(options) {
  18. this.editorView = options.editorView;
  19. this.listenTo(this.model, 'change:state', this.stateChange);
  20. this.listenTo(this.model, 'change:isChanged change:inTempStore', this.renderChanged);
  21. },
  22. remove: function remove() {
  23. this.setElement();
  24. Backbone.View.prototype.remove.call(this);
  25. },
  26. stateChange: function stateChange(model, state) {
  27. var from = model.previous('state');
  28. var to = state;
  29. switch (to) {
  30. case 'inactive':
  31. this.undecorate();
  32. break;
  33. case 'candidate':
  34. this.decorate();
  35. if (from !== 'inactive') {
  36. this.stopHighlight();
  37. if (from !== 'highlighted') {
  38. this.model.set('isChanged', false);
  39. this.stopEdit();
  40. }
  41. }
  42. this._unpad();
  43. break;
  44. case 'highlighted':
  45. this.startHighlight();
  46. break;
  47. case 'activating':
  48. this.prepareEdit();
  49. break;
  50. case 'active':
  51. if (from !== 'activating') {
  52. this.prepareEdit();
  53. }
  54. if (this.editorView.getQuickEditUISettings().padding) {
  55. this._pad();
  56. }
  57. break;
  58. case 'changed':
  59. this.model.set('isChanged', true);
  60. break;
  61. case 'saving':
  62. break;
  63. case 'saved':
  64. break;
  65. case 'invalid':
  66. break;
  67. }
  68. },
  69. renderChanged: function renderChanged() {
  70. this.$el.toggleClass('quickedit-changed', this.model.get('isChanged') || this.model.get('inTempStore'));
  71. },
  72. onMouseEnter: function onMouseEnter(event) {
  73. var that = this;
  74. that.model.set('state', 'highlighted');
  75. event.stopPropagation();
  76. },
  77. onMouseLeave: function onMouseLeave(event) {
  78. var that = this;
  79. that.model.set('state', 'candidate', { reason: 'mouseleave' });
  80. event.stopPropagation();
  81. },
  82. onClick: function onClick(event) {
  83. this.model.set('state', 'activating');
  84. event.preventDefault();
  85. event.stopPropagation();
  86. },
  87. decorate: function decorate() {
  88. this.$el.addClass('quickedit-candidate quickedit-editable');
  89. },
  90. undecorate: function undecorate() {
  91. this.$el.removeClass('quickedit-candidate quickedit-editable quickedit-highlighted quickedit-editing');
  92. },
  93. startHighlight: function startHighlight() {
  94. var that = this;
  95. that.$el.addClass('quickedit-highlighted');
  96. },
  97. stopHighlight: function stopHighlight() {
  98. this.$el.removeClass('quickedit-highlighted');
  99. },
  100. prepareEdit: function prepareEdit() {
  101. this.$el.addClass('quickedit-editing');
  102. if (this.editorView.getQuickEditUISettings().popup) {
  103. this.$el.addClass('quickedit-editor-is-popup');
  104. }
  105. },
  106. stopEdit: function stopEdit() {
  107. this.$el.removeClass('quickedit-highlighted quickedit-editing');
  108. if (this.editorView.getQuickEditUISettings().popup) {
  109. this.$el.removeClass('quickedit-editor-is-popup');
  110. }
  111. $('.quickedit-candidate').addClass('quickedit-editable');
  112. },
  113. _pad: function _pad() {
  114. if (this.$el.data('quickedit-padded')) {
  115. return;
  116. }
  117. var self = this;
  118. if (this.$el[0].style.width === '') {
  119. this._widthAttributeIsEmpty = true;
  120. this.$el.addClass('quickedit-animate-disable-width').css('width', this.$el.width());
  121. }
  122. var posProp = this._getPositionProperties(this.$el);
  123. setTimeout(function () {
  124. self.$el.removeClass('quickedit-animate-disable-width');
  125. self.$el.css({
  126. position: 'relative',
  127. top: posProp.top - 5 + 'px',
  128. left: posProp.left - 5 + 'px',
  129. 'padding-top': posProp['padding-top'] + 5 + 'px',
  130. 'padding-left': posProp['padding-left'] + 5 + 'px',
  131. 'padding-right': posProp['padding-right'] + 5 + 'px',
  132. 'padding-bottom': posProp['padding-bottom'] + 5 + 'px',
  133. 'margin-bottom': posProp['margin-bottom'] - 10 + 'px'
  134. }).data('quickedit-padded', true);
  135. }, 0);
  136. },
  137. _unpad: function _unpad() {
  138. if (!this.$el.data('quickedit-padded')) {
  139. return;
  140. }
  141. var self = this;
  142. if (this._widthAttributeIsEmpty) {
  143. this.$el.addClass('quickedit-animate-disable-width').css('width', '');
  144. }
  145. var posProp = this._getPositionProperties(this.$el);
  146. setTimeout(function () {
  147. self.$el.removeClass('quickedit-animate-disable-width');
  148. self.$el.css({
  149. position: 'relative',
  150. top: posProp.top + 5 + 'px',
  151. left: posProp.left + 5 + 'px',
  152. 'padding-top': posProp['padding-top'] - 5 + 'px',
  153. 'padding-left': posProp['padding-left'] - 5 + 'px',
  154. 'padding-right': posProp['padding-right'] - 5 + 'px',
  155. 'padding-bottom': posProp['padding-bottom'] - 5 + 'px',
  156. 'margin-bottom': posProp['margin-bottom'] + 10 + 'px'
  157. });
  158. }, 0);
  159. this.$el.removeData('quickedit-padded');
  160. },
  161. _getPositionProperties: function _getPositionProperties($e) {
  162. var p = void 0;
  163. var r = {};
  164. var props = ['top', 'left', 'bottom', 'right', 'padding-top', 'padding-left', 'padding-right', 'padding-bottom', 'margin-bottom'];
  165. var propCount = props.length;
  166. for (var i = 0; i < propCount; i++) {
  167. p = props[i];
  168. r[p] = parseInt(this._replaceBlankPosition($e.css(p)), 10);
  169. }
  170. return r;
  171. },
  172. _replaceBlankPosition: function _replaceBlankPosition(pos) {
  173. if (pos === 'auto' || !pos) {
  174. pos = '0px';
  175. }
  176. return pos;
  177. }
  178. });
  179. })(jQuery, Backbone, Drupal);