alpaca_basic.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /* ------------------------------------------------------------------------------
  2. *
  3. * # Alpaca - Basic inputs
  4. *
  5. * Demo JS code for alpaca_basic.html page
  6. *
  7. * ---------------------------------------------------------------------------- */
  8. // Setup module
  9. // ------------------------------
  10. var AlpacaBasic = function() {
  11. //
  12. // Setup module components
  13. //
  14. // Alpaca examples
  15. var _componentAlpaca = function() {
  16. if (!$().alpaca) {
  17. console.warn('Warning - alpaca.min.js is not loaded.');
  18. return;
  19. }
  20. // Text input
  21. // ------------------------------
  22. // Basic example
  23. $('#alpaca-basic').alpaca({
  24. data: 'I Love Alpaca Ice Cream!',
  25. options: {
  26. focus: false
  27. }
  28. });
  29. // Display only view
  30. $('#alpaca-static').alpaca({
  31. data: 'I Love Alpaca Ice Cream!',
  32. schema: {
  33. type: 'string'
  34. },
  35. view: 'bootstrap-display'
  36. });
  37. // Input field label
  38. $('#alpaca-input-label').alpaca({
  39. data: 'I Love Alpaca Ice Cream!',
  40. options: {
  41. label: 'Input label',
  42. focus: false
  43. }
  44. });
  45. // Static input label
  46. $('#alpaca-static-label').alpaca({
  47. data: 'I Love Alpaca Ice Cream!',
  48. schema: {
  49. type: 'string'
  50. },
  51. options: {
  52. label: 'Input label'
  53. },
  54. view: 'bootstrap-display'
  55. });
  56. // Validation
  57. $('#alpaca-validation').alpaca({
  58. data: 'Mint',
  59. schema: {
  60. minLength: 3,
  61. maxLength: 5
  62. },
  63. options: {
  64. label: 'Ice Cream',
  65. helper: 'Your favorite ice cream?',
  66. size: 30,
  67. focus: false
  68. }
  69. });
  70. // Validation with predefined value
  71. $('#alpaca-validation-predefined').alpaca({
  72. data: 'Mint Chocolate',
  73. schema: {
  74. minLength: 3,
  75. maxLength: 5
  76. },
  77. options: {
  78. label: 'Ice Cream',
  79. helper: 'Please tell us the kind of ice cream you love most!',
  80. size: 30,
  81. focus: false,
  82. placeholder: 'Enter an ice cream flavor'
  83. }
  84. });
  85. // Disallow empty spaces
  86. $('#alpaca-disallow-empty').alpaca({
  87. schema: {
  88. type: 'string'
  89. },
  90. options: {
  91. type: 'lowercase',
  92. label: 'User Name',
  93. disallowEmptySpaces: true,
  94. helper: 'Type something with empty space',
  95. focus: false
  96. }
  97. });
  98. // Disallow values
  99. $('#alpaca-disallow-values').alpaca({
  100. data: 'Mickey Mantle',
  101. schema: {
  102. type: 'string',
  103. disallow: ['Mickey Mantle', 'Mickey']
  104. },
  105. options: {
  106. label: 'Name',
  107. focus: false
  108. }
  109. });
  110. // Typeahead integration
  111. $('#alpaca-typeahead').alpaca({
  112. schema: {
  113. type: 'string'
  114. },
  115. options: {
  116. type: 'text',
  117. label: 'Company Name',
  118. helper: 'Select the name of a computing company',
  119. placeholder: 'Enter "a"',
  120. focus: false,
  121. typeahead: {
  122. config: {
  123. highlight: true,
  124. hint: true,
  125. minLength: 1
  126. },
  127. datasets: {
  128. type: 'local',
  129. displayKey: 'value',
  130. source: ['Google', 'Amazon', 'Microsoft', 'Apple', 'Spotify', 'Alpaca', 'Another company', 'Facebook']
  131. }
  132. }
  133. }
  134. });
  135. // [RTL] Typeahead integration
  136. $('#alpaca-typeahead-rtl').alpaca({
  137. schema: {
  138. type: 'string'
  139. },
  140. options: {
  141. type: 'text',
  142. label: 'Company Name',
  143. helper: 'Select the name of a computing company',
  144. placeholder: 'Enter "a"',
  145. focus: false,
  146. fieldClass: 'typeahead-rtl',
  147. typeahead: {
  148. config: {
  149. highlight: true,
  150. hint: true,
  151. minLength: 1
  152. },
  153. datasets: {
  154. type: 'local',
  155. displayKey: 'value',
  156. source: ['Google', 'Amazon', 'Microsoft', 'Apple', 'Spotify', 'Alpaca', 'Another company', 'Facebook']
  157. }
  158. }
  159. },
  160. postRender: function() {
  161. $('.typeahead-rtl').find('.form-control').attr('dir', 'rtl');
  162. }
  163. });
  164. // Maxlength integration
  165. $('#alpaca-maxlength').alpaca({
  166. schema: {
  167. type: 'string',
  168. minLength: 3,
  169. maxLength: 25
  170. },
  171. options: {
  172. type: 'text',
  173. label: 'What is your name?',
  174. constrainMaxLength: true,
  175. constrainMinLength: true,
  176. showMaxLengthIndicator: true,
  177. focus: false
  178. },
  179. data: 'Jackie Robinson'
  180. });
  181. // Textareas
  182. // ------------------------------
  183. // Basic textarea
  184. $('#alpaca-textarea').alpaca({
  185. data: 'Ice cream or ice-cream is a frozen dessert usually made from dairy products, such as milk and cream, and often combined with fruits or other ingredients and flavours.',
  186. options: {
  187. type: 'textarea',
  188. label: 'Receipt',
  189. helper: 'Receipt for Best Homemade Ice Cream',
  190. rows: 4,
  191. cols: 80,
  192. focus: false
  193. }
  194. });
  195. // With placeholder
  196. $('#alpaca-textarea-placeholder').alpaca({
  197. options: {
  198. type: 'textarea',
  199. label: 'Receipt',
  200. helper: 'Receipt for Best Homemade Ice Cream',
  201. placeholder: 'Enter your favorite ice cream here...',
  202. rows: 4,
  203. cols: 80,
  204. focus: false
  205. }
  206. });
  207. // Display mode
  208. $('#alpaca-textarea-static').alpaca({
  209. data: 'Ice cream or ice-cream is a frozen dessert usually made from dairy products, such as milk and cream, and often combined with fruits or other ingredients and flavours.',
  210. options: {
  211. type: 'textarea',
  212. label: 'Receipt',
  213. rows: 6,
  214. cols: 80,
  215. focus: false
  216. },
  217. view: 'bootstrap-display'
  218. });
  219. // Single field render
  220. $('#alpaca-textarea-override').alpaca({
  221. data: 'My name is Dr. Jacobian and I am a neuroscientist from the Cornell University. I\'ve perfected a DNA transcription process which makes it possible for the first time to use DNA nucleotides to store pedabytes of information in real-time.',
  222. schema: {
  223. type: 'string'
  224. },
  225. options: {
  226. type: 'textarea',
  227. label: 'Tell us about yourself',
  228. view: 'bootstrap-display'
  229. }
  230. });
  231. // Selects
  232. // ------------------------------
  233. // Basic select
  234. $('#alpaca-select').alpaca({
  235. data: 'coffee',
  236. schema: {
  237. enum: ['vanilla', 'chocolate', 'coffee', 'strawberry', 'mint']
  238. },
  239. options: {
  240. label: 'Ice cream',
  241. helper: 'What flavor of ice cream do you prefer?',
  242. focus: false
  243. }
  244. });
  245. // External data source
  246. $('#alpaca-select-external').alpaca({
  247. options: {
  248. label: 'Ice cream',
  249. helper: 'Guess my favorite ice cream?',
  250. type: 'select',
  251. focus: false,
  252. dataSource: '../../../../global_assets/demo_data/alpaca/selects.json'
  253. }
  254. });
  255. };
  256. // Alpaca with Select2
  257. var _componentAlpacaSelect2 = function() {
  258. if (!$().alpaca || !$().select2) {
  259. console.warn('Warning - alpaca.min.js and/or select2.min.js is not loaded.');
  260. return;
  261. }
  262. // Select2 select
  263. $('#alpaca-select2').alpaca({
  264. data: 'coffee',
  265. schema: {
  266. enum: ['vanilla', 'chocolate', 'coffee', 'strawberry', 'mint']
  267. },
  268. options: {
  269. label: 'Ice cream',
  270. helper: 'What flavor of ice cream do you prefer?',
  271. id: 'select2-basic',
  272. focus: false
  273. },
  274. postRender: function(control) {
  275. $('#select2-basic').select2({
  276. minimumResultsForSearch: Infinity
  277. });
  278. }
  279. });
  280. // Select2 select with search
  281. $('#alpaca-select2-search').alpaca({
  282. data: 'coffee',
  283. schema: {
  284. enum: ['vanilla', 'chocolate', 'coffee', 'strawberry', 'mint']
  285. },
  286. options: {
  287. label: 'Ice cream',
  288. helper: 'What flavor of ice cream do you prefer?',
  289. id: 'select2-search',
  290. focus: false
  291. },
  292. postRender: function(control) {
  293. $('#select2-search').select2();
  294. }
  295. });
  296. };
  297. // Alpaca with Multiselect
  298. var _componentAlpacaMultiselect = function() {
  299. if (!$().alpaca || !$().multiselect) {
  300. console.warn('Warning - alpaca.min.js and/or bootstrap-multiselect.js is not loaded.');
  301. return;
  302. }
  303. // Multiselect
  304. $('#alpaca-multiselect').alpaca({
  305. data: ['Vanilla', 'Chocolate'],
  306. schema: {
  307. type: 'array',
  308. items: {
  309. title: 'Ice Cream',
  310. type: 'string',
  311. enum: ['Vanilla', 'Chocolate', 'Strawberry', 'Mint'],
  312. minItems: 2,
  313. maxItems: 3
  314. }
  315. },
  316. options: {
  317. label: 'Ice cream',
  318. helper: 'Guess my favorite ice cream?',
  319. type: 'select',
  320. size: 5,
  321. id: 'multiselect',
  322. focus: false
  323. }
  324. });
  325. // Multiselect with remote data
  326. $('#alpaca-multiselect-remote').alpaca({
  327. options: {
  328. label: 'Select your favorite flavor of ice cream',
  329. type: 'select',
  330. multiple: true,
  331. helper: 'Guess my favorite ice cream?',
  332. size: 3,
  333. focus: false,
  334. id: 'multiselect-remote',
  335. dataSource: '../../../../global_assets/demo_data/alpaca/selects.json'
  336. }
  337. });
  338. };
  339. //
  340. // Return objects assigned to module
  341. //
  342. return {
  343. init: function() {
  344. _componentAlpaca();
  345. _componentAlpacaSelect2();
  346. _componentAlpacaMultiselect();
  347. }
  348. }
  349. }();
  350. // Initialize module
  351. // ------------------------------
  352. document.addEventListener('DOMContentLoaded', function() {
  353. AlpacaBasic.init();
  354. });