alpaca_controls.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /* ------------------------------------------------------------------------------
  2. *
  3. * # Alpaca - Controls
  4. *
  5. * Demo JS code for alpaca_controls.html page
  6. *
  7. * ---------------------------------------------------------------------------- */
  8. // Setup module
  9. // ------------------------------
  10. var AlpacaControls = function() {
  11. //
  12. // Setup module components
  13. //
  14. // Alpaca examples
  15. var _componentAlpacaControls = function() {
  16. if (!$().alpaca) {
  17. console.warn('Warning - alpaca.min.js is not loaded.');
  18. return;
  19. }
  20. // Checkboxes
  21. // ------------------------------
  22. // Checkbox with label
  23. var $alpacaCheckboxLabel = $('#alpaca-checkbox-label');
  24. $alpacaCheckboxLabel.alpaca({
  25. data: true,
  26. options: {
  27. label: 'Question:',
  28. rightLabel: 'Do you like Alpaca?'
  29. },
  30. postRender: function(control) {
  31. $alpacaCheckboxLabel.find('.checkbox').addClass('form-check');
  32. $alpacaCheckboxLabel.find('label:not(.alpaca-control-label)').addClass('form-check-label');
  33. $alpacaCheckboxLabel.find('input[type=checkbox]').addClass('form-check-input');
  34. }
  35. });
  36. // Display only mode
  37. $('#alpaca-checkbox-static').alpaca({
  38. data: false,
  39. view: 'bootstrap-display',
  40. options: {
  41. label: 'Registered?'
  42. }
  43. });
  44. // Basic checkbox list
  45. var $alpacaCheckboxList = $('#alpaca-checkbox-list');
  46. $alpacaCheckboxList.alpaca({
  47. data: ['sandwich', 'cookie', 'drink'],
  48. schema: {
  49. type: 'array',
  50. enum: ['sandwich', 'chips', 'cookie', 'drink']
  51. },
  52. options: {
  53. type: 'checkbox',
  54. label: 'What would you like with your order?',
  55. optionLabels: ['A Sandwich', 'Potato Chips', 'A Cookie', 'Soft Drink']
  56. },
  57. postRender: function(control) {
  58. $alpacaCheckboxList.find('.checkbox').addClass('form-check');
  59. $alpacaCheckboxList.find('label:not(.alpaca-control-label)').addClass('form-check-label');
  60. $alpacaCheckboxList.find('input[type=checkbox]').addClass('form-check-input');
  61. }
  62. });
  63. // Radios
  64. // ------------------------------
  65. // Basic radios
  66. var $alpacaRadioBasic = $('#alpaca-radio-basic');
  67. $alpacaRadioBasic.alpaca({
  68. data: 'green',
  69. options: {
  70. type: 'radio',
  71. label: 'Favorite Color',
  72. helper: 'Pick your favorite color',
  73. optionLabels: {
  74. red: 'Red',
  75. green: 'Green',
  76. blue: 'Blue',
  77. white: 'White',
  78. black: 'Black'
  79. }
  80. },
  81. schema: {
  82. required: true,
  83. enum: ['red', 'green', 'blue', 'white', 'black']
  84. },
  85. postRender: function(control) {
  86. $alpacaRadioBasic.find('.radio').addClass('form-check');
  87. $alpacaRadioBasic.find('label:not(.alpaca-control-label)').addClass('form-check-label');
  88. $alpacaRadioBasic.find('input[type=radio]').addClass('form-check-input');
  89. }
  90. });
  91. // Disabled mode
  92. var $alpacaRadioBasicDisabled = $('#alpaca-radio-basic-disabled');
  93. $alpacaRadioBasicDisabled.alpaca({
  94. data: 'Jimi Hendrix',
  95. schema: {
  96. enum: ['Jimi Hendrix', 'Mark Knopfler', 'Joe Satriani', 'Eddie Van Halen', 'Orianthi']
  97. },
  98. options: {
  99. type: 'radio',
  100. label: 'Who is your favorite guitarist?',
  101. vertical: true,
  102. disabled: true
  103. },
  104. postRender: function(control) {
  105. $alpacaRadioBasicDisabled.find('.radio').addClass('form-check');
  106. $alpacaRadioBasicDisabled.find('label:not(.alpaca-control-label)').addClass('form-check-label');
  107. $alpacaRadioBasicDisabled.find('input[type=radio]').addClass('form-check-input');
  108. }
  109. });
  110. // Required radios
  111. var $alpacaRadioRequired = $('#alpaca-radio-required');
  112. $alpacaRadioRequired.alpaca({
  113. data: 'Coffee2',
  114. options: {
  115. label: 'Ice cream',
  116. helper: 'Guess my favorite ice cream?',
  117. optionLabels: ['Vanilla Flavor', 'Chocolate Flavor', 'Coffee Flavor']
  118. },
  119. schema: {
  120. required: true,
  121. enum: ['Vanilla', 'Chocolate', 'Coffee']
  122. },
  123. postRender: function(control) {
  124. $alpacaRadioRequired.find('.radio').addClass('form-check');
  125. $alpacaRadioRequired.find('label:not(.alpaca-control-label)').addClass('form-check-label');
  126. $alpacaRadioRequired.find('input[type=radio]').addClass('form-check-input');
  127. }
  128. });
  129. // Options
  130. var $alpacaRadioOptions = $('#alpaca-radio-options');
  131. $alpacaRadioOptions.alpaca({
  132. data: 'Jimi Hendrix',
  133. schema: {
  134. enum: ['Jimi Hendrix', 'Mark Knopfler', 'Joe Satriani', 'Eddie Van Halen', 'Orianthi']
  135. },
  136. options: {
  137. type: 'radio',
  138. label: 'Who is your favorite guitarist?',
  139. removeDefaultNone: true,
  140. vertical: true
  141. },
  142. postRender: function(control) {
  143. $alpacaRadioOptions.find('.radio').addClass('form-check');
  144. $alpacaRadioOptions.find('label:not(.alpaca-control-label)').addClass('form-check-label');
  145. $alpacaRadioOptions.find('input[type=radio]').addClass('form-check-input');
  146. }
  147. });
  148. };
  149. // Alpaca with Uniform
  150. var _componentAlpacaControlsUniform = function() {
  151. if (!$().alpaca || !$().uniform) {
  152. console.warn('Warning - alpaca.min.js and/or uniform.min.js is not loaded.');
  153. return;
  154. }
  155. // Styled checkbox
  156. var $alpacaCheckboxStyled = $('#alpaca-checkbox-styled');
  157. $alpacaCheckboxStyled.alpaca({
  158. data: true,
  159. options: {
  160. label: 'Question:',
  161. rightLabel: 'Do you like Alpaca?'
  162. },
  163. postRender: function(control) {
  164. $alpacaCheckboxStyled.find('.checkbox').addClass('form-check');
  165. $alpacaCheckboxStyled.find('label:not(.alpaca-control-label)').addClass('form-check-label');
  166. $alpacaCheckboxStyled.find('input[type=checkbox]').addClass('form-check-input').uniform();
  167. }
  168. });
  169. // Disabled checkbox
  170. var $alpacaCheckboxStyledDisabled = $('#alpaca-checkbox-styled-disabled');
  171. $alpacaCheckboxStyledDisabled.alpaca({
  172. data: true,
  173. options: {
  174. label: 'Question:',
  175. rightLabel: 'Do you like Alpaca?',
  176. disabled: true
  177. },
  178. postRender: function(control) {
  179. $alpacaCheckboxStyledDisabled.find('.checkbox').addClass('form-check');
  180. $alpacaCheckboxStyledDisabled.find('label:not(.alpaca-control-label)').addClass('form-check-label');
  181. $alpacaCheckboxStyledDisabled.find('input[type=checkbox]').addClass('form-check-input').uniform();
  182. }
  183. });
  184. // Styled checkbox list
  185. var $alpacaCheckboxListStyled = $('#alpaca-checkbox-list-styled');
  186. $alpacaCheckboxListStyled.alpaca({
  187. data: ['sandwich', 'cookie', 'drink'],
  188. schema: {
  189. type: 'array',
  190. enum: ['sandwich', 'chips', 'cookie', 'drink']
  191. },
  192. options: {
  193. type: 'checkbox',
  194. label: 'What would you like with your order?',
  195. optionLabels: ['A Sandwich', 'Potato Chips', 'A Cookie', 'Soft Drink']
  196. },
  197. postRender: function(control) {
  198. $alpacaCheckboxListStyled.find('.checkbox').addClass('form-check');
  199. $alpacaCheckboxListStyled.find('label:not(.alpaca-control-label)').addClass('form-check-label');
  200. $alpacaCheckboxListStyled.find('input[type=checkbox]').addClass('form-check-input').uniform();
  201. }
  202. });
  203. // Styled radios
  204. var $alpacaRadioStyled = $('#alpaca-radio-styled');
  205. $alpacaRadioStyled.alpaca({
  206. data: 'Jimi Hendrix',
  207. schema: {
  208. enum: ['Jimi Hendrix', 'Mark Knopfler', 'Joe Satriani', 'Eddie Van Halen', 'Orianthi']
  209. },
  210. options: {
  211. type: 'radio',
  212. label: 'Who is your favorite guitarist?',
  213. vertical: true
  214. },
  215. postRender: function(control) {
  216. $alpacaRadioStyled.find('.radio').addClass('form-check');
  217. $alpacaRadioStyled.find('label:not(.alpaca-control-label)').addClass('form-check-label');
  218. $alpacaRadioStyled.find('input[type=radio]').addClass('form-check-input').uniform();
  219. }
  220. });
  221. // Disabled mode
  222. var $alpacaRadioStyledDisabled = $('#alpaca-radio-styled-disabled');
  223. $alpacaRadioStyledDisabled.alpaca({
  224. data: 'Jimi Hendrix',
  225. schema: {
  226. enum: ['Jimi Hendrix', 'Mark Knopfler', 'Joe Satriani', 'Eddie Van Halen', 'Orianthi']
  227. },
  228. options: {
  229. type: 'radio',
  230. label: 'Who is your favorite guitarist?',
  231. vertical: true,
  232. disabled: true
  233. },
  234. postRender: function(control) {
  235. $alpacaRadioStyledDisabled.find('.radio').addClass('form-check');
  236. $alpacaRadioStyledDisabled.find('label:not(.alpaca-control-label)').addClass('form-check-label');
  237. $alpacaRadioStyledDisabled.find('input[type=radio]').addClass('form-check-input').uniform();
  238. }
  239. });
  240. };
  241. // Alpaca with Switchery
  242. var _componentAlpacaControlsSwitchery = function() {
  243. if (!$().alpaca || typeof Switchery == 'undefined') {
  244. console.warn('Warning - alpaca.min.js and/or switchery.min.js is not loaded.');
  245. return;
  246. }
  247. // Switchery toggle
  248. var $alpacaSwitchery = $('#alpaca-switchery');
  249. $alpacaSwitchery.alpaca({
  250. data: true,
  251. options: {
  252. label: 'Question:',
  253. rightLabel: 'Do you like Alpaca?',
  254. fieldClass: 'switchery-demo'
  255. },
  256. postRender: function() {
  257. // Init Switchery
  258. var elems = Array.prototype.slice.call(document.querySelectorAll('.switchery-demo input[type=checkbox]'));
  259. elems.forEach(function(html) {
  260. var switchery = new Switchery(html);
  261. });
  262. $alpacaSwitchery.find('.checkbox').addClass('form-check form-check-switchery');
  263. $alpacaSwitchery.find('label:not(.alpaca-control-label)').addClass('form-check-label');
  264. }
  265. });
  266. // Disabled Switchery
  267. var $alpacaSwitcheryDisabled = $('#alpaca-switchery-disabled');
  268. $alpacaSwitcheryDisabled.alpaca({
  269. data: true,
  270. options: {
  271. label: 'Question:',
  272. rightLabel: 'Do you like Alpaca?',
  273. fieldClass: 'switchery-disabled-demo',
  274. disabled: true
  275. },
  276. postRender: function(control) {
  277. // Init Switchery
  278. var elems = Array.prototype.slice.call(document.querySelectorAll('.switchery-disabled-demo input[type=checkbox]'));
  279. elems.forEach(function(html) {
  280. var switchery = new Switchery(html);
  281. });
  282. $alpacaSwitcheryDisabled.find('.checkbox').addClass('form-check form-check-switchery');
  283. $alpacaSwitcheryDisabled.find('label:not(.alpaca-control-label)').addClass('form-check-label');
  284. }
  285. });
  286. };
  287. // Alpaca with Tokenfield
  288. var _componentAlpacaControlsTokenfield = function() {
  289. if (!$().alpaca || !$().tokenfield) {
  290. console.warn('Warning - alpaca.min.js and/or tokenfield.min.js is not loaded.');
  291. return;
  292. }
  293. // Basic setup
  294. $('#alpaca-tokenfield').alpaca({
  295. schema: {
  296. title: 'Character Names',
  297. type: 'string'
  298. },
  299. options: {
  300. type: 'token',
  301. focus: false,
  302. tokenfield: {
  303. autocomplete: {
  304. source: ['marty', 'doc', 'george', 'biff', 'lorraine', 'mr. strickland'],
  305. delay: 100
  306. },
  307. showAutocompleteOnFocus: true
  308. }
  309. },
  310. data: 'marty,doc,george,biff'
  311. });
  312. // Display only mode
  313. $('#alpaca-tokenfield-static').alpaca({
  314. schema: {
  315. title: 'Character Names',
  316. type: 'string'
  317. },
  318. options: {
  319. type: 'token',
  320. focus: false,
  321. tokenfield: {
  322. autocomplete: {
  323. source: ['marty', 'doc', 'george', 'biff', 'lorraine', 'mr. strickland'],
  324. delay: 100
  325. },
  326. showAutocompleteOnFocus: true
  327. }
  328. },
  329. data: 'marty,doc,george,biff',
  330. view: 'bootstrap-display'
  331. });
  332. };
  333. //
  334. // Return objects assigned to module
  335. //
  336. return {
  337. init: function() {
  338. _componentAlpacaControls();
  339. _componentAlpacaControlsUniform();
  340. _componentAlpacaControlsSwitchery();
  341. _componentAlpacaControlsTokenfield();
  342. }
  343. }
  344. }();
  345. // Initialize module
  346. // ------------------------------
  347. document.addEventListener('DOMContentLoaded', function() {
  348. AlpacaControls.init();
  349. });