123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395 |
- /* ------------------------------------------------------------------------------
- *
- * # Alpaca - Basic inputs
- *
- * Demo JS code for alpaca_basic.html page
- *
- * ---------------------------------------------------------------------------- */
- // Setup module
- // ------------------------------
- var AlpacaBasic = function() {
- //
- // Setup module components
- //
- // Alpaca examples
- var _componentAlpaca = function() {
- if (!$().alpaca) {
- console.warn('Warning - alpaca.min.js is not loaded.');
- return;
- }
- // Text input
- // ------------------------------
- // Basic example
- $('#alpaca-basic').alpaca({
- data: 'I Love Alpaca Ice Cream!',
- options: {
- focus: false
- }
- });
- // Display only view
- $('#alpaca-static').alpaca({
- data: 'I Love Alpaca Ice Cream!',
- schema: {
- type: 'string'
- },
- view: 'bootstrap-display'
- });
- // Input field label
- $('#alpaca-input-label').alpaca({
- data: 'I Love Alpaca Ice Cream!',
- options: {
- label: 'Input label',
- focus: false
- }
- });
- // Static input label
- $('#alpaca-static-label').alpaca({
- data: 'I Love Alpaca Ice Cream!',
- schema: {
- type: 'string'
- },
- options: {
- label: 'Input label'
- },
- view: 'bootstrap-display'
- });
- // Validation
- $('#alpaca-validation').alpaca({
- data: 'Mint',
- schema: {
- minLength: 3,
- maxLength: 5
- },
- options: {
- label: 'Ice Cream',
- helper: 'Your favorite ice cream?',
- size: 30,
- focus: false
- }
- });
- // Validation with predefined value
- $('#alpaca-validation-predefined').alpaca({
- data: 'Mint Chocolate',
- schema: {
- minLength: 3,
- maxLength: 5
- },
- options: {
- label: 'Ice Cream',
- helper: 'Please tell us the kind of ice cream you love most!',
- size: 30,
- focus: false,
- placeholder: 'Enter an ice cream flavor'
- }
- });
- // Disallow empty spaces
- $('#alpaca-disallow-empty').alpaca({
- schema: {
- type: 'string'
- },
- options: {
- type: 'lowercase',
- label: 'User Name',
- disallowEmptySpaces: true,
- helper: 'Type something with empty space',
- focus: false
- }
- });
- // Disallow values
- $('#alpaca-disallow-values').alpaca({
- data: 'Mickey Mantle',
- schema: {
- type: 'string',
- disallow: ['Mickey Mantle', 'Mickey']
- },
- options: {
- label: 'Name',
- focus: false
- }
- });
- // Typeahead integration
- $('#alpaca-typeahead').alpaca({
- schema: {
- type: 'string'
- },
- options: {
- type: 'text',
- label: 'Company Name',
- helper: 'Select the name of a computing company',
- placeholder: 'Enter "a"',
- focus: false,
- typeahead: {
- config: {
- highlight: true,
- hint: true,
- minLength: 1
- },
- datasets: {
- type: 'local',
- displayKey: 'value',
- source: ['Google', 'Amazon', 'Microsoft', 'Apple', 'Spotify', 'Alpaca', 'Another company', 'Facebook']
- }
- }
- }
- });
- // [RTL] Typeahead integration
- $('#alpaca-typeahead-rtl').alpaca({
- schema: {
- type: 'string'
- },
- options: {
- type: 'text',
- label: 'Company Name',
- helper: 'Select the name of a computing company',
- placeholder: 'Enter "a"',
- focus: false,
- fieldClass: 'typeahead-rtl',
- typeahead: {
- config: {
- highlight: true,
- hint: true,
- minLength: 1
- },
- datasets: {
- type: 'local',
- displayKey: 'value',
- source: ['Google', 'Amazon', 'Microsoft', 'Apple', 'Spotify', 'Alpaca', 'Another company', 'Facebook']
- }
- }
- },
- postRender: function() {
- $('.typeahead-rtl').find('.form-control').attr('dir', 'rtl');
- }
- });
- // Maxlength integration
- $('#alpaca-maxlength').alpaca({
- schema: {
- type: 'string',
- minLength: 3,
- maxLength: 25
- },
- options: {
- type: 'text',
- label: 'What is your name?',
- constrainMaxLength: true,
- constrainMinLength: true,
- showMaxLengthIndicator: true,
- focus: false
- },
- data: 'Jackie Robinson'
- });
- // Textareas
- // ------------------------------
- // Basic textarea
- $('#alpaca-textarea').alpaca({
- 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.',
- options: {
- type: 'textarea',
- label: 'Receipt',
- helper: 'Receipt for Best Homemade Ice Cream',
- rows: 4,
- cols: 80,
- focus: false
- }
- });
- // With placeholder
- $('#alpaca-textarea-placeholder').alpaca({
- options: {
- type: 'textarea',
- label: 'Receipt',
- helper: 'Receipt for Best Homemade Ice Cream',
- placeholder: 'Enter your favorite ice cream here...',
- rows: 4,
- cols: 80,
- focus: false
- }
- });
- // Display mode
- $('#alpaca-textarea-static').alpaca({
- 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.',
- options: {
- type: 'textarea',
- label: 'Receipt',
- rows: 6,
- cols: 80,
- focus: false
- },
- view: 'bootstrap-display'
- });
- // Single field render
- $('#alpaca-textarea-override').alpaca({
- 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.',
- schema: {
- type: 'string'
- },
- options: {
- type: 'textarea',
- label: 'Tell us about yourself',
- view: 'bootstrap-display'
- }
- });
- // Selects
- // ------------------------------
- // Basic select
- $('#alpaca-select').alpaca({
- data: 'coffee',
- schema: {
- enum: ['vanilla', 'chocolate', 'coffee', 'strawberry', 'mint']
- },
- options: {
- label: 'Ice cream',
- helper: 'What flavor of ice cream do you prefer?',
- focus: false
- }
- });
- // External data source
- $('#alpaca-select-external').alpaca({
- options: {
- label: 'Ice cream',
- helper: 'Guess my favorite ice cream?',
- type: 'select',
- focus: false,
- dataSource: '../../../../global_assets/demo_data/alpaca/selects.json'
- }
- });
- };
- // Alpaca with Select2
- var _componentAlpacaSelect2 = function() {
- if (!$().alpaca || !$().select2) {
- console.warn('Warning - alpaca.min.js and/or select2.min.js is not loaded.');
- return;
- }
- // Select2 select
- $('#alpaca-select2').alpaca({
- data: 'coffee',
- schema: {
- enum: ['vanilla', 'chocolate', 'coffee', 'strawberry', 'mint']
- },
- options: {
- label: 'Ice cream',
- helper: 'What flavor of ice cream do you prefer?',
- id: 'select2-basic',
- focus: false
- },
- postRender: function(control) {
- $('#select2-basic').select2({
- minimumResultsForSearch: Infinity
- });
- }
- });
- // Select2 select with search
- $('#alpaca-select2-search').alpaca({
- data: 'coffee',
- schema: {
- enum: ['vanilla', 'chocolate', 'coffee', 'strawberry', 'mint']
- },
- options: {
- label: 'Ice cream',
- helper: 'What flavor of ice cream do you prefer?',
- id: 'select2-search',
- focus: false
- },
- postRender: function(control) {
- $('#select2-search').select2();
- }
- });
- };
- // Alpaca with Multiselect
- var _componentAlpacaMultiselect = function() {
- if (!$().alpaca || !$().multiselect) {
- console.warn('Warning - alpaca.min.js and/or bootstrap-multiselect.js is not loaded.');
- return;
- }
- // Multiselect
- $('#alpaca-multiselect').alpaca({
- data: ['Vanilla', 'Chocolate'],
- schema: {
- type: 'array',
- items: {
- title: 'Ice Cream',
- type: 'string',
- enum: ['Vanilla', 'Chocolate', 'Strawberry', 'Mint'],
- minItems: 2,
- maxItems: 3
- }
- },
- options: {
- label: 'Ice cream',
- helper: 'Guess my favorite ice cream?',
- type: 'select',
- size: 5,
- id: 'multiselect',
- focus: false
- }
- });
- // Multiselect with remote data
- $('#alpaca-multiselect-remote').alpaca({
- options: {
- label: 'Select your favorite flavor of ice cream',
- type: 'select',
- multiple: true,
- helper: 'Guess my favorite ice cream?',
- size: 3,
- focus: false,
- id: 'multiselect-remote',
- dataSource: '../../../../global_assets/demo_data/alpaca/selects.json'
- }
- });
- };
- //
- // Return objects assigned to module
- //
- return {
- init: function() {
- _componentAlpaca();
- _componentAlpacaSelect2();
- _componentAlpacaMultiselect();
- }
- }
- }();
- // Initialize module
- // ------------------------------
- document.addEventListener('DOMContentLoaded', function() {
- AlpacaBasic.init();
- });
|