PageRenderTime 95ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/components/bitrix/main.tree.selector/script.js

https://gitlab.com/alexprowars/bitrix
JavaScript | 641 lines | 518 code | 119 blank | 4 comment | 143 complexity | 88d05ddc8b9694076142a66685006652 MD5 | raw file
  1. function JCTreeSelectControl(arParams)
  2. {
  3. var _this = this;
  4. this.arParams = arParams; // {ONSELECT, MULTIPLE, VALUE, AJAX_PAGE, AJAX_PARAMS}
  5. this.arTreeData = {};
  6. this.multiple = this.arParams.MULTIPLE;
  7. if (null != arParams.VALUE)
  8. this.SetValue(arParams.VALUE);
  9. this.div = null;
  10. this._onkeypress = function(e)
  11. {
  12. if (null == e) e = window.event;
  13. if (null == e) return;
  14. if (e.keyCode == 27)
  15. _this.CloseDialog();
  16. };
  17. // current value and its setter and getter
  18. var current_value = [];
  19. this.getElementsByName = function(tag_name, control_name)
  20. {
  21. var result = [];
  22. var inputs = document.getElementsByTagName(tag_name);
  23. for(var i = 0; i < inputs.length; i++)
  24. {
  25. if(inputs[i].getAttribute("name") == control_name)
  26. result.push(inputs[i]);
  27. }
  28. return result;
  29. };
  30. this.SetValueFromInput = function(input_name)
  31. {
  32. var values = [];
  33. var inp = document.getElementById(input_name);
  34. if(inp)
  35. {
  36. values[values.length] = inp.value;
  37. }
  38. else
  39. {
  40. var inputs = this.getElementsByName('INPUT', input_name);
  41. if(inputs && inputs.length > 0)
  42. {
  43. for(var i = 0; i < inputs.length; i++)
  44. values[values.length] = inputs[i].value;
  45. }
  46. else
  47. {
  48. inputs = this.getElementsByName('INPUT', input_name + '[]');
  49. if(inputs && inputs.length > 0)
  50. {
  51. for(i = 0; i < inputs.length; i++)
  52. values[values.length] = inputs[i].value;
  53. }
  54. }
  55. }
  56. this.SetValue(values);
  57. };
  58. this.SetValue = function(value)
  59. {
  60. if (typeof value == 'string' || typeof value == 'object' && value.constructor == String)
  61. value = value.split(',');
  62. if (typeof value == 'object')
  63. {
  64. current_value = [];
  65. for (var i = 0; i < value.length; i++)
  66. {
  67. var q = parseInt(value[i]);
  68. if (!isNaN(q))
  69. current_value[current_value.length] = q;
  70. }
  71. }
  72. return typeof current_value == 'object';
  73. };
  74. this.GetValue = function(tostring)
  75. {
  76. if (null == tostring) tostring = false;
  77. if (tostring)
  78. {
  79. if (null != current_value)
  80. return current_value.join(',');
  81. }
  82. return current_value;
  83. };
  84. this._control = null;
  85. this._timerId = null;
  86. this._delay = 500;
  87. this._value = '';
  88. this._result = [];
  89. this._ajax_error = '';
  90. this._div = null;
  91. this._search_focus = -1;
  92. this.InitControl = function(control_id)
  93. {
  94. this._control = document.getElementById(control_id);
  95. if (this._control)
  96. {
  97. this._control.value = _this.arParams.START_TEXT;
  98. this._control.value_tmp = this._control.value;
  99. this._control.className = 'bx-search-control-empty';
  100. this._control.onfocus = this.__control_focus;
  101. this._control.onblur = this.__control_blur;
  102. this._control.onkeydown = this.__control_keypress;
  103. }
  104. };
  105. this.Run = function()
  106. {
  107. if (null != _this._timerId)
  108. clearTimeout(_this._timerId);
  109. _this._search_focus = -1;
  110. if (_this._control.value && _this._control.value != _this._control.value_tmp)
  111. {
  112. _this._value = _this._control.value;
  113. var url = _this.arParams.AJAX_PAGE+'?MODE=search&win_id=' + _this.arParams.WIN.id + '&search=' + encodeURIComponent(_this._value);
  114. if (_this.arParams.AJAX_PARAMS)
  115. {
  116. for(var param_name in _this.arParams.AJAX_PARAMS)
  117. url += '&' + param_name + '=' + encodeURIComponent(_this.arParams.AJAX_PARAMS[param_name]);
  118. }
  119. BX.ajax.get(url, _this.SetResult);
  120. }
  121. };
  122. this.SetResult = function(data)
  123. {
  124. var DATA = [];
  125. _this._ajax_error = '';
  126. if (data.length > 0)
  127. {
  128. if(data.substr(0, 1) == '[')
  129. eval('DATA = ' + data);
  130. else
  131. _this._ajax_error = data;
  132. }
  133. _this._result = DATA;
  134. _this.SearchShow();
  135. };
  136. this.SearchShow = function()
  137. {
  138. if (null == _this._div)
  139. {
  140. var _content_div = BX('_f_popup_content');
  141. _content_div.style.position = 'relative';
  142. var pos = BX.pos(_this._control, true);
  143. _this._div = _content_div.insertBefore(document.createElement('DIV'), _content_div.firstChild);
  144. _this._div.className = 'mts-search-result';
  145. _this._div.style.top = pos.bottom + 'px';
  146. _this._div.style.left = '0px';
  147. /*_this._div.style.zIndex = 1110; */
  148. _this._div.style.zIndex = 111000;
  149. jsUtils.addCustomEvent('onTreeSearchClose', _this.__onclose, [], _this);
  150. }
  151. else
  152. {
  153. _this._div.innerHTML = '';
  154. }
  155. if (_this._result.length > 0)
  156. {
  157. for (var i = 0; i < _this._result.length; i++)
  158. {
  159. _this._result[i]._row = _this._div.appendChild(document.createElement('DIV'));
  160. _this._result[i]._row.className = 'mts-search-result-row';
  161. _this._result[i]._row.innerHTML = _this._result[i].NAME;
  162. _this._result[i]._row.onclick = _this.__result_row_click;
  163. _this._result[i]._row.__bx_data = _this._result[i];
  164. }
  165. }
  166. else
  167. {
  168. if(_this._ajax_error.length > 0)
  169. _this._div.innerHTML = '<i>' + _this._ajax_error + '</i>';
  170. else
  171. _this._div.innerHTML = '<i>' + _this.arParams['NO_SEARCH_RESULT_TEXT'] + '</i>';
  172. }
  173. _this._div.style.display = 'block';
  174. };
  175. this._openSection = function(SECTION_ID, bScrollToSection)
  176. {
  177. if (null == bScrollToSection)
  178. bScrollToSection = false;
  179. var obSectionDiv = document.getElementById('mts_section_' + SECTION_ID);
  180. if (null != obSectionDiv)
  181. {
  182. var obParentSection = obSectionDiv.parentNode;
  183. if (null != obParentSection)
  184. {
  185. obParentSection = obParentSection.previousSibling;
  186. if (null != obParentSection && obParentSection.id && obParentSection.id.substr(0, 20) == 'mts_section_')
  187. {
  188. _this._openSection(parseInt(obParentSection.id.substr(20)));
  189. }
  190. }
  191. _this.LoadSection(SECTION_ID, true, bScrollToSection);
  192. }
  193. };
  194. this.__result_row_click = function()
  195. {
  196. _this._openSection(this.__bx_data.SECTION_ID, true);
  197. var obUserRow = document.getElementById('mts_' + this.__bx_data.ID);
  198. if (null != obUserRow)
  199. {
  200. if (obUserRow.className != 'mts-row mts-selected')
  201. {
  202. obUserRow.onclick();
  203. }
  204. }
  205. else
  206. {
  207. if(_this.multiple)
  208. current_selected[current_selected.length] = parseInt(this.__bx_data.ID);
  209. else
  210. current_selected[0] = parseInt(this.__bx_data.ID);
  211. }
  212. };
  213. this.__onclose = function()
  214. {
  215. if (null != _this._div)
  216. _this._div.parentNode.removeChild(_this._div);
  217. if (null != _this._timerId)
  218. clearTimeout(_this._timerId);
  219. jsUtils.removeCustomEvent('onTreeSearchClose', _this.__onclose);
  220. };
  221. this.__control_keypress = function(e)
  222. {
  223. if (null == e)
  224. e = window.event;
  225. // 40 - down, 38 - up, 13 - enter
  226. switch (e.keyCode)
  227. {
  228. case 13: //enter
  229. if (_this._search_focus < 0)
  230. _this.Run();
  231. else
  232. {
  233. _this._control.onblur();
  234. _this._control.blur();
  235. _this._result[_this._search_focus]._row.onclick();
  236. }
  237. break;
  238. case 40: //down
  239. if (_this._result.length > 0 && _this._search_focus < _this._result.length-1)
  240. {
  241. if (_this._search_focus >= 0)
  242. _this._result[_this._search_focus]._row.className = 'mts-search-result-row';
  243. _this._search_focus++;
  244. _this._result[_this._search_focus]._row.className = 'mts-search-result-row mts-search-result-row-selected';
  245. }
  246. break;
  247. case 38: //up
  248. if (_this._result.length > 0 && _this._search_focus > -1)
  249. {
  250. _this._result[_this._search_focus]._row.className = 'mts-search-result-row';
  251. _this._search_focus--;
  252. if (_this._search_focus >= 0)
  253. _this._result[_this._search_focus]._row.className = 'mts-search-result-row mts-search-result-row-selected';
  254. }
  255. break;
  256. default:
  257. if (null != _this._timerId)
  258. clearTimeout(_this._timerId);
  259. _this._timerId = setTimeout(_this.Run, _this._delay);
  260. break;
  261. }
  262. };
  263. this.__control_focus = function()
  264. {
  265. if (this.value == this.value_tmp)
  266. {
  267. this.value = '';
  268. this.className = '';
  269. }
  270. if (null != this._div)
  271. this._div.style.display = 'block';
  272. };
  273. this.__control_blur = function()
  274. {
  275. if (_this.value == '')
  276. {
  277. _this.value = _this.value_tmp;
  278. _this.className = 'bx-search-control-empty';
  279. }
  280. if (null != _this._div)
  281. {
  282. setTimeout(function() {
  283. _this._div.style.display = 'none';
  284. }, 300);
  285. }
  286. };
  287. this.OnSelect = function()
  288. {
  289. if (null != this.arParams.ONSELECT)
  290. {
  291. var value = this.GetValue();
  292. if (this.arParams.GET_FULL_INFO)
  293. {
  294. var new_value = [];
  295. for (var i = 0; i < value.length; i++)
  296. {
  297. new_value[new_value.length] = this.arTreeData[value[i]];
  298. }
  299. value = new_value;
  300. this.arParams.ONSELECT(value);
  301. }
  302. else
  303. {
  304. this.arParams.ONSELECT(value);
  305. }
  306. }
  307. };
  308. this.Show = function(arParams)
  309. {
  310. if(null != this.div)
  311. return;
  312. var _this = this;
  313. if (null == arParams) arParams = {};
  314. if (null == arParams.id) arParams.id = 'tree_selector_select_control';
  315. if (null == arParams.className) arParams.className = '';
  316. this.arParams.WIN = arParams;
  317. CHttpRequest.Action = function(result) {_this._ShowWindow(result)};
  318. var url = this.arParams.AJAX_PAGE+'?win_id=' +this.arParams.WIN.id;
  319. if (this.arParams.AJAX_PARAMS)
  320. {
  321. for(var param_name in this.arParams.AJAX_PARAMS)
  322. url += '&' + param_name + '=' + encodeURIComponent(this.arParams.AJAX_PARAMS[param_name]);
  323. }
  324. if(this.arParams.INPUT_NAME)
  325. this.SetValueFromInput(this.arParams.INPUT_NAME);
  326. var value = this.GetValue(true);
  327. if ((this.multiple ? value.length : value) > 0)
  328. url += '&value=' + value;
  329. if (this.multiple)
  330. url += '&multiple=Y';
  331. ShowWaitWindow();
  332. CHttpRequest.Send(url);
  333. };
  334. this._ShowWindow = function(result)
  335. {
  336. CloseWaitWindow();
  337. var _this = this;
  338. this.div = document.body.appendChild(document.createElement("DIV"));
  339. this.div.id = this.arParams.WIN.id;
  340. this.div.className = "settings-float-form" + (this.arParams.WIN && this.arParams.WIN.className ? ' ' + this.arParams.WIN.className : '');
  341. this.div.style.position = 'absolute';
  342. /*this.div.style.zIndex = '1100'; */
  343. this.div.style.zIndex = '110000';
  344. this.div.innerHTML = result;
  345. this.div.__object = this;
  346. var obSize = BX.GetWindowSize();
  347. var left = parseInt(obSize.scrollLeft + obSize.innerWidth/2 - this.div.offsetWidth/2);
  348. var top = parseInt(obSize.scrollTop + obSize.innerHeight/2 - this.div.offsetHeight/2);
  349. jsFloatDiv.Show(this.div, left, top);
  350. jsUtils.onCustomEvent('onTreeSearchShow', {div: this.div});
  351. jsUtils.onCustomEvent('onTreeSearchShow', {div: this.div});
  352. BX.bind(document, "keypress", this._onkeypress);
  353. };
  354. this.CloseDialog = function()
  355. {
  356. BX.unbind(document, "keypress", this._onkeypress);
  357. jsUtils.onCustomEvent('onTreeSearchClose', {div: this.div});
  358. this._div = null;
  359. jsFloatDiv.Close(this.div);
  360. this.div.parentNode.removeChild(this.div);
  361. this.div = null;
  362. };
  363. this.LoadSection = function(SECTION_ID, bShowOnly, bScrollToSection)
  364. {
  365. if (null == bShowOnly) bShowOnly = false;
  366. if (null == bScrollToSection) bScrollToSection = false;
  367. SECTION_ID = parseInt(SECTION_ID);
  368. var obSection = document.getElementById('mts_section_' + SECTION_ID);
  369. if (null == obSection.BX_LOADED)
  370. {
  371. var url = this.arParams.AJAX_PAGE+'?MODE=section&win_id=' + this.arParams.WIN.id + '&SECTION_ID=' + SECTION_ID;
  372. if (this.arParams.AJAX_PARAMS)
  373. {
  374. for(var param_name in this.arParams.AJAX_PARAMS)
  375. url += '&' + param_name + '=' + encodeURIComponent(this.arParams.AJAX_PARAMS[param_name]);
  376. }
  377. if (bScrollToSection)
  378. {
  379. BX.ajax.get(url, function(data){
  380. _this.ShowSection(data);
  381. document.getElementById('mts_search_layout').scrollTop = document.getElementById('mts_section_' + SECTION_ID).offsetTop - 40;
  382. });
  383. }
  384. else
  385. {
  386. BX.ajax.get(url, this.ShowSection);
  387. }
  388. }
  389. else if (bScrollToSection)
  390. {
  391. document.getElementById('mts_search_layout').scrollTop = document.getElementById('mts_section_' + SECTION_ID).offsetTop - 40;
  392. }
  393. var obChildren = document.getElementById('bx_children_' + SECTION_ID);
  394. if (bShowOnly || obChildren.style.display == 'none')
  395. {
  396. obSection.firstChild.className = obSection.firstChild.className.replace('mts-closed', 'mts-opened');
  397. obChildren.style.display = 'block';
  398. }
  399. else
  400. {
  401. obSection.firstChild.className = obSection.firstChild.className.replace('mts-opened', 'mts-closed');
  402. obChildren.style.display = 'none';
  403. }
  404. };
  405. this.ElementSet = function()
  406. {
  407. if (!!this.arParams.SET_ALWAYS || current_selected.length > 0)
  408. {
  409. this.SetValue(current_selected);
  410. this.OnSelect();
  411. }
  412. this.CloseDialog();
  413. };
  414. this.ElementSelect = function()
  415. {
  416. if(_this.multiple)
  417. {
  418. var bFound = false;
  419. for (var i = 0; i < current_selected.length; i++)
  420. {
  421. if (current_selected[i] == this.BX_ID)
  422. {
  423. bFound = true;
  424. break;
  425. }
  426. }
  427. if (bFound)
  428. {
  429. this.className = 'mts-row';
  430. current_selected = current_selected.slice(0, i).concat(current_selected.slice(i + 1));
  431. this.firstChild.checked = false;
  432. }
  433. else
  434. {
  435. current_selected[current_selected.length] = this.BX_ID;
  436. this.className = 'mts-row mts-selected';
  437. this.firstChild.checked = true;
  438. }
  439. }
  440. else
  441. {
  442. for (i = 0; i < current_selected.length; i++)
  443. {
  444. var row = document.getElementById('mts_' + current_selected[i]);
  445. if(row)
  446. {
  447. row.className = 'mts-row';
  448. row.firstChild.checked = false;
  449. }
  450. }
  451. current_selected = [this.BX_ID];
  452. this.className = 'mts-row mts-selected';
  453. this.firstChild.checked = true;
  454. }
  455. };
  456. this.ShowSection = function (data)
  457. {
  458. var DATA = [];
  459. if (data.length > 0)
  460. eval('DATA = ' + data);
  461. var SECTION_ID = DATA.SECTION_ID;
  462. var arElements = DATA.arElements;
  463. var obSection = document.getElementById('mts_section_' + SECTION_ID);
  464. if (!obSection.BX_LOADED)
  465. {
  466. obSection.BX_LOADED = true;
  467. var obSectionDiv = document.getElementById('mts_elements_' + SECTION_ID);
  468. if (obSectionDiv)
  469. {
  470. obSectionDiv.innerHTML = '';
  471. for (var i = 0; i < arElements.length; i++)
  472. {
  473. _this.arTreeData[arElements[i].ID] = {
  474. ID: arElements[i].ID,
  475. NAME: arElements[i].NAME
  476. };
  477. var obUserRow = document.createElement('DIV');
  478. obUserRow.id = 'mts_' + arElements[i].ID;
  479. obUserRow.className = 'mts-row';
  480. obUserRow.BX_ID = arElements[i].ID;
  481. if (_this.multiple)
  482. {
  483. var obCheckbox = document.createElement('INPUT');
  484. obCheckbox.type = 'checkbox';
  485. obCheckbox.id = 'mts_check_' + arElements[i].ID;
  486. obCheckbox.defaultChecked = false;
  487. for (var j = 0; j < current_selected.length; j++)
  488. {
  489. if (obUserRow.BX_ID == current_selected[j])
  490. {
  491. obCheckbox.defaultChecked = true;
  492. obUserRow.className += ' mts-selected';
  493. break;
  494. }
  495. }
  496. }
  497. else
  498. {
  499. for (j = 0; j < current_selected.length; j++)
  500. {
  501. if (obUserRow.BX_ID == current_selected[j])
  502. {
  503. obUserRow.className += ' mts-selected';
  504. break;
  505. }
  506. }
  507. obUserRow.ondblclick = function(){ _this.ElementSet() };
  508. }
  509. obUserRow.onclick = _this.ElementSelect;
  510. obUserRow.innerHTML = arElements[i].CONTENT;
  511. if (_this.multiple)
  512. {
  513. obUserRow.insertBefore(obCheckbox, obUserRow.firstChild);
  514. }
  515. obSectionDiv.appendChild(obUserRow);
  516. }
  517. var obClearer = obSectionDiv.appendChild(document.createElement('DIV'));
  518. obClearer.style.clear = 'both';
  519. }
  520. }
  521. }
  522. }