form_searchable_select.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // searchable_select
  2. QUnit.test( "searchable_select check", assert => {
  3. $('#forms').append('<hr><h1>searchable_select check</h1><form id="form1"></form>')
  4. var el = $('#form1')
  5. var defaults = {
  6. searchable_select2: 'bbb',
  7. searchable_select4: 'ccc',
  8. }
  9. var options = {
  10. 'aaa': 'aaa display',
  11. 'bbb': 'bbb display',
  12. 'ccc': 'ccc display',
  13. }
  14. var options_4_tree = [
  15. { value: 'aaa', name: 'aaa display' },
  16. { value: 'bbb', name: 'bbb display' },
  17. { value: 'ccc', name: 'ccc display', children: [
  18. { value: 'ccc::aaa', name: 'aaa display' },
  19. { value: 'ccc::bbb', name: 'bbb display' },
  20. ] },
  21. ]
  22. new App.ControllerForm({
  23. el: el,
  24. model: {
  25. configure_attributes: [
  26. {
  27. name: 'searchable_select1',
  28. display: 'SearchableSelect1',
  29. tag: 'searchable_select',
  30. options: options,
  31. null: true,
  32. default: defaults['searchable_select1']
  33. },
  34. {
  35. name: 'searchable_select2',
  36. display: 'SearchableSelect2',
  37. tag: 'searchable_select',
  38. options: options,
  39. null: false,
  40. default: defaults['searchable_select2']
  41. },
  42. {
  43. name: 'searchable_select3',
  44. display: 'SearchableSelect3',
  45. tag: 'searchable_select',
  46. options: options,
  47. default: defaults['searchable_select3'],
  48. null: true,
  49. unknown: true
  50. },
  51. {
  52. name: 'searchable_select4',
  53. display: 'SearchableSelect4',
  54. tag: 'searchable_select',
  55. options: options_4_tree,
  56. default: defaults['searchable_select4'],
  57. null: true,
  58. unknown: true
  59. },
  60. ]
  61. },
  62. autofocus: true
  63. })
  64. var params = App.ControllerForm.params(el)
  65. var test_params = {
  66. searchable_select1: '',
  67. searchable_select2: 'bbb',
  68. searchable_select3: '',
  69. searchable_select4: 'ccc',
  70. }
  71. assert.deepEqual(params, test_params, 'form param check')
  72. // change selection
  73. $('[name="searchable_select1"].js-shadow + .js-input').trigger('focus').val('').trigger('input')
  74. var $element = $('[name="searchable_select1"]').closest('.searchableSelect').find('.js-optionsList')
  75. var entries = $element.find('li:not(.is-hidden)').length
  76. assert.equal(entries, 3, 'dropdown count')
  77. $('[name="searchable_select1"].js-shadow + .js-input').trigger('focus').val('ccc display').trigger('input')
  78. var entries = $element.find('li:not(.is-hidden)').length
  79. assert.equal(entries, 1, 'dropdown count')
  80. $element.find('li:not(.is-hidden)').first().trigger('click')
  81. params = App.ControllerForm.params(el)
  82. test_params = {
  83. searchable_select1: 'ccc',
  84. searchable_select2: 'bbb',
  85. searchable_select3: '',
  86. searchable_select4: 'ccc',
  87. }
  88. assert.deepEqual(params, test_params, 'form param check')
  89. $('[name="searchable_select2"].js-shadow + .js-input').trigger('focus').val('').trigger('input')
  90. var $element = $('[name="searchable_select2"]').closest('.searchableSelect').find('.js-optionsList')
  91. var entries = $element.find('li:not(.is-hidden)').length
  92. assert.equal(entries, 3, 'dropdown count')
  93. $('[name="searchable_select2"].js-shadow + .js-input').trigger('focus').val('ccc display').trigger('input')
  94. var entries = $element.find('li:not(.is-hidden)').length
  95. assert.equal(entries, 1, 'dropdown count')
  96. $element.find('li:not(.is-hidden)').first().trigger('click')
  97. params = App.ControllerForm.params(el)
  98. test_params = {
  99. searchable_select1: 'ccc',
  100. searchable_select2: 'ccc',
  101. searchable_select3: '',
  102. searchable_select4: 'ccc',
  103. }
  104. assert.deepEqual(params, test_params, 'form param check')
  105. $('[name="searchable_select3"].js-shadow + .js-input').trigger('focus').val('').trigger('input')
  106. var $element = $('[name="searchable_select3"]').closest('.searchableSelect').find('.js-optionsList')
  107. var entries = $element.find('li:not(.is-hidden)').length
  108. assert.equal(entries, 3, 'dropdown count')
  109. $('[name="searchable_select3"].js-shadow + .js-input').trigger('focus').val('ccc display').trigger('input')
  110. var entries = $element.find('li:not(.is-hidden)').length
  111. assert.equal(entries, 1, 'dropdown count')
  112. $('[name="searchable_select3"].js-shadow + .js-input').trigger('focus').val('unknown value').trigger('input')
  113. var entries = $element.find('li:not(.is-hidden)').length
  114. assert.equal(entries, 3, 'dropdown count')
  115. var entries = $element.find('li.is-active').length
  116. assert.equal(entries, 0, 'active count')
  117. var e = $.Event('keydown')
  118. e.which = 13 //enter
  119. e.keyCode = 13
  120. $('[name="searchable_select3"].js-shadow + .js-input').trigger(e)
  121. params = App.ControllerForm.params(el)
  122. test_params = {
  123. searchable_select1: 'ccc',
  124. searchable_select2: 'ccc',
  125. searchable_select3: 'unknown value',
  126. searchable_select4: 'ccc',
  127. }
  128. assert.deepEqual(params, test_params, 'form param check')
  129. $('#forms').append('<hr><h1>searchable_select check for .js-input field values</h1><form id="form2"></form>')
  130. var el = $('#form2')
  131. var defaults = {
  132. searchable_select1: 'ccc::aaa',
  133. searchable_select2: 'ccc::ccc',
  134. }
  135. var options = [
  136. { value: 'aaa', name: 'aaa display' },
  137. { value: 'bbb', name: 'bbb display' },
  138. { value: 'ccc', name: 'ccc display', children: [
  139. { value: 'ccc::aaa', name: 'aaa display L2' },
  140. { value: 'ccc::bbb', name: 'bbb display L2' },
  141. { value: 'ccc::ccc', name: 'ccc display L2' },
  142. ] },
  143. ]
  144. new App.ControllerForm({
  145. el: el,
  146. model: {
  147. configure_attributes: [
  148. {
  149. name: 'searchable_select1',
  150. display: 'SearchableSelect1',
  151. tag: 'searchable_select',
  152. options: options,
  153. default: defaults['searchable_select1'],
  154. null: true,
  155. },
  156. {
  157. name: 'searchable_select2',
  158. display: 'SearchableSelect2',
  159. tag: 'searchable_select',
  160. options: options,
  161. default: defaults['searchable_select2'],
  162. null: true,
  163. },
  164. ]
  165. },
  166. })
  167. var params = App.ControllerForm.params(el)
  168. var test_params = {
  169. searchable_select1: 'ccc::aaa',
  170. searchable_select2: 'ccc::ccc',
  171. }
  172. assert.deepEqual(params, test_params, 'form param check')
  173. assert.equal(el.find('[name="searchable_select1"].js-shadow + .js-input').val(), 'aaa display L2', 'verify shown input')
  174. assert.equal(el.find('[name="searchable_select1"].js-shadow + .js-input').attr('title'), 'ccc display › aaa display L2', 'verify provided tooltip')
  175. assert.equal(el.find('[name="searchable_select2"].js-shadow + .js-input').val(), 'ccc display L2', 'verify shown input')
  176. assert.equal(el.find('[name="searchable_select2"].js-shadow + .js-input').attr('title'), 'ccc display › ccc display L2', 'verify provided tooltip')
  177. });
  178. QUnit.test("searchable_select submenu and option list check", assert => {
  179. var done = assert.async()
  180. $('#forms').append('<hr><h1>searchable_select check for special charaters values</h1><form id="form3"></form>')
  181. var el = $('#form3')
  182. var defaults = {
  183. searchable_select1: 'aaa',
  184. }
  185. var options = [
  186. { value: 'aaa', name: 'aaa display' },
  187. { value: 'bbb', name: 'bbb display' },
  188. { value: 'c\\cc', name: 'ccc display', children: [
  189. { value: 'c\\cc::aaa', name: 'aaa display L2' },
  190. { value: 'c\\cc::bbb', name: 'bbb display L2' },
  191. { value: 'c\\cc::ccc', name: 'ccc display L2' },
  192. ] },
  193. ]
  194. new App.ControllerForm({
  195. el: el,
  196. model: {
  197. configure_attributes: [
  198. {
  199. name: 'searchable_select1',
  200. display: 'SearchableSelect1',
  201. tag: 'searchable_select',
  202. options: options,
  203. default: defaults['searchable_select1'],
  204. null: true,
  205. },
  206. {
  207. name: 'searchable_select2',
  208. display: 'SearchableSelect2',
  209. tag: 'searchable_select',
  210. options: options,
  211. default: defaults['searchable_select1'],
  212. null: true,
  213. },
  214. ]
  215. },
  216. })
  217. el.find("[name=\"searchable_select2\"].js-shadow + .js-input").trigger('click')
  218. el.find("div[data-attribute-name=searchable_select2] .js-optionsList [data-value=\"c\\\\cc\"] .searchableSelect-option-text").mouseenter().trigger('click')
  219. el.find("[name=\"searchable_select1\"].js-shadow + .js-input").trigger('click')
  220. el.find("div[data-attribute-name=searchable_select1] .js-optionsList [data-value=\"c\\\\cc\"] .searchableSelect-option-arrow").mouseenter().trigger('click')
  221. el.find("div[data-attribute-name=searchable_select1] .js-optionsSubmenu [data-value=\"c\\\\cc::bbb\"] .searchableSelect-option-text").mouseenter().trigger('click')
  222. el.find("[name=\"searchable_select1\"].js-shadow + .js-input").trigger('click')
  223. var params = App.ControllerForm.params(el)
  224. var test_params = {
  225. searchable_select1: 'c\\cc::bbb',
  226. searchable_select2: 'c\\cc',
  227. }
  228. var optionsSubmenu = el.find("div[data-attribute-name=searchable_select1] .searchableSelect .js-optionsSubmenu")
  229. var optionsList = el.find("div[data-attribute-name=searchable_select1] .searchableSelect .js-optionsList")
  230. setTimeout( () => {
  231. assert.deepEqual(params, test_params, 'form param check')
  232. assert.equal(optionsSubmenu.is('[hidden]'), false, 'options submenu menu not hidden')
  233. assert.equal(optionsList.is('[hidden]'), true, 'options list is hidden')
  234. done()
  235. }, 300)
  236. });
  237. QUnit.test("searchable_select current selection indicator", assert => {
  238. $('#forms').append('<hr><h1>searchable_select current selection indicator</h1><form id="form4"></form>')
  239. var el = $('#form4')
  240. var defaults = {
  241. searchable_select5: 'bbb',
  242. searchable_select6: ['bbb'],
  243. }
  244. var options = {
  245. 'aaa': 'aaa display',
  246. 'bbb': 'bbb display',
  247. 'ccc': 'ccc display',
  248. }
  249. new App.ControllerForm({
  250. el: el,
  251. model: {
  252. configure_attributes: [
  253. {
  254. name: 'searchable_select5',
  255. display: 'SearchableSelect5',
  256. tag: 'searchable_select',
  257. options: options,
  258. null: true,
  259. default: defaults['searchable_select5']
  260. },
  261. {
  262. name: 'searchable_select6',
  263. display: 'SearchableSelect6',
  264. tag: 'searchable_select',
  265. options: options,
  266. null: true,
  267. multiple: true,
  268. default: defaults['searchable_select6']
  269. },
  270. ]
  271. },
  272. autofocus: true
  273. })
  274. var params = App.ControllerForm.params(el)
  275. var test_params = {
  276. searchable_select5: 'bbb',
  277. searchable_select6: ['bbb'],
  278. searchable_select6_completion: '',
  279. }
  280. assert.deepEqual(params, test_params, 'form param check')
  281. $('[name="searchable_select5"].js-shadow + .js-input').trigger('focus').val('').trigger('input')
  282. var $element = $('[name="searchable_select5"]').closest('.searchableSelect').find('.js-optionsList')
  283. assert.equal($element.find('li.is-selected').length, 1, 'selection indicator count')
  284. $element.find('li:not(.is-hidden)').first().trigger('click')
  285. assert.equal($element.find('li.is-selected').length, 1, 'selection indicator count')
  286. $('[name="searchable_select6"].js-shadow + .js-input').trigger('focus').val('').trigger('input')
  287. $element = $('[name="searchable_select6"]').closest('.searchableSelect').find('.js-optionsList')
  288. assert.equal($element.find('li.is-selected').length, 1, 'selection indicator count')
  289. $element.find('li:not(.is-hidden)').first().trigger('click')
  290. assert.equal($element.find('li.is-selected').length, 2, 'selection indicator count')
  291. $('[name="searchable_select6"]').closest('.searchableSelect').find('.js-remove').first().trigger('click')
  292. assert.equal($element.find('li.is-selected').length, 1, 'selection indicator count')
  293. });