PageRenderTime 62ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/components/bitrix/ui.toolbar/templates/admin/script.js

https://gitlab.com/alexprowars/bitrix
JavaScript | 280 lines | 217 code | 47 blank | 16 comment | 24 complexity | b4b18242742d50815288935c283e704c MD5 | raw file
  1. ;(function () {
  2. 'use strict';
  3. BX.namespace('BX.UI');
  4. BX.UI.ToolbarManager =
  5. {
  6. toolbars: {},
  7. /**
  8. *
  9. * @return {BX.UI.Toolbar}
  10. */
  11. create: function(options)
  12. {
  13. var toolbar = new BX.UI.Toolbar(options);
  14. if (this.get(toolbar.getId()))
  15. {
  16. throw new Error("The toolbar instance with the same 'id' already exists.");
  17. }
  18. this.toolbars[toolbar.getId()] = toolbar;
  19. return toolbar;
  20. },
  21. /**
  22. *
  23. * @return {BX.UI.Toolbar|null}
  24. */
  25. getDefaultToolbar: function()
  26. {
  27. return this.get('default-toolbar');
  28. },
  29. /**
  30. *
  31. * @return {BX.UI.Toolbar|null}
  32. */
  33. get: function(id)
  34. {
  35. return id in this.toolbars ? this.toolbars[id] : null
  36. }
  37. };
  38. BX.UI.Toolbar = function(options)
  39. {
  40. options = BX.type.isPlainObject(options) ? options : {};
  41. this.titleMinWidth = BX.type.isNumber(options.titleMinWidth) ? options.titleMinWidth : 158;
  42. this.titleMaxWidth = BX.type.isNumber(options.titleMaxWidth) ? options.titleMaxWidth : '';
  43. this.filterMinWidth = BX.type.isNumber(options.filterMinWidth) ? options.filterMinWidth : 300;
  44. this.filterMaxWidth = BX.type.isNumber(options.filterMaxWidth) ? options.filterMaxWidth : 748;
  45. this.currentFavoriteId = BX.type.isNumber(options.currentFavoriteId) ? options.currentFavoriteId : 0;
  46. this.initPagetitleStar();
  47. this.id = BX.Type.isStringFilled(options.id) ? options.id : BX.Text.getRandom();
  48. this.toolbarContainer = options.target;
  49. if (!BX.Type.isDomNode(this.toolbarContainer))
  50. {
  51. throw new Error('BX.UI.Toolbar: "target" parameter is required.');
  52. }
  53. this.titleContainer = this.toolbarContainer.querySelector('.ui-toolbar-title-box');
  54. this.filterContainer = this.toolbarContainer.querySelector('.ui-toolbar-filter-box');
  55. this.filterButtons = this.toolbarContainer.querySelector('.ui-toolbar-filter-buttons');
  56. this.rightButtons = this.toolbarContainer.querySelector('.ui-toolbar-right-buttons');
  57. this.afterTitleButtons = this.toolbarContainer.querySelector('.ui-toolbar-after-title-buttons');
  58. if (!this.filterContainer)
  59. {
  60. this.filterMinWidth = 0;
  61. this.filterMaxWidth = 0;
  62. }
  63. this.buttons = Object.create(null);
  64. this.buttonIds = BX.Type.isArray(options.buttonIds) ? options.buttonIds : [];
  65. if (!this.buttonIds.length)
  66. {
  67. return;
  68. }
  69. this.buttonIds.forEach(function(buttonId) {
  70. var button = BX.UI.ButtonManager.createByUniqId(buttonId);
  71. if (button)
  72. {
  73. button.getContainer().originalWidth = button.getContainer().offsetWidth;
  74. if (!button.getIcon() && !BX.Type.isStringFilled(button.getDataSet()['toolbarCollapsedIcon']))
  75. {
  76. if (button.getColor() === BX.UI.ButtonColor.PRIMARY)
  77. {
  78. button.setDataSet({
  79. 'toolbarCollapsedIcon': BX.UI.ButtonIcon.ADD
  80. });
  81. }
  82. else
  83. {
  84. console.warn(
  85. 'BX.UI.Toolbar: the button "' + button.getText() + '" ' +
  86. 'doesn\'t have an icon for collapsed mode. ' +
  87. 'Use the "data-toolbar-collapsed-icon" attribute.'
  88. );
  89. }
  90. }
  91. this.buttons[buttonId] = button;
  92. }
  93. else
  94. {
  95. console.warn('BX.UI.Toolbar: the button "' + buttonId + '" wasn\'t initialized.');
  96. }
  97. }, this);
  98. this.windowWidth = document.body.offsetWidth;
  99. this.reduceItemsWidth();
  100. window.addEventListener('resize', function() {
  101. if (this.isWindowIncreased())
  102. {
  103. this.increaseItemsWidth();
  104. }
  105. else
  106. {
  107. this.reduceItemsWidth();
  108. }
  109. }.bind(this));
  110. };
  111. BX.UI.Toolbar.prototype =
  112. {
  113. /**
  114. *
  115. * @return {Map<string, BX.UI.Button>}
  116. */
  117. getButtons: function()
  118. {
  119. return this.buttons;
  120. },
  121. getButton: function(id)
  122. {
  123. return id in this.buttons ? this.buttons[id] : null;
  124. },
  125. getId: function()
  126. {
  127. return this.id;
  128. },
  129. isWindowIncreased: function()
  130. {
  131. var previousWindowWidth = this.windowWidth;
  132. var currentWindowWidth = document.body.offsetWidth;
  133. this.windowWidth = currentWindowWidth;
  134. return currentWindowWidth > previousWindowWidth;
  135. },
  136. getContainerSize: function()
  137. {
  138. return this.toolbarContainer.offsetWidth;
  139. },
  140. getInnerTotalWidth: function()
  141. {
  142. return this.toolbarContainer.scrollWidth;
  143. },
  144. reduceItemsWidth: function()
  145. {
  146. if (this.getInnerTotalWidth() <= this.getContainerSize())
  147. {
  148. return;
  149. }
  150. var buttons = Object.values(this.getButtons());
  151. for (var i = buttons.length - 1; i >= 0; i--)
  152. {
  153. var button = buttons[i];
  154. if (!button.getIcon() && !BX.Type.isStringFilled(button.getDataSet()['toolbarCollapsedIcon']))
  155. {
  156. continue;
  157. }
  158. if (button.isCollapsed())
  159. {
  160. continue;
  161. }
  162. button.setCollapsed(true);
  163. if (!button.getIcon())
  164. {
  165. button.setIcon(button.getDataSet()['toolbarCollapsedIcon']);
  166. }
  167. if (this.getInnerTotalWidth() <= this.getContainerSize())
  168. {
  169. return;
  170. }
  171. }
  172. },
  173. increaseItemsWidth: function()
  174. {
  175. var buttons = Object.values(this.getButtons());
  176. for (var i = 0; i < buttons.length; i++)
  177. {
  178. var button = buttons[i];
  179. var item = button.getContainer();
  180. if (!button.isCollapsed())
  181. {
  182. continue;
  183. }
  184. var newInnerWidth = (
  185. this.titleMinWidth +
  186. this.filterMinWidth +
  187. (this.afterTitleButtons ? this.afterTitleButtons.offsetWidth : 0) +
  188. (this.filterButtons ? this.filterButtons.offsetWidth : 0) +
  189. (this.rightButtons ? this.rightButtons.offsetWidth : 0) +
  190. (item.originalWidth - item.offsetWidth)
  191. );
  192. if (newInnerWidth > this.getContainerSize())
  193. {
  194. break;
  195. }
  196. button.setCollapsed(false);
  197. if (button.getIcon() === button.getDataSet()['toolbarCollapsedIcon'])
  198. {
  199. button.setIcon(null);
  200. }
  201. }
  202. },
  203. initPagetitleStar: function ()
  204. {
  205. var starContNode = BX('uiToolbarStar');
  206. if (!starContNode)
  207. {
  208. return false;
  209. }
  210. if (this.currentFavoriteId)
  211. {
  212. BX.addClass(starContNode, "ui-toolbar-star-active");
  213. }
  214. BX.bind(starContNode, 'click', function ()
  215. {
  216. BX.adminFav.titleLinkClick(starContNode, this.currentFavoriteId);
  217. if (BX.hasClass(starContNode, 'ui-toolbar-star-active'))
  218. {
  219. BX.removeClass(starContNode, "ui-toolbar-star-active");
  220. }
  221. else
  222. {
  223. BX.addClass(starContNode, "ui-toolbar-star-active");
  224. }
  225. setTimeout(function()
  226. {
  227. BX.removeClass(starContNode, "adm-fav-link-active");
  228. }, 300);
  229. }.bind(this));
  230. return true;
  231. }
  232. };
  233. })();