form.js 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. // form
  2. test("form without @el", function() {
  3. var form = new App.ControllerForm()
  4. equal($(form.html()).is('div'), true)
  5. equal($(form.html()).hasClass('alert'), true)
  6. equal($(form.html()).hasClass('hide'), true)
  7. })
  8. test("form elements check", function() {
  9. // deepEqual(item, test.value, 'group set/get tests' );
  10. $('#forms').append('<hr><h1>form elements check</h1><form id="form1"></form>')
  11. var el = $('#form1')
  12. var defaults = {
  13. input2: '123abc',
  14. password2: 'pw1234<l>',
  15. textarea2: 'lalu <l> lalu',
  16. select1: false,
  17. select2: true,
  18. selectmulti1: false,
  19. selectmulti2: [ false, true ],
  20. selectmultioption1: false,
  21. selectmultioption2: [ false, true ],
  22. richtext2: 'lalu <l> lalu',
  23. datetime1: Date.parse('2015-01-11T12:40:00Z'),
  24. }
  25. new App.ControllerForm({
  26. el: el,
  27. model: {
  28. configure_attributes: [
  29. { name: 'input1', display: 'Input1', tag: 'input', type: 'text', limit: 100, null: true, default: defaults['input1'] },
  30. { name: 'input2', display: 'Input2', tag: 'input', type: 'text', limit: 100, null: false, default: defaults['input2'] },
  31. { name: 'password1', display: 'Password1', tag: 'input', type: 'password', limit: 100, null: true, default: defaults['password1'] },
  32. { name: 'password2', display: 'Password2', tag: 'input', type: 'password', limit: 100, null: false, default: defaults['password2'] },
  33. { name: 'textarea1', display: 'Textarea1', tag: 'textarea', rows: 6, limit: 100, null: true, upload: true, default: defaults['textarea1'] },
  34. { name: 'textarea2', display: 'Textarea2', tag: 'textarea', rows: 6, limit: 100, null: false, upload: true, default: defaults['textarea2'] },
  35. { name: 'select1', display: 'Select1', tag: 'select', null: true, options: { true: 'internal', false: 'public' }, default: defaults['select1'] },
  36. { name: 'select2', display: 'Select2', tag: 'select', null: false, options: { true: 'internal', false: 'public' }, default: defaults['select2'] },
  37. { name: 'selectmulti1', display: 'SelectMulti1', tag: 'select', null: true, multiple: true, options: { true: 'internal', false: 'public' }, default: defaults['selectmulti1'] },
  38. { name: 'selectmulti2', display: 'SelectMulti2', tag: 'select', null: false, multiple: true, options: { true: 'internal', false: 'public' }, default: defaults['selectmulti2'] },
  39. { name: 'selectmultioption1', display: 'SelectMultiOption1', tag: 'select', null: true, multiple: true, options: [{ value: true, name: 'internal' }, { value: false, name: 'public' }], default: defaults['selectmultioption1'] },
  40. { name: 'selectmultioption2', display: 'SelectMultiOption2', tag: 'select', null: false, multiple: true, options: [{ value: true, name: 'A' }, { value: 1, name: 'B'}, { value: false, name: 'C' }], default: defaults['selectmultioption2'] },
  41. { name: 'richtext1', display: 'Richtext1', tag: 'richtext', limit: 100, null: true, upload: true, default: defaults['richtext1'] },
  42. { name: 'richtext2', display: 'Richtext2', tag: 'richtext', limit: 100, null: true, upload: true, default: defaults['richtext2'] },
  43. { name: 'datetime1', display: 'Datetime1', tag: 'datetime', null: true, default: defaults['datetime1'] },
  44. { name: 'datetime2', display: 'Datetime2', tag: 'datetime', null: false, default: defaults['datetime2'] },
  45. ]
  46. },
  47. autofocus: true
  48. });
  49. equal(el.find('[name="input1"]').val(), '', 'check input1 value')
  50. equal(el.find('[name="input1"]').prop('required'), false, 'check input1 required')
  51. // equal(el.find('[name="input1"]').is(":focus"), true, 'check input1 focus')
  52. equal(el.find('[name="input2"]').val(), '123abc', 'check input2 value')
  53. equal(el.find('[name="input2"]').prop('required'), true, 'check input2 required')
  54. equal(el.find('[name="input2"]').is(":focus"), false, 'check input2 focus')
  55. equal(el.find('[name="password1"]').val(), '', 'check password1 value')
  56. equal(el.find('[name="password1_confirm"]').val(), '', 'check password1 value')
  57. equal(el.find('[name="password1"]').prop('required'), false, 'check password1 required')
  58. equal(el.find('[name="password1"]').is(":focus"), false, 'check password1 focus')
  59. equal(el.find('[name="password2"]').val(), 'pw1234<l>', 'check password2 value')
  60. equal(el.find('[name="password2_confirm"]').val(), 'pw1234<l>', 'check password2 value')
  61. equal(el.find('[name="password2"]').prop('required'), true, 'check password2 required')
  62. equal(el.find('[name="password2"]').is(":focus"), false, 'check password2 focus')
  63. equal(el.find('[name="textarea1"]').val(), '', 'check textarea1 value')
  64. equal(el.find('[name="textarea1"]').prop('required'), false, 'check textarea1 required')
  65. equal(el.find('[name="textarea1"]').is(":focus"), false, 'check textarea1 focus')
  66. equal(el.find('[name="textarea2"]').val(), 'lalu <l> lalu', 'check textarea2 value')
  67. equal(el.find('[name="textarea2"]').prop('required'), true, 'check textarea2 required')
  68. equal(el.find('[name="textarea2"]').is(":focus"), false, 'check textarea2 focus')
  69. equal(el.find('[name="select1"]').val(), 'false', 'check select1 value')
  70. equal(el.find('[name="select1"]').prop('required'), false, 'check select1 required')
  71. equal(el.find('[name="select1"]').is(":focus"), false, 'check select1 focus')
  72. equal(el.find('[name="select2"]').val(), 'true', 'check select2 value')
  73. equal(el.find('[name="select2"]').prop('required'), true, 'check select2 required')
  74. equal(el.find('[name="select2"]').is(":focus"), false, 'check select2 focus')
  75. equal(el.find('[name="selectmulti1"]').val(), 'false', 'check selectmulti1 value')
  76. equal(el.find('[name="selectmulti1"]').prop('required'), false, 'check selectmulti1 required')
  77. equal(el.find('[name="selectmulti1"]').is(":focus"), false, 'check selectmulti1 focus')
  78. equal(el.find('[name="selectmulti2"]').val()[0], 'true', 'check selectmulti2 value')
  79. equal(el.find('[name="selectmulti2"]').val()[1], 'false', 'check selectmulti2 value')
  80. equal(el.find('[name="selectmulti2"]').prop('required'), true, 'check selectmulti2 required')
  81. equal(el.find('[name="selectmulti2"]').is(":focus"), false, 'check selectmulti2 focus')
  82. //equal(el.find('[name="richtext1"]').val(), '', 'check textarea1 value')
  83. //equal(el.find('[name="richtext1"]').prop('required'), false, 'check textarea1 required')
  84. equal(el.find('[name="richtext1"]').is(":focus"), false, 'check textarea1 focus')
  85. //equal(el.find('[name="richtext2"]').val(), 'lalu <l> lalu', 'check textarea2 value')
  86. //equal(el.find('[name="richtext2"]').prop('required'), true, 'check textarea2 required')
  87. equal(el.find('[name="richtext2"]').is(":focus"), false, 'check textarea2 focus')
  88. });
  89. test("form params check", function() {
  90. // deepEqual(item, test.value, 'group set/get tests' );
  91. $('#forms').append('<hr><h1>form params check</h1><form id="form2"></form>')
  92. var el = $('#form2')
  93. var defaults = {
  94. input2: '123abc',
  95. password2: 'pw1234<l>',
  96. textarea2: 'lalu <l> lalu',
  97. select1: false,
  98. select2: true,
  99. select3: null,
  100. select4: undefined,
  101. selectmulti1: false,
  102. selectmulti2: [ false, true ],
  103. selectmultioption1: false,
  104. selectmultioption2: [ false, true ],
  105. autocompletion2: 'id2',
  106. autocompletion2_autocompletion_value_shown: 'value2',
  107. richtext2: '<div>lalu <b>b</b> lalu</div>',
  108. richtext3: '<div></div>',
  109. richtext4: '<div>lalu <i>b</i> lalu</div>',
  110. richtext5: '<div></div>',
  111. richtext6: '<div>lalu <b>b</b> lalu</div>',
  112. richtext7: "<div>&nbsp;<div>&nbsp;\n</div> \n</div>",
  113. richtext8: '<div>lalu <i>b</i> lalu</div>',
  114. datetime1: new Date(Date.parse('2015-01-11T12:40:00Z') ),
  115. datetime3: new Date(Date.parse('2015-01-11T12:40:00Z') ),
  116. date1: '2015-01-11',
  117. date3: '2015-01-11',
  118. active1: true,
  119. active2: false,
  120. }
  121. new App.ControllerForm({
  122. el: el,
  123. model: {
  124. configure_attributes: [
  125. { name: 'input1', display: 'Input1', tag: 'input', type: 'text', limit: 100, null: true },
  126. { name: 'input2', display: 'Input2', tag: 'input', type: 'text', limit: 100, null: false },
  127. { name: 'password1', display: 'Password1', tag: 'input', type: 'password', limit: 100, null: true },
  128. { name: 'password2', display: 'Password2', tag: 'input', type: 'password', limit: 100, null: false },
  129. { name: 'textarea1', display: 'Textarea1', tag: 'textarea', rows: 6, limit: 100, null: true, upload: true },
  130. { name: 'textarea2', display: 'Textarea2', tag: 'textarea', rows: 6, limit: 100, null: false, upload: true },
  131. { name: 'select1', display: 'Select1', tag: 'select', null: true, options: { true: 'internal', false: 'public' } },
  132. { name: 'select2', display: 'Select2', tag: 'select', null: false, options: { true: 'internal', false: 'public' } },
  133. { name: 'select3', display: 'Select3', tag: 'select', null: false, nulloption: true, options: { aa: 'aa', bb: 'bb', select3: 'select3' } },
  134. { name: 'select4', display: 'Select4', tag: 'select', null: false, nulloption: true, options: { aa: 'aa', bb: 'bb', select3: 'select4' } },
  135. { name: 'selectmulti1', display: 'SelectMulti1', tag: 'select', null: true, multiple: true, options: { true: 'internal', false: 'public' } },
  136. { name: 'selectmulti2', display: 'SelectMulti2', tag: 'select', null: false, multiple: true, options: { true: 'internal', false: 'public' } },
  137. { name: 'selectmultioption1', display: 'SelectMultiOption1', tag: 'select', null: true, multiple: true, options: [{ value: true, name: 'internal' }, { value: false, name: 'public' }] },
  138. { name: 'selectmultioption2', display: 'SelectMultiOption2', tag: 'select', null: false, multiple: true, options: [{ value: true, name: 'A' }, { value: 1, name: 'B'}, { value: false, name: 'C' }] },
  139. { name: 'autocompletion1', display: 'AutoCompletion1', tag: 'autocompletion', null: false, options: { true: 'internal', false: 'public' }, source: [ { label: "Choice1", value: "value1", id: "id1" }, { label: "Choice2", value: "value2", id: "id2" }, ], minLength: 1 },
  140. { name: 'autocompletion2', display: 'AutoCompletion2', tag: 'autocompletion', null: false, options: { true: 'internal', false: 'public' }, source: [ { label: "Choice1", value: "value1", id: "id1" }, { label: "Choice2", value: "value2", id: "id2" }, ], minLength: 1 },
  141. { name: 'richtext1', display: 'Richtext1', tag: 'richtext', maxlength: 100, null: true, type: 'richtext', multiline: true, upload: true, default: defaults['richtext1'] },
  142. { name: 'richtext2', display: 'Richtext2', tag: 'richtext', maxlength: 100, null: true, type: 'richtext', multiline: true, upload: true, default: defaults['richtext2'] },
  143. { name: 'richtext3', display: 'Richtext3', tag: 'richtext', maxlength: 100, null: true, type: 'richtext', multiline: false, default: defaults['richtext3'] },
  144. { name: 'richtext4', display: 'Richtext4', tag: 'richtext', maxlength: 100, null: true, type: 'richtext', multiline: false, default: defaults['richtext4'] },
  145. { name: 'richtext5', display: 'Richtext5', tag: 'richtext', maxlength: 100, null: true, type: 'textonly', multiline: true, upload: true, default: defaults['richtext5'] },
  146. { name: 'richtext6', display: 'Richtext6', tag: 'richtext', maxlength: 100, null: true, type: 'textonly', multiline: true, upload: true, default: defaults['richtext6'] },
  147. { name: 'richtext7', display: 'Richtext7', tag: 'richtext', maxlength: 100, null: true, type: 'textonly', multiline: false, default: defaults['richtext7'] },
  148. { name: 'richtext8', display: 'Richtext8', tag: 'richtext', maxlength: 100, null: true, type: 'textonly', multiline: false, default: defaults['richtext8'] },
  149. { name: 'datetime1', display: 'Datetime1', tag: 'datetime', null: true, default: defaults['datetime1'] },
  150. { name: 'datetime2', display: 'Datetime2', tag: 'datetime', null: true, default: defaults['datetime2'] },
  151. { name: 'datetime3', display: 'Datetime3', tag: 'datetime', null: false, default: defaults['datetime3'] },
  152. { name: 'datetime4', display: 'Datetime4', tag: 'datetime', null: false, default: defaults['datetime4'] },
  153. { name: 'date1', display: 'Date1', tag: 'date', null: true, default: defaults['date1'] },
  154. { name: 'date2', display: 'Date2', tag: 'date', null: true, default: defaults['date2'] },
  155. { name: 'date3', display: 'Date3', tag: 'date', null: false, default: defaults['date3'] },
  156. { name: 'date4', display: 'Date4', tag: 'date', null: false, default: defaults['date4'] },
  157. { name: 'active1', display: 'Active1', tag: 'active', default: defaults['active1'] },
  158. { name: 'active2', display: 'Active2', tag: 'active', default: defaults['active2'] },
  159. ],
  160. },
  161. params: defaults,
  162. autofocus: true
  163. });
  164. equal(el.find('[name="input1"]').val(), '', 'check input1 value')
  165. equal(el.find('[name="input1"]').prop('required'), false, 'check input1 required')
  166. // equal(el.find('[name="input1"]').is(":focus"), true, 'check input1 focus')
  167. equal(el.find('[name="input2"]').val(), '123abc', 'check input2 value')
  168. equal(el.find('[name="input2"]').prop('required'), true, 'check input2 required')
  169. equal(el.find('[name="input2"]').is(":focus"), false, 'check input2 focus')
  170. equal(el.find('[name="password1"]').val(), '', 'check password1 value')
  171. equal(el.find('[name="password1_confirm"]').val(), '', 'check password1 value')
  172. equal(el.find('[name="password1"]').prop('required'), false, 'check password1 required')
  173. equal(el.find('[name="password1"]').is(":focus"), false, 'check password1 focus')
  174. equal(el.find('[name="password2"]').val(), 'pw1234<l>', 'check password2 value')
  175. equal(el.find('[name="password2_confirm"]').val(), 'pw1234<l>', 'check password2 value')
  176. equal(el.find('[name="password2"]').prop('required'), true, 'check password2 required')
  177. equal(el.find('[name="password2"]').is(":focus"), false, 'check password2 focus')
  178. equal(el.find('[name="textarea1"]').val(), '', 'check textarea1 value')
  179. equal(el.find('[name="textarea1"]').prop('required'), false, 'check textarea1 required')
  180. equal(el.find('[name="textarea1"]').is(":focus"), false, 'check textarea1 focus')
  181. equal(el.find('[name="textarea2"]').val(), 'lalu <l> lalu', 'check textarea2 value')
  182. equal(el.find('[name="textarea2"]').prop('required'), true, 'check textarea2 required')
  183. equal(el.find('[name="textarea2"]').is(":focus"), false, 'check textarea2 focus')
  184. equal(el.find('[name="select1"]').val(), 'false', 'check select1 value')
  185. equal(el.find('[name="select1"]').prop('required'), false, 'check select1 required')
  186. equal(el.find('[name="select1"]').is(":focus"), false, 'check select1 focus')
  187. equal(el.find('[name="select2"]').val(), 'true', 'check select2 value')
  188. equal(el.find('[name="select2"]').prop('required'), true, 'check select2 required')
  189. equal(el.find('[name="select2"]').is(":focus"), false, 'check select2 focus')
  190. equal(el.find('[name="select3"]').val(), '', 'check select3 value')
  191. equal(el.find('[name="select3"]').prop('required'), true, 'check select3 required')
  192. equal(el.find('[name="select3"]').is(":focus"), false, 'check select3 focus')
  193. equal(el.find('[name="select4"]').val(), '', 'check select4 value')
  194. equal(el.find('[name="select4"]').prop('required'), true, 'check select4 required')
  195. equal(el.find('[name="select4"]').is(":focus"), false, 'check select4 focus')
  196. equal(el.find('[name="selectmulti1"]').val(), 'false', 'check selectmulti1 value')
  197. equal(el.find('[name="selectmulti1"]').prop('required'), false, 'check selectmulti1 required')
  198. equal(el.find('[name="selectmulti1"]').is(":focus"), false, 'check selectmulti1 focus')
  199. equal(el.find('[name="selectmulti2"]').val()[0], 'true', 'check selectmulti2 value')
  200. equal(el.find('[name="selectmulti2"]').val()[1], 'false', 'check selectmulti2 value')
  201. equal(el.find('[name="selectmulti2"]').prop('required'), true, 'check selectmulti2 required')
  202. equal(el.find('[name="selectmulti2"]').is(":focus"), false, 'check selectmulti2 focus')
  203. params = App.ControllerForm.params(el)
  204. test_params = {
  205. input1: '',
  206. input2: '123abc',
  207. password1: '',
  208. password1_confirm: '',
  209. password2: 'pw1234<l>',
  210. password2_confirm: 'pw1234<l>',
  211. textarea1: '',
  212. textarea2: 'lalu <l> lalu',
  213. select1: 'false',
  214. select2: 'true',
  215. select3: '',
  216. select4: '',
  217. selectmulti1: 'false',
  218. selectmulti2: [ 'true', 'false' ],
  219. selectmultioption1: 'false',
  220. selectmultioption2: [ 'true', 'false' ],
  221. autocompletion1: '',
  222. autocompletion1_autocompletion: '',
  223. autocompletion1_autocompletion_value_shown: '',
  224. autocompletion2: 'id2',
  225. autocompletion2_autocompletion: 'value2',
  226. autocompletion2_autocompletion_value_shown: 'value2',
  227. richtext1: '',
  228. richtext2: '<div>lalu <b>b</b> lalu</div>',
  229. richtext3: '',
  230. richtext4: '<div>lalu <i>b</i> lalu</div>',
  231. richtext5: '',
  232. richtext6: '<div>lalu <b>b</b> lalu</div>',
  233. richtext7: '',
  234. richtext8: '<div>lalu <i>b</i> lalu</div>',
  235. datetime1: '2015-01-11T12:40:00.000Z',
  236. datetime2: undefined,
  237. datetime3: '2015-01-11T12:40:00.000Z',
  238. datetime4: undefined,
  239. date1: '2015-01-11',
  240. date2: undefined,
  241. date3: '2015-01-11',
  242. date4: undefined,
  243. active1: true,
  244. active2: false,
  245. }
  246. deepEqual(params, test_params, 'form param check')
  247. });
  248. test("form defaults + params check", function() {
  249. // deepEqual(item, test.value, 'group set/get tests' );
  250. // mix default and params -> check it -> add note
  251. // test auto completion
  252. // show/hide fields base on field values -> bind changed event
  253. // form validation
  254. // form params check
  255. // add signature only if form_state is empty
  256. $('#forms').append('<hr><h1>form defaults + params check</h1><form id="form3"></form>')
  257. var el = $('#form3')
  258. var defaults = {
  259. input1: '',
  260. password2: 'pw1234<l>',
  261. textarea2: 'lalu <l> lalu',
  262. select2: false,
  263. selectmulti2: [ false, true ],
  264. selectmultioption1: false,
  265. }
  266. new App.ControllerForm({
  267. el: el,
  268. model: {
  269. configure_attributes: [
  270. { name: 'input1', display: 'Input1', tag: 'input', type: 'text', limit: 100, null: true, default: 'some not used default' },
  271. { name: 'input2', display: 'Input2', tag: 'input', type: 'text', limit: 100, null: true, default: 'some used default' },
  272. { name: 'password1', display: 'Password1', tag: 'input', type: 'password', limit: 100, null: false, default: 'some used pass' },
  273. { name: 'password2', display: 'Password2', tag: 'input', type: 'password', limit: 100, null: false, default: 'some not used pass' },
  274. { name: 'textarea1', display: 'Textarea1', tag: 'textarea', rows: 6, limit: 100, null: false, upload: true, default: 'some used text' },
  275. { name: 'textarea2', display: 'Textarea2', tag: 'textarea', rows: 6, limit: 100, null: false, upload: true, default: 'some not used text' },
  276. { name: 'select1', display: 'Select1', tag: 'select', null: true, options: { true: 'internal', false: 'public' }, default: false},
  277. { name: 'select2', display: 'Select2', tag: 'select', null: true, options: { true: 'internal', false: 'public' }, default: true },
  278. { name: 'selectmulti2', display: 'SelectMulti2', tag: 'select', null: false, multiple: true, options: { true: 'internal', false: 'public' }, default: [] },
  279. { name: 'selectmultioption1', display: 'SelectMultiOption1', tag: 'select', null: true, multiple: true, options: [{ value: true, name: 'internal' }, { value: false, name: 'public' }], default: true },
  280. ],
  281. },
  282. params: defaults,
  283. autofocus: true
  284. });
  285. equal(el.find('[name="input1"]').val(), '', 'check input1 value')
  286. equal(el.find('[name="input1"]').prop('required'), false, 'check input1 required')
  287. // equal(el.find('[name="input1"]').is(":focus"), true, 'check input1 focus')
  288. equal(el.find('[name="input2"]').val(), 'some used default', 'check input2 value')
  289. equal(el.find('[name="input2"]').prop('required'), false, 'check input2 required')
  290. equal(el.find('[name="password1"]').val(), 'some used pass', 'check password1 value')
  291. equal(el.find('[name="password1_confirm"]').val(), 'some used pass', 'check password1 value')
  292. equal(el.find('[name="password1"]').prop('required'), true, 'check password1 required')
  293. equal(el.find('[name="password1"]').is(":focus"), false, 'check password1 focus')
  294. equal(el.find('[name="password2"]').val(), 'pw1234<l>', 'check password2 value')
  295. equal(el.find('[name="password2_confirm"]').val(), 'pw1234<l>', 'check password2 value')
  296. equal(el.find('[name="password2"]').prop('required'), true, 'check password2 required')
  297. equal(el.find('[name="password2"]').is(":focus"), false, 'check password2 focus')
  298. equal(el.find('[name="textarea1"]').val(), 'some used text', 'check textarea1 value')
  299. equal(el.find('[name="textarea1"]').prop('required'), true, 'check textarea1 required')
  300. equal(el.find('[name="textarea1"]').is(":focus"), false, 'check textarea1 focus')
  301. equal(el.find('[name="textarea2"]').val(), 'lalu <l> lalu', 'check textarea2 value')
  302. equal(el.find('[name="textarea2"]').prop('required'), true, 'check textarea2 required')
  303. equal(el.find('[name="textarea2"]').is(":focus"), false, 'check textarea2 focus')
  304. equal(el.find('[name="select1"]').val(), 'false', 'check select1 value')
  305. equal(el.find('[name="select1"]').prop('required'), false, 'check select1 required')
  306. equal(el.find('[name="select1"]').is(":focus"), false, 'check select1 focus')
  307. equal(el.find('[name="select2"]').val(), 'false', 'check select2 value')
  308. equal(el.find('[name="select2"]').prop('required'), false, 'check select2 required')
  309. equal(el.find('[name="select2"]').is(":focus"), false, 'check select2 focus')
  310. equal(el.find('[name="selectmulti2"]').val()[0], 'true', 'check selectmulti2 value')
  311. equal(el.find('[name="selectmulti2"]').val()[1], 'false', 'check selectmulti2 value')
  312. equal(el.find('[name="selectmulti2"]').prop('required'), true, 'check selectmulti2 required')
  313. equal(el.find('[name="selectmulti2"]').is(":focus"), false, 'check selectmulti2 focus')
  314. });
  315. test("form dependend fields check", function() {
  316. // deepEqual(item, test.value, 'group set/get tests' );
  317. // mix default and params -> check it -> add note
  318. // test auto completion
  319. // show/hide fields base on field values -> bind changed event
  320. // form validation
  321. // form params check
  322. // add signature only if form_state is empty
  323. $('#forms').append('<hr><h1>form dependend fields check</h1><form id="form4"></form>')
  324. var el = $('#form4')
  325. var defaults = {
  326. input1: '',
  327. select2: false,
  328. selectmulti2: [ false, true ],
  329. selectmultioption1: false,
  330. datetime1: new Date(Date.parse('2015-01-11T12:40:00Z')),
  331. datetime3: new Date(Date.parse('2015-01-11T12:40:00Z')),
  332. date1: '2015-01-11',
  333. date3: '2015-01-11',
  334. }
  335. var form = new App.ControllerForm({
  336. el: el,
  337. model: {
  338. configure_attributes: [
  339. { name: 'input1', display: 'Input1', tag: 'input', type: 'text', limit: 100, null: true, default: 'some not used default' },
  340. { name: 'input2', display: 'Input2', tag: 'input', type: 'text', limit: 100, null: true, default: 'some used default' },
  341. { name: 'input3', display: 'Input3', tag: 'input', type: 'text', limit: 100, null: true, hide: true, default: 'some used default' },
  342. { name: 'select1', display: 'Select1', tag: 'select', null: true, options: { true: 'internal', false: 'public' }, default: false},
  343. { name: 'select2', display: 'Select2', tag: 'select', null: true, options: { true: 'internal', false: 'public' }, default: true },
  344. { name: 'selectmulti2', display: 'SelectMulti2', tag: 'select', null: false, multiple: true, options: { true: 'internal', false: 'public' }, default: [] },
  345. { name: 'selectmultioption1', display: 'SelectMultiOption1', tag: 'select', null: true, multiple: true, options: [{ value: true, name: 'internal' }, { value: false, name: 'public' }], default: true },
  346. { name: 'datetime1', display: 'Datetime1', tag: 'datetime', null: true, default: defaults['datetime1'] },
  347. { name: 'datetime2', display: 'Datetime2', tag: 'datetime', null: true, default: defaults['datetime2'] },
  348. { name: 'datetime3', display: 'Datetime3', tag: 'datetime', null: false, default: defaults['datetime3'] },
  349. { name: 'datetime4', display: 'Datetime4', tag: 'datetime', null: false, default: defaults['datetime4'] },
  350. { name: 'date1', display: 'Date1', tag: 'date', null: true, default: defaults['date1'] },
  351. { name: 'date2', display: 'Date2', tag: 'date', null: true, default: defaults['date2'] },
  352. { name: 'date3', display: 'Date3', tag: 'date', null: false, default: defaults['date3'] },
  353. { name: 'date4', display: 'Date4', tag: 'date', null: false, default: defaults['date4'] },
  354. ],
  355. },
  356. params: defaults,
  357. dependency: [
  358. {
  359. bind: {
  360. name: 'select1',
  361. value: ["true"]
  362. },
  363. change: {
  364. name: 'input2',
  365. action: 'hide'
  366. },
  367. },
  368. {
  369. bind: {
  370. name: 'select1',
  371. value: ["true"]
  372. },
  373. change: {
  374. name: 'datetime1',
  375. action: 'hide'
  376. },
  377. },
  378. {
  379. bind: {
  380. name: 'select1',
  381. value: ["false"]
  382. },
  383. change: {
  384. name: 'input2',
  385. action: 'show'
  386. },
  387. },
  388. {
  389. bind: {
  390. name: 'select1',
  391. value: ["false"]
  392. },
  393. change: {
  394. name: 'datetime1',
  395. action: 'show'
  396. },
  397. },
  398. {
  399. bind: {
  400. name: 'select1',
  401. value: ["true"]
  402. },
  403. change: {
  404. name: 'input3',
  405. action: 'show'
  406. },
  407. },
  408. {
  409. bind: {
  410. name: 'select1',
  411. value: ["false"]
  412. },
  413. change: {
  414. name: 'input3',
  415. action: 'hide'
  416. },
  417. }
  418. ],
  419. autofocus: true
  420. });
  421. equal(el.find('[name="input1"]').val(), '', 'check input1 value')
  422. equal(el.find('[name="input1"]').prop('required'), false, 'check input1 required')
  423. // equal(el.find('[name="input1"]').is(":focus"), true, 'check input1 focus')
  424. equal(el.find('[name="input2"]').val(), 'some used default', 'check input2 value')
  425. equal(el.find('[name="input2"]').prop('required'), false, 'check input2 required')
  426. equal(el.find('[name="input3"]').val(), 'some used default', 'check input3 value')
  427. equal(el.find('[name="input3"]').prop('required'), false, 'check input3 required')
  428. equal(el.find('[name="select1"]').val(), 'false', 'check select1 value')
  429. equal(el.find('[name="select1"]').prop('required'), false, 'check select1 required')
  430. equal(el.find('[name="select1"]').is(":focus"), false, 'check select1 focus')
  431. equal(el.find('[name="select2"]').val(), 'false', 'check select2 value')
  432. equal(el.find('[name="select2"]').prop('required'), false, 'check select2 required')
  433. equal(el.find('[name="select2"]').is(":focus"), false, 'check select2 focus')
  434. equal(el.find('[name="selectmulti2"]').val()[0], 'true', 'check selectmulti2 value')
  435. equal(el.find('[name="selectmulti2"]').val()[1], 'false', 'check selectmulti2 value')
  436. equal(el.find('[name="selectmulti2"]').prop('required'), true, 'check selectmulti2 required')
  437. equal(el.find('[name="selectmulti2"]').is(":focus"), false, 'check selectmulti2 focus')
  438. var params = App.ControllerForm.params(el)
  439. var test_params = {
  440. input1: "",
  441. input2: "some used default",
  442. select1: "false",
  443. select2: "false",
  444. selectmulti2: [ "true", "false" ],
  445. selectmultioption1: "false",
  446. datetime1: '2015-01-11T12:40:00.000Z',
  447. datetime2: undefined,
  448. datetime3: '2015-01-11T12:40:00.000Z',
  449. datetime4: undefined,
  450. date1: '2015-01-11',
  451. date2: undefined,
  452. date3: '2015-01-11',
  453. date4: undefined,
  454. }
  455. deepEqual(params, test_params, 'form param check')
  456. errors = form.validate(params)
  457. test_errors = {
  458. datetime4: "is required",
  459. date4: "is required",
  460. }
  461. deepEqual(errors, test_errors, 'validation errors check')
  462. App.ControllerForm.validate({ errors: errors, form: el })
  463. el.find('[name="select1"]').val('true')
  464. el.find('[name="select1"]').trigger('change')
  465. params = App.ControllerForm.params(el)
  466. test_params = {
  467. input1: "",
  468. input3: "some used default",
  469. select1: "true",
  470. select2: "false",
  471. selectmulti2: [ "true", "false" ],
  472. selectmultioption1: "false",
  473. datetime1: null,
  474. datetime2: undefined,
  475. datetime3: '2015-01-11T12:40:00.000Z',
  476. datetime4: undefined,
  477. date1: '2015-01-11',
  478. date2: undefined,
  479. date3: '2015-01-11',
  480. date4: undefined,
  481. }
  482. deepEqual(params, test_params, 'form param check')
  483. });
  484. test("form handler check with and without fieldset", function() {
  485. // deepEqual(item, test.value, 'group set/get tests' );
  486. // mix default and params -> check it -> add note
  487. // test auto completion
  488. // show/hide fields base on field values -> bind changed event
  489. // form validation
  490. // form params check
  491. // add signature only if form_state is empty
  492. $('#forms').append('<hr><h1>form handler check with and without fieldset</h1><form id="form5"></form>')
  493. var el = $('#form5')
  494. var defaults = {
  495. select1: 'a',
  496. select2: '',
  497. }
  498. var formChanges = function(params, attribute, attributes, classname, form, ui) {
  499. //console.log('FROM', form)
  500. if (params['select1'] === 'b') {
  501. //console.log('select1 -> b', params)
  502. var item = {
  503. name: 'select2',
  504. display: 'Select2',
  505. tag: 'select',
  506. null: true,
  507. options: { 1:'1', 2:'2', 3:'3' },
  508. default: 3,
  509. };
  510. var newElement = ui.formGenItem(item, classname, form)
  511. form.find('[name="select2"]').closest('.form-group').replaceWith(newElement)
  512. }
  513. if (params['select1'] === 'a') {
  514. //console.log('select1 -> a', params)
  515. var item = {
  516. name: 'select2',
  517. display: 'Select2',
  518. tag: 'select',
  519. null: true,
  520. options: { 1:'1', 2:'2', 3:'3' },
  521. default: 1,
  522. };
  523. var newElement = ui.formGenItem(item, classname, form)
  524. form.find('[name="select2"]').closest('.form-group').replaceWith(newElement)
  525. }
  526. }
  527. new App.ControllerForm({
  528. el: el,
  529. model: {
  530. configure_attributes: [
  531. { name: 'select1', display: 'Select1', tag: 'select', null: true, options: { a: 'a', b: 'b' }, default: 'b'},
  532. { name: 'select2', display: 'Select2', tag: 'select', null: true, options: { 1:'1', 2:'2', 3:'3' }, default: 2 },
  533. ],
  534. },
  535. params: defaults,
  536. handlers: [
  537. formChanges,
  538. ],
  539. //noFieldset: true,
  540. });
  541. equal(el.find('[name="select1"]').val(), 'a', 'check select1 value')
  542. equal(el.find('[name="select1"]').prop('required'), false, 'check select1 required')
  543. equal(el.find('[name="select2"]').val(), '1', 'check select2 value')
  544. equal(el.find('[name="select2"]').prop('required'), false, 'check select2 required')
  545. var params = App.ControllerForm.params(el)
  546. var test_params = {
  547. select1: 'a',
  548. select2: '1',
  549. }
  550. deepEqual(params, test_params, 'form param check')
  551. el.find('[name="select1"]').val('b')
  552. el.find('[name="select1"]').trigger('change')
  553. params = App.ControllerForm.params(el)
  554. test_params = {
  555. select1: 'b',
  556. select2: '3',
  557. }
  558. deepEqual(params, test_params, 'form param check')
  559. el.find('[name="select1"]').val('a')
  560. el.find('[name="select1"]').trigger('change')
  561. params = App.ControllerForm.params(el)
  562. test_params = {
  563. select1: 'a',
  564. select2: '1',
  565. }
  566. deepEqual(params, test_params, 'form param check')
  567. // test with noFieldset
  568. el.empty()
  569. new App.ControllerForm({
  570. el: el,
  571. model: {
  572. configure_attributes: [
  573. { name: 'select1', display: 'Select1', tag: 'select', null: true, options: { a: 'a', b: 'b' }, default: 'b'},
  574. { name: 'select2', display: 'Select2', tag: 'select', null: true, options: { 1:'1', 2:'2', 3:'3' }, default: 2 },
  575. ],
  576. },
  577. params: defaults,
  578. handlers: [
  579. formChanges,
  580. ],
  581. noFieldset: true,
  582. });
  583. equal(el.find('[name="select1"]').val(), 'a', 'check select1 value')
  584. equal(el.find('[name="select1"]').prop('required'), false, 'check select1 required')
  585. equal(el.find('[name="select2"]').val(), '1', 'check select2 value')
  586. equal(el.find('[name="select2"]').prop('required'), false, 'check select2 required')
  587. var params = App.ControllerForm.params(el)
  588. var test_params = {
  589. select1: 'a',
  590. select2: '1',
  591. }
  592. deepEqual(params, test_params, 'form param check')
  593. el.find('[name="select1"]').val('b')
  594. el.find('[name="select1"]').trigger('change')
  595. params = App.ControllerForm.params(el)
  596. test_params = {
  597. select1: 'b',
  598. select2: '3',
  599. }
  600. deepEqual(params, test_params, 'form param check')
  601. el.find('[name="select1"]').val('a')
  602. el.find('[name="select1"]').trigger('change')
  603. params = App.ControllerForm.params(el)
  604. test_params = {
  605. select1: 'a',
  606. select2: '1',
  607. }
  608. deepEqual(params, test_params, 'form param check')
  609. });
  610. test("form postmaster filter", function() {
  611. // check match area
  612. // check set area
  613. // add match rule
  614. // add set rule
  615. App.TicketPriority.refresh([
  616. {
  617. id: 1,
  618. name: 'prio 1',
  619. },
  620. {
  621. id: 2,
  622. name: 'prio 2',
  623. },
  624. ] )
  625. App.Group.refresh([
  626. {
  627. id: 1,
  628. name: 'group 1',
  629. },
  630. {
  631. id: 2,
  632. name: 'group 2',
  633. },
  634. ] )
  635. $('#forms').append('<hr><h1>form postmaster filter</h1><form id="form6"></form>')
  636. var el = $('#form6')
  637. var defaults = {
  638. input2: 'some name',
  639. match: {
  640. from: {
  641. operator: 'contains',
  642. value: 'some@address',
  643. },
  644. subject: {
  645. operator: 'contains',
  646. value: 'some subject',
  647. },
  648. },
  649. set: {
  650. 'x-zammad-ticket-customer_id': {
  651. value: 'customer',
  652. value_completion: ''
  653. },
  654. 'x-zammad-ticket-group_id': {
  655. value: '1'
  656. },
  657. 'x-zammad-ticket-owner_id': {
  658. value: 'owner',
  659. value_completion: ''
  660. },
  661. 'x-zammad-ticket-priority_id': {
  662. value: '1'
  663. }
  664. },
  665. }
  666. new App.ControllerForm({
  667. el: el,
  668. model: {
  669. configure_attributes: [
  670. { name: 'input1', display: 'Input1', tag: 'input', type: 'text', limit: 100, null: true, default: 'some not used default' },
  671. { name: 'input2', display: 'Input2', tag: 'input', type: 'text', limit: 100, null: true, default: 'some used default' },
  672. { name: 'match', display: 'Match', tag: 'postmaster_match', null: false, default: false},
  673. { name: 'set', display: 'Set', tag: 'postmaster_set', null: false, default: false},
  674. ],
  675. },
  676. params: defaults,
  677. });
  678. params = App.ControllerForm.params(el)
  679. test_params = {
  680. input1: 'some not used default',
  681. input2: 'some name',
  682. match: {
  683. from: {
  684. operator: 'contains',
  685. value: 'some@address'
  686. },
  687. subject: {
  688. operator: 'contains',
  689. value: 'some subject'
  690. }
  691. },
  692. set: {
  693. 'x-zammad-ticket-customer_id': {
  694. value: 'customer',
  695. value_completion: ''
  696. },
  697. 'x-zammad-ticket-group_id': {
  698. value: '1'
  699. },
  700. 'x-zammad-ticket-owner_id': {
  701. value: 'owner',
  702. value_completion: ''
  703. },
  704. 'x-zammad-ticket-priority_id': {
  705. value: '1'
  706. }
  707. },
  708. };
  709. deepEqual(params, test_params, 'form param check')
  710. el.find('[name="set::x-zammad-ticket-priority_id::value"]').closest('.js-filterElement').find('.js-remove').click()
  711. el.find('[name="set::x-zammad-ticket-customer_id::value"]').closest('.js-filterElement').find('.js-remove').click()
  712. App.Delay.set(function() {
  713. test("form param check after remove click", function() {
  714. params = App.ControllerForm.params(el)
  715. test_params = {
  716. input1: 'some not used default',
  717. input2: 'some name',
  718. match: {
  719. from: {
  720. operator: 'contains',
  721. value: 'some@address'
  722. },
  723. subject: {
  724. operator: 'contains',
  725. value: 'some subject'
  726. }
  727. },
  728. set: {
  729. 'x-zammad-ticket-owner_id': {
  730. value: 'owner',
  731. value_completion: ''
  732. },
  733. 'x-zammad-ticket-group_id': {
  734. value: '1'
  735. },
  736. },
  737. };
  738. deepEqual(params, test_params, 'form param check')
  739. });
  740. },
  741. 1000
  742. );
  743. });
  744. test("form selector", function() {
  745. $('#forms').append('<hr><h1>form selector</h1><div><form id="form7"></form></div>')
  746. var el = $('#form7')
  747. var defaults = {
  748. input2: 'some name66',
  749. }
  750. new App.ControllerForm({
  751. el: el,
  752. model: {
  753. configure_attributes: [
  754. { name: 'input1', display: 'Input1', tag: 'input', type: 'text', limit: 100, null: true, default: 'some not used default33' },
  755. { name: 'input2', display: 'Input2', tag: 'input', type: 'text', limit: 100, null: true, default: 'some used default' },
  756. ],
  757. },
  758. params: defaults,
  759. });
  760. test_params = {
  761. input1: 'some not used default33',
  762. input2: 'some name66',
  763. };
  764. params = App.ControllerForm.params(el)
  765. deepEqual(params, test_params, 'form param check via $("#form")')
  766. params = App.ControllerForm.params(el.find('input'))
  767. deepEqual(params, test_params, 'form param check via $("#form").find("input")')
  768. params = App.ControllerForm.params(el.parent())
  769. deepEqual(params, test_params, 'form param check via $("#form").parent()')
  770. });
  771. test("form required_if + shown_if", function() {
  772. $('#forms').append('<hr><h1>form required_if + shown_if</h1><div><form id="form8"></form></div>')
  773. var el = $('#form8')
  774. var defaults = {
  775. input2: 'some name66',
  776. input3: 'some name77',
  777. input4: 'some name88',
  778. }
  779. new App.ControllerForm({
  780. el: el,
  781. model: {
  782. configure_attributes: [
  783. { name: 'input1', display: 'Input1', tag: 'input', type: 'text', limit: 100, null: true, default: 'some not used default33' },
  784. { name: 'input2', display: 'Input2', tag: 'input', type: 'text', limit: 100, null: true, default: 'some used default', required_if: { active: true }, shown_if: { active: true } },
  785. { name: 'input3', display: 'Input3', tag: 'input', type: 'text', limit: 100, null: true, default: 'some used default', required_if: { active: [true,false] }, shown_if: { active: [true,false] } },
  786. { name: 'input4', display: 'Input4', tag: 'input', type: 'text', limit: 100, null: true, default: 'some used default', required_if: { active: [55,66] }, shown_if: { active: [55,66] } },
  787. { name: 'active', display: 'Active', tag: 'active', 'default': true },
  788. ],
  789. },
  790. params: defaults,
  791. });
  792. test_params = {
  793. input1: "some not used default33",
  794. input2: "some name66",
  795. input3: "some name77",
  796. active: true,
  797. };
  798. params = App.ControllerForm.params(el)
  799. deepEqual(params, test_params, 'form param check via $("#form")')
  800. equal(el.find('[name="input2"]').attr('required'), 'required', 'check required attribute of input2 ')
  801. equal(el.find('[name="input2"]').is(":visible"), true, 'check visible attribute of input2 ')
  802. equal(el.find('[name="input3"]').attr('required'), 'required', 'check required attribute of input3 ')
  803. equal(el.find('[name="input3"]').is(":visible"), true, 'check visible attribute of input3 ')
  804. equal(el.find('[name="input4"]').is(":visible"), false, 'check visible attribute of input4 ')
  805. el.find('[name="active"]').val('false').trigger('change')
  806. test_params = {
  807. input1: "some not used default33",
  808. active: false,
  809. };
  810. params = App.ControllerForm.params(el)
  811. deepEqual(params, test_params, 'form param check via $("#form")')
  812. equal(el.find('[name="input2"]').attr('required'), undefined, 'check required attribute of input2')
  813. equal(el.find('[name="input2"]').is(":visible"), false, 'check visible attribute of input2')
  814. equal(el.find('[name="input3"]').is(":visible"), false, 'check visible attribute of input3')
  815. equal(el.find('[name="input4"]').is(":visible"), false, 'check visible attribute of input4')
  816. el.find('[name="active"]').val('true').trigger('change')
  817. test_params = {
  818. input1: "some not used default33",
  819. input2: "some name66",
  820. input3: "some name77",
  821. active: true,
  822. };
  823. params = App.ControllerForm.params(el)
  824. deepEqual(params, test_params, 'form param check via $("#form")')
  825. equal(el.find('[name="input2"]').attr('required'), 'required', 'check required attribute of input2')
  826. equal(el.find('[name="input2"]').is(":visible"), true, 'check visible attribute of input2')
  827. equal(el.find('[name="input3"]').attr('required'), 'required', 'check required attribute of input3')
  828. equal(el.find('[name="input3"]').is(":visible"), true, 'check visible attribute of input3')
  829. equal(el.find('[name="input4"]').is(":visible"), false, 'check visible attribute of input4')
  830. });
  831. test("form params check", function() {
  832. $('#forms').append('<hr><h1>form params check</h1><form id="form9"></form>')
  833. var el = $('#form9')
  834. var defaults = {
  835. select1: false,
  836. select2: true,
  837. select3: null,
  838. select4: undefined,
  839. }
  840. new App.ControllerForm({
  841. el: el,
  842. model: {
  843. configure_attributes: [
  844. { name: 'select1', display: 'Select1', tag: 'select', null: true, options: { true: 'internal', false: 'public' } },
  845. { name: 'select2', display: 'Select2', tag: 'select', null: false, options: { true: 'internal', false: 'public' } },
  846. { name: 'select3', display: 'Select3', tag: 'select', null: false, nulloption: true, options: { aa: 'aa', bb: 'bb', select3: 'select3' } },
  847. { name: 'select4', display: 'Select4', tag: 'select', null: false, nulloption: true, options: { aa: 'aa', bb: 'bb', select3: 'select4' } },
  848. ],
  849. },
  850. params: defaults,
  851. autofocus: true
  852. });
  853. params = App.ControllerForm.params(el)
  854. test_params = {
  855. select1: 'false',
  856. select2: 'true',
  857. select3: '',
  858. select4: '',
  859. }
  860. //console.log('params', params)
  861. //console.log('test_params', test_params)
  862. deepEqual(params, test_params, 'form param check')
  863. });
  864. test("form params check direct", function() {
  865. $('#forms').append('<hr><h1>form params check direct</h1><form id="form10"><input name="a" value="b"><input name="l::l::l1" value="d"><input name="l::l::" value><input name="f::f::" value><input name="f::f::f1" value="e"></form>')
  866. var el = $('#form10')
  867. params = App.ControllerForm.params(el)
  868. test_params = {
  869. a: 'b',
  870. l: {
  871. l: {
  872. l1: 'd',
  873. '': '',
  874. },
  875. },
  876. f: {
  877. f: {
  878. f1: 'e',
  879. '': '',
  880. },
  881. },
  882. }
  883. //console.log('params', params)
  884. //console.log('test_params', test_params)
  885. deepEqual(params, test_params, 'form param check')
  886. });