form_tree_select.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. QUnit.test("form elements check", assert => {
  2. $('#forms').append('<hr><h1>form elements check 1</h1><form id="form1"></form>')
  3. var el = $('#form1')
  4. new App.ControllerForm({
  5. el: el,
  6. model: {
  7. "configure_attributes": [
  8. {
  9. "name": "tree_select",
  10. "display": "tree_select",
  11. "tag": "tree_select",
  12. "null": true,
  13. "translate": true,
  14. "options": [
  15. {
  16. "value": "aa",
  17. "name": "yes",
  18. "children": [
  19. {
  20. "value": "aa::aaa",
  21. "name": "yes1",
  22. },
  23. {
  24. "value": "aa::aab",
  25. "name": "yes2",
  26. },
  27. {
  28. "value": "aa::aac",
  29. "name": "yes3",
  30. },
  31. ]
  32. },
  33. {
  34. "value": "bb",
  35. "name": "bb (comment)",
  36. "children": [
  37. {
  38. "value": "bb::bba",
  39. "name": "yes11",
  40. },
  41. {
  42. "value": "bb::bbb",
  43. "name": "yes22",
  44. },
  45. {
  46. "value": "bb::bbc",
  47. "name": "yes33",
  48. },
  49. ]
  50. },
  51. ],
  52. }
  53. ]
  54. },
  55. autofocus: true
  56. });
  57. assert.equal(el.find('[name="tree_select"]').val(), '', 'check tree_select value');
  58. assert.equal(el.find('[name="tree_select"]').closest('.searchableSelect').find('.js-input').val(), '', 'check tree_select .js-input value');
  59. var params = App.ControllerForm.params(el)
  60. var test_params = {
  61. tree_select: ''
  62. }
  63. assert.deepEqual(params, test_params, 'form param check')
  64. $('#forms').append('<hr><h1>form elements check 2</h1><form id="form2"></form>')
  65. var el = $('#form2')
  66. new App.ControllerForm({
  67. el: el,
  68. model: {
  69. "configure_attributes": [
  70. {
  71. "name": "tree_select",
  72. "display": "tree_select",
  73. "tag": "tree_select",
  74. "null": true,
  75. "translate": true,
  76. "value": "aa",
  77. "options": [
  78. {
  79. "value": "aa",
  80. "name": "yes",
  81. "children": [
  82. {
  83. "value": "aa::aaa",
  84. "name": "yes1",
  85. },
  86. {
  87. "value": "aa::aab",
  88. "name": "yes2",
  89. },
  90. {
  91. "value": "aa::aac",
  92. "name": "yes3",
  93. },
  94. ]
  95. },
  96. {
  97. "value": "bb",
  98. "name": "bb (comment)",
  99. "children": [
  100. {
  101. "value": "bb::bba",
  102. "name": "yes11",
  103. },
  104. {
  105. "value": "bb::bbb",
  106. "name": "yes22",
  107. },
  108. {
  109. "value": "bb::bbc",
  110. "name": "yes33",
  111. },
  112. ]
  113. },
  114. ],
  115. }
  116. ]
  117. },
  118. autofocus: true
  119. });
  120. assert.equal(el.find('[name="tree_select"]').val(), 'aa', 'check tree_select value');
  121. assert.equal(el.find('[name="tree_select"]').closest('.searchableSelect').find('.js-input').val(), 'yes', 'check tree_select .js-input value');
  122. var params = App.ControllerForm.params(el)
  123. var test_params = {
  124. tree_select: 'aa'
  125. }
  126. assert.deepEqual(params, test_params, 'form param check')
  127. $('#forms').append('<hr><h1>form elements check 3</h1><form id="form3"></form>')
  128. var el = $('#form3')
  129. new App.ControllerForm({
  130. el: el,
  131. model: {
  132. "configure_attributes": [
  133. {
  134. "name": "tree_select",
  135. "display": "tree_select",
  136. "tag": "tree_select",
  137. "null": true,
  138. "translate": true,
  139. "value": "aa::aab",
  140. "options": [
  141. {
  142. "value": "aa",
  143. "name": "yes",
  144. "children": [
  145. {
  146. "value": "aa::aaa",
  147. "name": "yes1",
  148. },
  149. {
  150. "value": "aa::aab",
  151. "name": "yes2",
  152. },
  153. {
  154. "value": "aa::aac",
  155. "name": "yes3",
  156. },
  157. ]
  158. },
  159. {
  160. "value": "bb",
  161. "name": "bb (comment)",
  162. "children": [
  163. {
  164. "value": "bb::bba",
  165. "name": "yes11",
  166. },
  167. {
  168. "value": "bb::bbb",
  169. "name": "yes22",
  170. },
  171. {
  172. "value": "bb::bbc",
  173. "name": "yes33",
  174. },
  175. ]
  176. },
  177. ],
  178. }
  179. ]
  180. },
  181. autofocus: true
  182. });
  183. assert.equal(el.find('[name="tree_select"]').val(), 'aa::aab', 'check tree_select value');
  184. assert.equal(el.find('[name="tree_select"]').closest('.searchableSelect').find('.js-input').val(), 'yes2', 'check tree_select .js-input value');
  185. assert.equal(el.find('[name="tree_select"]').closest('.searchableSelect').find('.js-input').attr('title'), 'yes › yes2', 'check tree_select .js-input tooltip');
  186. var params = App.ControllerForm.params(el)
  187. var test_params = {
  188. tree_select: 'aa::aab'
  189. }
  190. assert.deepEqual(params, test_params, 'form param check')
  191. $('#forms').append('<hr><h1>form elements check 4</h1><form id="form4"></form>')
  192. var el = $('#form4')
  193. new App.ControllerForm({
  194. el: el,
  195. model: {
  196. "configure_attributes": [
  197. {
  198. "name": "tree_select_search",
  199. "display": "tree_select_search",
  200. "tag": "tree_select_search",
  201. "null": true,
  202. "translate": true,
  203. "multiple": true,
  204. "value": ['aa::aab', 'bb', 'aa::aac::33'],
  205. "options": [
  206. {
  207. "value": "aa",
  208. "name": "yes",
  209. "children": [
  210. {
  211. "value": "aa::aaa",
  212. "name": "yes1",
  213. },
  214. {
  215. "value": "aa::aab",
  216. "name": "yes2",
  217. },
  218. {
  219. "value": "aa::aac",
  220. "name": "yes3",
  221. "children": [
  222. {
  223. "value": "aa::aaa::11",
  224. "name": "11",
  225. },
  226. {
  227. "value": "aa::aa1::22",
  228. "name": "22",
  229. },
  230. {
  231. "value": "aa::aac::33",
  232. "name": "33",
  233. },
  234. ]
  235. },
  236. ]
  237. },
  238. {
  239. "value": "bb",
  240. "name": "bb (comment)",
  241. "children": [
  242. {
  243. "value": "bb::bba",
  244. "name": "yes11",
  245. },
  246. {
  247. "value": "bb::bbb",
  248. "name": "yes22",
  249. },
  250. {
  251. "value": "bb::bbc",
  252. "name": "yes33",
  253. },
  254. ]
  255. },
  256. ],
  257. }
  258. ]
  259. },
  260. autofocus: true
  261. });
  262. var params = App.ControllerForm.params(el)
  263. var test_params = {
  264. tree_select_search: ['aa::aab', 'bb', 'aa::aac::33'],
  265. tree_select_search_completion: ""
  266. }
  267. assert.deepEqual(params, test_params, 'form param check')
  268. $('#forms').append('<hr><h1>form elements check / tree_select multiple / 3 selected</h1><form id="form6"></form>')
  269. var el = $('#form6')
  270. new App.ControllerForm({
  271. el: el,
  272. model: {
  273. "configure_attributes": [
  274. {
  275. "name": "tree_select",
  276. "display": "tree_select",
  277. "tag": "tree_select",
  278. "null": true,
  279. "multiple": true,
  280. "translate": true,
  281. "value": ['aa::aab', 'bb', 'aa::aac::33'],
  282. "options": [
  283. {
  284. "value": "aa",
  285. "name": "yes",
  286. "children": [
  287. {
  288. "value": "aa::aaa",
  289. "name": "yes1",
  290. },
  291. {
  292. "value": "aa::aab",
  293. "name": "yes2",
  294. },
  295. {
  296. "value": "aa::aac",
  297. "name": "yes3",
  298. "children": [
  299. {
  300. "value": "aa::aaa::11",
  301. "name": "11",
  302. },
  303. {
  304. "value": "aa::aa1::22",
  305. "name": "22",
  306. },
  307. {
  308. "value": "aa::aac::33",
  309. "name": "33",
  310. },
  311. ]
  312. },
  313. ]
  314. },
  315. {
  316. "value": "bb",
  317. "name": "bb (comment)",
  318. "children": [
  319. {
  320. "value": "bb::bba",
  321. "name": "yes11",
  322. },
  323. {
  324. "value": "bb::bbb",
  325. "name": "yes22",
  326. },
  327. {
  328. "value": "bb::bbc",
  329. "name": "yes33",
  330. },
  331. ]
  332. },
  333. ],
  334. }
  335. ]
  336. },
  337. autofocus: true
  338. });
  339. var params = App.ControllerForm.params(el)
  340. var test_params = {
  341. tree_select: ['aa::aab', 'bb', 'aa::aac::33'],
  342. tree_select_completion: "",
  343. }
  344. assert.deepEqual(params, test_params, 'form param check')
  345. $('#forms').append('<hr><h1>form elements check / tree_select multiple / 1 selected</h1><form id="form7"></form>')
  346. var el = $('#form7')
  347. new App.ControllerForm({
  348. el: el,
  349. model: {
  350. "configure_attributes": [
  351. {
  352. "name": "tree_select",
  353. "display": "tree_select",
  354. "tag": "tree_select",
  355. "null": true,
  356. "multiple": true,
  357. "translate": true,
  358. "value": ['aa::aab'],
  359. "options": [
  360. {
  361. "value": "aa",
  362. "name": "yes",
  363. "children": [
  364. {
  365. "value": "aa::aaa",
  366. "name": "yes1",
  367. },
  368. {
  369. "value": "aa::aab",
  370. "name": "yes2",
  371. },
  372. {
  373. "value": "aa::aac",
  374. "name": "yes3",
  375. "children": [
  376. {
  377. "value": "aa::aaa::11",
  378. "name": "11",
  379. },
  380. {
  381. "value": "aa::aa1::22",
  382. "name": "22",
  383. },
  384. {
  385. "value": "aa::aac::33",
  386. "name": "33",
  387. },
  388. ]
  389. },
  390. ]
  391. },
  392. {
  393. "value": "bb",
  394. "name": "bb (comment)",
  395. "children": [
  396. {
  397. "value": "bb::bba",
  398. "name": "yes11",
  399. },
  400. {
  401. "value": "bb::bbb",
  402. "name": "yes22",
  403. },
  404. {
  405. "value": "bb::bbc",
  406. "name": "yes33",
  407. },
  408. ]
  409. },
  410. ],
  411. }
  412. ]
  413. },
  414. autofocus: true
  415. });
  416. var params = App.ControllerForm.params(el)
  417. var test_params = {
  418. tree_select: ['aa::aab'],
  419. tree_select_completion: "",
  420. }
  421. assert.deepEqual(params, test_params, 'form param check')
  422. });
  423. QUnit.test("ui elements check", assert => {
  424. attribute = {
  425. "name": "tree_select_search",
  426. "display": "tree_select_search",
  427. "tag": "tree_select_search",
  428. "null": true,
  429. "translate": true,
  430. "value": ['bb::bba', 'bb::bbb'],
  431. "multiple": true,
  432. "options": [
  433. {
  434. "value": "aa",
  435. "name": "yes",
  436. "children": [
  437. {
  438. "value": "aa::aaa",
  439. "name": "yes1",
  440. },
  441. {
  442. "value": "aa::aab",
  443. "name": "yes2",
  444. },
  445. ]
  446. },
  447. {
  448. "value": "bb",
  449. "name": "bb (comment)",
  450. "children": [
  451. {
  452. "value": "bb::bba",
  453. "name": "yes11",
  454. },
  455. {
  456. "value": "bb::bbb",
  457. "name": "yes22",
  458. },
  459. ]
  460. },
  461. ],
  462. };
  463. options = [
  464. {
  465. "value": "aa",
  466. "name": "yes",
  467. "children": [
  468. {
  469. "value": "aa::aaa",
  470. "name": "yes1",
  471. },
  472. {
  473. "value": "aa::aab",
  474. "name": "yes2",
  475. },
  476. ]
  477. },
  478. {
  479. "value": "bb",
  480. "name": "bb (comment)",
  481. "children": [
  482. {
  483. "value": "bb::bba",
  484. "name": "yes11",
  485. },
  486. {
  487. "value": "bb::bbb",
  488. "name": "yes22",
  489. },
  490. ]
  491. }
  492. ]
  493. element = App.UiElement.tree_select_search.render(attribute)
  494. assert.deepEqual(attribute.options, options, 'options tree_select_search')
  495. attribute.name = 'tree_select'
  496. attribute.display = 'tree_select'
  497. attribute.tag = 'tree_select'
  498. element = App.UiElement.tree_select.render(attribute)
  499. assert.deepEqual(attribute.options, options, 'options tree_select')
  500. });
  501. QUnit.test("searchable_select submenu and option list check", assert => {
  502. var done = assert.async()
  503. $('#forms').append('<hr><h1>form elements check 5</h1><form id="form5"></form>')
  504. var el = $('#form5')
  505. new App.ControllerForm({
  506. el: el,
  507. model: {
  508. "configure_attributes": [
  509. {
  510. "name": "tree_select",
  511. "display": "tree_select",
  512. "tag": "tree_select",
  513. "null": true,
  514. "translate": true,
  515. "value": "bb",
  516. "options": [
  517. {
  518. "value": "a\\a",
  519. "name": "a\\a",
  520. "children": [
  521. {
  522. "value": "a\\a::aaa",
  523. "name": "aaa",
  524. },
  525. {
  526. "value": "a\\a::aab",
  527. "name": "aab",
  528. },
  529. {
  530. "value": "a\\a::aac",
  531. "name": "aac",
  532. },
  533. ]
  534. },
  535. {
  536. "value": "bb",
  537. "name": "bb",
  538. "children": [
  539. {
  540. "value": "bb::bba",
  541. "name": "bba",
  542. },
  543. {
  544. "value": "bb::bbb",
  545. "name": "bbb",
  546. },
  547. {
  548. "value": "bb::bbc",
  549. "name": "bbc",
  550. },
  551. ]
  552. },
  553. ],
  554. }
  555. ]
  556. },
  557. autofocus: true
  558. });
  559. el.find("[name=\"tree_select\"].js-shadow + .js-input").trigger('click')
  560. el.find(".searchableSelect .js-optionsList [data-value=\"a\\\\a\"] .searchableSelect-option-arrow").mouseenter().trigger('click')
  561. el.find(".searchableSelect .js-optionsSubmenu [data-value=\"a\\\\a::aab\"] .searchableSelect-option-text").mouseenter().trigger('click')
  562. el.find("[name=\"tree_select\"].js-shadow + .js-input").trigger('click')
  563. var params = App.ControllerForm.params(el)
  564. var test_params = {
  565. tree_select: 'a\\a::aab'
  566. }
  567. var optionsSubmenu = el.find(".searchableSelect [data-parent-value=\"a\\\\a\"].js-optionsSubmenu")
  568. var optionsList = el.find(".searchableSelect .js-optionsList")
  569. setTimeout( () => {
  570. assert.deepEqual(params, test_params, 'form param check')
  571. assert.equal(optionsSubmenu.is('[hidden]'), false, 'options submenu menu not hidden')
  572. assert.equal(optionsList.is('[hidden]'), true, 'options list is hidden')
  573. done()
  574. }, 300)
  575. });
  576. QUnit.test("[Core Workflow] Remove option does not work with tree select node that has sub-options #4407", assert => {
  577. $('#forms').append('<hr><h1>Remove option does not work with tree select node that has sub-options #4407 form8</h1><form id="form8"></form>')
  578. var el = $('#form8')
  579. attribute = {
  580. "name": "ts4407",
  581. "display": "ts4407",
  582. "tag": "tree_select",
  583. "null": true,
  584. "nulloption": true,
  585. "translate": true,
  586. "filter": ["aa", "bb::aaa", "dd::aaa::bbb"],
  587. "options": [
  588. {
  589. "value": "aa",
  590. "name": "aa yes",
  591. "children": [
  592. {
  593. "value": "aa::aaa",
  594. "name": "aa yes1",
  595. },
  596. {
  597. "value": "aa::aab",
  598. "name": "aa yes2",
  599. },
  600. ]
  601. },
  602. {
  603. "value": "bb",
  604. "name": "bb yes",
  605. "children": [
  606. {
  607. "value": "bb::aaa",
  608. "name": "bb yes1",
  609. },
  610. {
  611. "value": "bb::aab",
  612. "name": "bb yes2",
  613. },
  614. ]
  615. },
  616. {
  617. "value": "cc",
  618. "name": "cc yes",
  619. "children": [
  620. {
  621. "value": "cc::aaa",
  622. "name": "cc yes1",
  623. },
  624. {
  625. "value": "cc::aab",
  626. "name": "cc yes2",
  627. },
  628. ]
  629. },
  630. {
  631. "value": "dd",
  632. "name": "dd yes",
  633. "children": [
  634. {
  635. "value": "dd::aaa",
  636. "name": "dd yes1",
  637. "children": [
  638. {
  639. "value": "dd::aaa::bbb",
  640. "name": "dd yes2",
  641. },
  642. ]
  643. },
  644. ]
  645. }
  646. ],
  647. }
  648. element = App.UiElement.tree_select.render(attribute)
  649. // children are removed
  650. assert.equal(false, element.find("[data-value='aa::aaa']").length > 0)
  651. // one child removed, one child allowed
  652. assert.equal(true, element.find("[data-value='bb::aaa'] span.searchableSelect-option-text:not(.is-inactive)").length > 0)
  653. assert.equal(false, element.find("[data-value='bb::aab']").length > 0)
  654. assert.equal(true, element.find("[data-value='bb'] span.searchableSelect-option-text.is-inactive").length > 0)
  655. // parent and childs removed
  656. assert.equal(false, element.find("[data-value='cc']").length > 0)
  657. assert.equal(false, element.find("[data-value='cc::aaa']").length > 0)
  658. assert.equal(false, element.find("[data-value='cc::aab']").length > 0)
  659. // level 3 child allowed
  660. assert.equal(true, element.find("[data-value='dd'] span.searchableSelect-option-text.is-inactive").length > 0)
  661. assert.equal(true, element.find("[data-value='dd::aaa'] span.searchableSelect-option-text.is-inactive").length > 0)
  662. assert.equal(true, element.find("[data-value='dd::aaa::bbb'] span.searchableSelect-option-text:not(.is-inactive)").length > 0)
  663. attribute.value = 'aa'
  664. element = App.UiElement.tree_select.render(attribute)
  665. assert.equal('aa yes', element.find(".js-input").val())
  666. attribute.value = 'aa::aaa'
  667. element = App.UiElement.tree_select.render(attribute)
  668. assert.equal('-', element.find(".js-input").val())
  669. attribute.tag = 'multi_tree_select'
  670. attribute.multiple = true
  671. attribute.value = ['aa']
  672. element = App.UiElement.tree_select.render(attribute)
  673. assert.equal(true, element.find(".token[data-value='aa']").length > 0)
  674. attribute.value = ['aa::aaa']
  675. element = App.UiElement.tree_select.render(attribute)
  676. assert.equal(false, element.find(".token[data-value='aa::aaa']").length > 0)
  677. el.append(element)
  678. });
  679. QUnit.test("Escaping of values works just fine", assert => {
  680. $('#forms').append('<hr><h1>Escaping of values works just fine form9</h1><form id="form9"></form>')
  681. var el = $('#form9')
  682. attribute = {
  683. "name": "multi_tree_select",
  684. "display": "multi_tree_select",
  685. "tag": "multi_tree_select",
  686. "null": true,
  687. "nulloption": true,
  688. "translate": true,
  689. "multiple": true,
  690. "options": [
  691. {
  692. "value": "aa",
  693. "name": "aa yes",
  694. "children": [
  695. {
  696. "value": 'aa::aaa "test"',
  697. "name": "aa yes with quote",
  698. },
  699. {
  700. "value": "aa::aab",
  701. "name": "aa yes2",
  702. },
  703. ]
  704. },
  705. ],
  706. }
  707. attribute.value = ['aa::aaa "test"']
  708. element = App.UiElement.tree_select.render(attribute)
  709. escaped_selector = jQuery.escapeSelector('aa::aaa "test"')
  710. assert.equal(true, element.find(".token[data-value=" + escaped_selector + "]").length > 0)
  711. element.find('.js-remove').trigger('click')
  712. assert.equal(false, element.find(".token[data-value=" + escaped_selector + "]").length > 0)
  713. el.append(element)
  714. });