Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

156 строки
4.9 KiB

  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'jquery',
  7. 'jquery/ui',
  8. 'jquery/jstree/jquery.jstree'
  9. ], function ($) {
  10. 'use strict';
  11. $.widget('mage.folderTree', {
  12. options: {
  13. root: 'root',
  14. rootName: 'Root',
  15. url: '',
  16. currentPath: ['root'],
  17. tree: {
  18. core: {
  19. themes: {
  20. dots: false
  21. },
  22. // jscs:disable requireCamelCaseOrUpperCaseIdentifiers
  23. check_callback: true
  24. // jscs:enable requireCamelCaseOrUpperCaseIdentifiers
  25. }
  26. }
  27. },
  28. /** @inheritdoc */
  29. _create: function () {
  30. var options = this.options,
  31. treeOptions = $.extend(
  32. true,
  33. {},
  34. options.tree,
  35. {
  36. core: {
  37. data: {
  38. url: options.url,
  39. type: 'POST',
  40. dataType: 'text',
  41. dataFilter: $.proxy(function (data) {
  42. return this._convertData(JSON.parse(data));
  43. }, this),
  44. /**
  45. * @param {HTMLElement} node
  46. * @return {Object}
  47. */
  48. data: function (node) {
  49. return {
  50. node: node.id === 'root' ? null : node.id,
  51. 'form_key': window.FORM_KEY
  52. };
  53. }
  54. }
  55. }
  56. }
  57. );
  58. this.element.jstree(treeOptions)
  59. .on('ready.jstree', $.proxy(this.treeLoaded, this))
  60. .on('load_node.jstree', $.proxy(this._createRootNode, this));
  61. },
  62. /**
  63. * Tree loaded.
  64. */
  65. treeLoaded: function () {
  66. var path = this.options.currentPath,
  67. tree = this.element,
  68. lastExistentFolderEl,
  69. /**
  70. * Recursively open folders specified in path array.
  71. */
  72. recursiveOpen = function () {
  73. var folderEl = $('[data-id="' + path.pop() + '"]');
  74. // if folder doesn't exist, select the last opened folder
  75. if (!folderEl.length) {
  76. tree.jstree('select_node', lastExistentFolderEl);
  77. return;
  78. }
  79. lastExistentFolderEl = folderEl;
  80. if (path.length) {
  81. tree.jstree('open_node', folderEl, recursiveOpen);
  82. } else {
  83. tree.jstree('open_node', folderEl, function () {
  84. tree.jstree('select_node', folderEl);
  85. });
  86. }
  87. };
  88. recursiveOpen();
  89. },
  90. /**
  91. * Create tree root node
  92. *
  93. * @param {jQuery.Event} event
  94. * @param {Object} data
  95. * @private
  96. */
  97. _createRootNode: function (event, data) {
  98. var rootNode, children;
  99. // jscs:disable requireCamelCaseOrUpperCaseIdentifiers
  100. if (data.node.id === '#') {
  101. rootNode = {
  102. id: this.options.root,
  103. text: this.options.rootName,
  104. li_attr: {
  105. 'data-id': this.options.root
  106. }
  107. };
  108. children = data.node.children;
  109. data.instance.element.jstree().create_node(null, rootNode, 'first', function () {
  110. data.instance.element.jstree().move_node(children, rootNode.id);
  111. });
  112. }
  113. // jscs:enable requireCamelCaseOrUpperCaseIdentifiers
  114. },
  115. /**
  116. * @param {*} data
  117. * @return {*}
  118. * @private
  119. */
  120. _convertData: function (data) {
  121. return $.map(data, function (node) {
  122. return {
  123. id: node.id,
  124. text: node.text,
  125. path: node.path,
  126. // jscs:disable requireCamelCaseOrUpperCaseIdentifiers
  127. li_attr: {
  128. 'data-id': node.id
  129. },
  130. // jscs:enable requireCamelCaseOrUpperCaseIdentifiers
  131. children: node.children
  132. };
  133. });
  134. }
  135. });
  136. return $.mage.folderTree;
  137. });