item.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * DevExtreme (ui/collection/item.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. var $ = require("../../core/renderer");
  11. var Class = require("../../core/class");
  12. var each = require("../../core/utils/iterator").each;
  13. var publicComponentUtils = require("../../core/utils/public_component");
  14. var INVISIBLE_STATE_CLASS = "dx-state-invisible";
  15. var DISABLED_STATE_CLASS = "dx-state-disabled";
  16. var ITEM_CONTENT_PLACEHOLDER_CLASS = "dx-item-content-placeholder";
  17. var forcibleWatcher = function(watchMethod, fn, callback) {
  18. var filteredCallback = function() {
  19. var oldValue;
  20. return function(value) {
  21. if (oldValue !== value) {
  22. callback(value, oldValue);
  23. oldValue = value
  24. }
  25. }
  26. }();
  27. return {
  28. dispose: watchMethod(fn, filteredCallback),
  29. force: function() {
  30. filteredCallback(fn())
  31. }
  32. }
  33. };
  34. var CollectionItem = Class.inherit({
  35. ctor: function($element, options, rawData) {
  36. this._$element = $element;
  37. this._options = options;
  38. this._rawData = rawData;
  39. publicComponentUtils.attachInstanceToElement($element, this, this._dispose);
  40. this._render()
  41. },
  42. _render: function() {
  43. var $placeholder = $("<div>").addClass(ITEM_CONTENT_PLACEHOLDER_CLASS);
  44. this._$element.append($placeholder);
  45. this._watchers = [];
  46. this._renderWatchers()
  47. },
  48. _renderWatchers: function() {
  49. this._startWatcher("disabled", this._renderDisabled.bind(this));
  50. this._startWatcher("visible", this._renderVisible.bind(this))
  51. },
  52. _startWatcher: function(field, render) {
  53. var rawData = this._rawData;
  54. var exprGetter = this._options.fieldGetter(field);
  55. var watcher = forcibleWatcher(this._options.watchMethod(), function() {
  56. return exprGetter(rawData)
  57. }, function(value, oldValue) {
  58. this._dirty = true;
  59. render(value, oldValue)
  60. }.bind(this));
  61. this._watchers.push(watcher)
  62. },
  63. setDataField: function() {
  64. this._dirty = false;
  65. each(this._watchers, function(_, watcher) {
  66. watcher.force()
  67. });
  68. if (this._dirty) {
  69. return true
  70. }
  71. },
  72. _renderDisabled: function(value, oldValue) {
  73. this._$element.toggleClass(DISABLED_STATE_CLASS, !!value);
  74. this._updateOwnerFocus(value)
  75. },
  76. _updateOwnerFocus: function(isDisabled) {
  77. var ownerComponent = this._options.owner;
  78. if (ownerComponent && isDisabled) {
  79. ownerComponent._resetItemFocus(this._$element)
  80. }
  81. },
  82. _renderVisible: function(value, oldValue) {
  83. this._$element.toggleClass(INVISIBLE_STATE_CLASS, void 0 !== value && !value)
  84. },
  85. _dispose: function() {
  86. each(this._watchers, function(_, watcher) {
  87. watcher.dispose()
  88. })
  89. }
  90. });
  91. CollectionItem.getInstance = function($element) {
  92. return publicComponentUtils.getInstanceByElement($element, this)
  93. };
  94. module.exports = CollectionItem;