JAVASCRIPT   9

picker.js

Guest on 24th May 2021 03:45:08 AM

  1. /*!
  2.  * pickadate.js v3.5.4, 2014/09/11
  3.  * By Amsul, http://amsul.ca
  4.  * Hosted on http://amsul.github.io/pickadate.js
  5.  * Licensed under MIT
  6.  */
  7.  
  8. (function ( factory ) {
  9.  
  10.     // AMD.
  11.     if ( typeof define == 'function' && define.amd )
  12.         define( 'picker', ['jquery'], factory )
  13.  
  14.     // Node.js/browserify.
  15.     else if ( typeof exports == 'object' )
  16.         module.exports = factory( require('jquery') )
  17.  
  18.     // Browser globals.
  19.     else this.Picker = factory( jQuery )
  20.  
  21. }(function( $ ) {
  22.  
  23. var $window = $( window )
  24. var $document = $( document )
  25. var $html = $( document.documentElement )
  26.  
  27.  
  28. /**
  29.  * The picker constructor that creates a blank picker.
  30.  */
  31. function PickerConstructor( ELEMENT, NAME, COMPONENT, OPTIONS ) {
  32.  
  33.     // If there’s no element, return the picker constructor.
  34. tor.
  35.     if ( !ELEMENT ) return PickerConstructor
  36.  
  37.  
  38.     var
  39.         IS_DEFAULT_THEME = false,
  40.  
  41.  
  42.    // The state of the picker.
  43. ker.
  44.         STATE = {
  45.             id: ELEMENT.id || 'P' + Math.abs( ~~(Math.random() * new Date()) )
  46.         },
  47.  
  48.  
  49.    // Merge the defaults and options passed.
  50. sed.
  51.         SETTINGS = COMPONENT ? $.extend( true, {}, COMPONENT.defaults, OPTIONS ) : OPTIONS || {},
  52.  
  53.  
  54.    // Merge the default classes with the settings classes.
  55. ses.
  56.         CLASSES = $.extend( {}, PickerConstructor.klasses(), SETTINGS.klass ),
  57.  
  58.  
  59.    // The element node wrapper into a jQuery object.
  60. ect.
  61.         $ELEMENT = $( ELEMENT ),
  62.  
  63.  
  64.    // Pseudo picker constructor.
  65. tor.
  66.         PickerInstance = function() {
  67.             return this.start()
  68.         },
  69.  
  70.  
  71.    // The picker prototype.
  72. ype.
  73.         P = PickerInstance.prototype = {
  74.  
  75.             constructor: PickerInstance,
  76.  
  77.             $node: $ELEMENT,
  78.  
  79.  
  80.        /**
  81.              * Initialize everything
  82.              */   */
  83.             start: function() {
  84.  
  85.            // If it’s already started, do nothing.
  86.  nothing.
  87.                 if ( STATE && STATE.start ) return P
  88.  
  89.  
  90.       // Update the picker states.
  91. r states.
  92.                 STATE.methods = {}
  93.                 STATE.start = true
  94.                 STATE.open = false
  95.                 STATE.type = ELEMENT.type
  96.  
  97.  
  98.       // Confirm focus state, convert into text input to remove UA stylings,
  99. stylings,
  100.       // and set as readonly to prevent keyboard popup.
  101. rd popup.
  102.                 ELEMENT.autofocus = ELEMENT == document.activeElement
  103.                 ELEMENT.readOnly = !SETTINGS.editable
  104.                 ELEMENT.id = ELEMENT.id || STATE.id
  105.                 if ( ELEMENT.type != 'text' ) {
  106.                     ELEMENT.type = 'text'
  107.                 }
  108.  
  109.  
  110.       // Create a new picker component with the settings.
  111. settings.
  112.                 P.component = new COMPONENT(P, SETTINGS)
  113.  
  114.  
  115.       // Create the picker root with a holder and then prepare it.
  116. epare it.
  117.                 P.$root = $( PickerConstructor._.node('div', createWrappedComponent(), CLASSES.picker, 'id="' + ELEMENT.id + '_root"') )
  118.                 prepareElementRoot()
  119.  
  120.  
  121.       // If there’s a format for the hidden input element, create the element.
  122. e the element.
  123.                 if ( SETTINGS.formatSubmit ) {
  124.                     prepareElementHidden()
  125.                 }
  126.  
  127.  
  128.  // Prepare the input element.
  129. input element.
  130.                 prepareElement()
  131.  
  132.  
  133.  // Insert the root as specified in the settings.
  134.  the settings.
  135.                 if ( SETTINGS.container ) $( SETTINGS.container ).append( P.$root )
  136.                 else $ELEMENT.after( P.$root )
  137.  
  138.  
  139.  // Bind the default component and settings events.
  140. ttings events.
  141.                 P.on({
  142.                     start: P.component.onStart,
  143.                     render: P.component.onRender,
  144.                     stop: P.component.onStop,
  145.                     open: P.component.onOpen,
  146.                     close: P.component.onClose,
  147.                     set: P.component.onSet
  148.                 }).on({
  149.                     start: SETTINGS.onStart,
  150.                     render: SETTINGS.onRender,
  151.                     stop: SETTINGS.onStop,
  152.                     open: SETTINGS.onOpen,
  153.                     close: SETTINGS.onClose,
  154.                     set: SETTINGS.onSet
  155.                 })
  156.  
  157.  
  158.  // Once we’re all set, check the theme in use.
  159. k the theme in use.
  160.                 IS_DEFAULT_THEME = isUsingDefaultTheme( P.$root.children()[ 0 ] // If the element has autofocus, open the picker.
  161. s, open the picker.
  162.                 if ( ELEMENT.autofocus ) {
  163.                     P.open()
  164.                 // Trigger queued the “start” and “render” events.
  165.  and “render” events.
  166.                 return P.trigger( 'start' )//start
  167. ( 'render' )
  168.  /**
  169.              * Render a new picker
  170.              */ Render a new picker
  171.              */
  172.             render: function( e// Insert a new component holder in the root or box.
  173. omponent holder in the root or box.
  174.                 if ( entireComponent ) P.$root.html( createWrappedComponent() )
  175.                 else P.$root.find( '.' + CLASSES.box ).html( P.component.nod// Trigger the queued “render” events.
  176. ger the queued “render” events.
  177.                 //render
  178. trigger( 'rend/**
  179.              * Destroy everything
  180.              */        * Destroy everything
  181.              */
  182.     // If it’s already stopped, do nothing.
  183.   // If it’s already stopped, do nothing.
  184.              // Then close the picker.
  185. urn P
  186.  
  187.                 // Then close the pi// Remove the hidden field.
  188. e()
  189.  
  190.                 // Remove the hidden field.
  191.                 if ( P._hidden ) {
  192.                     P._hidden.parentNode.removeChild// Remove the root.
  193.           }
  194.  
  195.                 // Remove the root.
  196.  // Remove the input class, remove the stored data, and unbind
  197.  input class, re// the events (after a tick for IE - see `P.close`).
  198. he events (after a tick for IE - see `P.close`).
  199.                 $ELEMENT.removeClass( CLASSES.input ).removeData( NAME )
  200.                 setTimeout( function() {
  201.                     $ELEMENT.off( '.' + S// Restore the element state
  202. 0)
  203.  
  204.                 // Restore the element state
  205.                 ELEMENT.type = STATE.type
  206.          // Trigger the queued “stop” events.
  207.         // Trigger the queued “stop” even// Reset the picker states.
  208. r( 'stop' )
  209.  
  210.                 // Reset the picker states.
  211.                 STATE.methods = {}
  212.                 STAT//stop
  213.  = false
  214.  
  215.     /**
  216.              * Open up the picker
  217.              */    /**
  218.              * Open up the picker
  219.              */
  220.       // If it’s already open, do nothing.
  221.  
  222.  
  223.                 // If it’s already open, do nothing// Add the “active” class.
  224. en ) return P
  225.  
  226.                 // Add the “active” class.
  227.                 $ELEMENT.addClass( CLASSES.active// * A Firefox bug, when `html` has `overflow:hidden`, results in
  228.     // * A Firef//   killing transitions :(. So add the “opened” state on the next tick.
  229. lling transition//   Bug: https://bugzilla.mozilla.org/show_bug.cgi?id=625289
  230.                 //   Bug: https://bugzilla.mozilla.org/show_bu// Add the “opened” class to the picker root.
  231. on() {
  232.  
  233.                     // Add the “opened” class to the picker root.
  234.                     P.$root.addClass( CLASSES.opened )
  235.            // If we have to give focus, bind the element and doc events.
  236. }, 0 )
  237.  
  238.                 // If we have to give focus, bind the element // Set it as open.
  239.              if ( dontGiveFocus !== false ) {
  240.  
  241.             // Prevent the page from scrolling.
  242.            STATE.open = true
  243.  
  244.                     // Prevent the page from scrolling.
  245.                     if ( IS_DEFAULT_THEME ) {
  246.                         $html.
  247.                             css( 'overflow', 'hidden' ).
  248.                             css( 'pa// Pass focus to the element’s jQuery object.
  249.                }
  250.  
  251.                     // Pass focus to the elementâ// Bind the document events.
  252.              $ELEMENT.trigger( 'focus' )
  253.  
  254.                     // Bind the document events.
  255.                     $document.on( 'click.' + STATE.id + ' focusin.' + STATE.id, funct// If the target of the event is not the element, close the picker picker.
  256.                 // If th// * Don’t worry about clicks or focusins on the root because those don’t bubble up.
  257.   // * Don’t worr//   Also, for Firefox, a click on an `option` element bubbles up directly
  258. .
  259.                       //   to the doc. So make sure the target wasn't the doc.
  260. bubbles up directly
  261.     // * In Firefox stopPropagation() doesn’t prevent right-click events from bubbling,
  262.                // * In F//   which causes the picker to unexpectedly close when right-clicking it. So make
  263.                        ///   sure the event wasn’t a right-click.
  264. close when right-clicking it. So make
  265.                         //   sure the event wasn’t a right-click.
  266.            // If the target was the holder that covers the screen,
  267. t && event.which != 3 ) {
  268.  
  269.  // keep the element focused to maintain tabindex.
  270. e holder that covers the screen,
  271.                             // keep the element focused to maintain tabindex.
  272.                             P.close( target === P.$root.children()[0] )
  273.                         }
  274.  
  275.                 // Get the keycode.
  276. ' + STATE.id, function( event ) {
  277.  
  278.                         var
  279.                    // Translate that to a selection change.
  280.                 keycode = event.keyCode,
  281.  
  282.                             // Translate that to a selectio// Grab the target.
  283.                   keycodeToMove = P.component.key[ keycode ],
  284.  
  285.              // On escape, close the picker and give focus.
  286.                 target = event.target
  287.  
  288.  
  289.                         // On escape, close the picker and give focus.
  290.                         if ( key// Check if there is a key movement or “enter” keypress on the element.
  291.        }
  292.  
  293.  
  294.                         // Check if there is a key movement or “enter” keypress on the element.
  295.     // Prevent the default action to stop page movement.
  296. keycodeToMove || keycode == 13 ) ) {
  297.  
  298.                             // Prevent the// Trigger the key movement action.
  299. t.
  300.                             event.preventDefault()
  301.  
  302.                             // Trigger the key movement action.
  303.                             if ( keycodeToMove ) {
  304.                                 PickerConstructor._.trigger( P.component.ke// On “enter”, if the highlighted item isn’t disabled, set the value and close.
  305.   }
  306.  
  307.                             // On “enter”, if the highlighted item isn’t disabled, set the value and close.
  308.                             else if ( !P.$root.find( '.' + CLASSES.highlighted ).hasClass( CLASSES.disabled ) ) {
  309.                                 P.se// If the target is within the root and “enter” is pressed,
  310.               }
  311.         // prevent the default action and trigger a click on the target instead.
  312. e root and “enter” is pressed,
  313.                         // prevent the default action and trigger a click on the target instead.
  314.                         else if ( $.contains( P.$root[0], target ) && keycode == 13 ) {
  315.                             event.prev// Trigger the queued “open” events.
  316. target.click()
  317.                         }
  318.                  //open
  319.               /**
  320.              * Close the picker
  321.              */en” events.
  322.                 return P.trigger( 'open' )
  323. // If we need to give focus, do it before changing states.
  324. e the picker
  325.              */
  326.             close: functio// ....ah yes! It would’ve been incomplete without a crazy workaround for IE :|
  327. ng states.
  328.          // The focus is triggered *after* the close has completed - causing it
  329. €™ve been incomp// to open again. So unbind and rebind the event at the next tick.
  330. e focus is triggered *after* the close has completed - causing it
  331.                     // to open again. So unbind and rebind the event at the next tick.
  332.                     $ELEMENT.off( 'focus.' + STATE.id ).trigger( 'focus' )
  333.                     setTim// Remove the “active” class.
  334.          $ELEMENT.on( 'focus.' + STATE.id, focusToOpen )
  335.                     }, 0 )
  336.                 }
  337.  
  338.                 // R// * A Firefox bug, when `html` has `overflow:hidden`, results in
  339. oveClass( CLASSE//   killing transitions :(. So remove the “opened” state on the next tick.
  340. / * A Firefox bu//   Bug: https://bugzilla.mozilla.org/show_bug.cgi?id=625289
  341.    //   killing transitions :(. So remove the “openedâ// Remove the “opened” and “focused” class from the picker root.
  342. ozilla.org/show_bug.cgi?id=625289
  343.                 setTimeout( function() {
  344.  
  345.                     // Remove the “opened” and “focused” class from the picker// If it’s already closed, do nothing more.
  346. ( CLASSES.opened + ' ' + CLASSES.focused )
  347.                   // Set it as closed.
  348. hidden', true )
  349.  
  350.                 }, 0 )
  351.  
  352.            // Allow the page to scroll.
  353. y closed, do nothing more.
  354.                 if ( !STATE.open ) return P
  355.  
  356.                 // Set it as closed.
  357.                 STATE.open = false
  358.  
  359.                 // Allow the page to scroll.
  360.                 if ( IS_DEFAULT_THEM// Unbind the document events.
  361. .
  362.                         css( '.'erflow', '' ).
  363.                 // Trigger the queued “close” events.
  364. rollbarWidth() )
  365.                 }
  366.  
  367.                 // Unbi//close
  368. ocument events/**
  369.              * Clear the values
  370.              */                // Trigger the queued “close” events.
  371.                 return P.trigger( 'close' )
  372.   //clear
  373.   }, //close
  374.  
  375. /**
  376.              * Set something
  377.              */
  378.              */
  379.             clear: function( options ) {
  380.                 return P.set( 'clear', null, options )
  381.             }, //clear
  382.  
  383.  
  384.             /**
  385.              * Set something
  386.              */
  387.             set: function( thing, value// Make sure we have usable options.
  388. ingItem, thingValue,
  389.                     thingIsObject = $.isPlainObject( thing ),
  390.                     thingObject = thingIsObject ? thing : {}
  391.  
  392. // If the thing isn’t an object, make it one.
  393. ons.
  394.                 options = thingIsObject && $.isPlainObject( value ) ? value : options || {}
  395.  
  396.                 if ( thing ) {
  397.  
  398.           // Go through the things of items to set.
  399. object, make it one.
  400.                     if ( !thingIsObject ) {
  401.                // Grab the value of the thing.
  402. value
  403.                     }
  404.  
  405.                     // Go through the things of items to s// First, if the item exists and there’s a value, set it.
  406.                         // Grab the value of the thing.
  407.                         thingValue = thingObject[ thingItem ]
  408.  
  409.                         // First, if the item exists and there’s a value, set it.
  410.                         if ( thingItem in P.component.item ) {// Then, check to update the element value and broadcast a change.
  411. alue = null
  412.                             P.component.set( thingItem, thingValue, options )
  413.                         }
  414.  
  415.                         // Then, check to update the element value and broadcast a change.
  416.                         if ( thingItem == 'select' || thingItem == 'clear' ) {
  417.                             $ELEMENT.
  418.                  // Render a new picker.
  419. gItem == 'clear' ? '' : P.get( thingItem, SETTINGS.format ) ).
  420.    // When the method isn’t muted, trigger queued “set” events and pass the `thingObject`.
  421.  }
  422.  
  423.                     // Render a new picker.
  424.                     P.render()
  425.            //set
  426.  
  427.  
  428.             /**
  429.              * Get something
  430.              */er queued “set” events and pass the `thingObject`.
  431. // Make sure there’s something to get.
  432. : P.trigger( 'set', thingObject )
  433.             }, //set
  434.  
  435.  
  436.  // If a picker state exists, return that.
  437. g
  438.              */
  439.             get: function( thing, format ) {
  440.  
  441.                 // Make sure there’s something to get.// Return the value, if that.
  442.  || 'value'
  443.  
  444.                 // If a picker state exists, return that.
  445.                 if ( STATE[ thing ] != null ) {// Check if a component item exists, return that.
  446.          }
  447.  
  448.                 // Return the value, if that.
  449.                 if ( thing == 'value' ) {
  450.                     return ELEMENT.value
  451.                 }
  452.  
  453.                 // Check if a component item exists, return that.
  454.                 if ( thing in P.component.item ) {
  455.                     if ( typeof format == 'string' ) {
  456.                         var thingValue = P.component.get( thing )
  457.                         return thingValue ?
  458.                             PickerConstructor._.trigger(
  459.                                 P.component.formats.toString,
  460.                 //get
  461.           P.com/**
  462.              * Bind events on the things.
  463.              */]
  464.                             ) : ''
  465.                     }
  466.                     return P.component.get( thing )
  467.                 }
  468.             }, //get
  469.  
  470.  
  471.  
  472.             /**
  473.              * Bind events on the things.
  474.              */
  475.             on: function( thing, method, internal// If the thing isn’t an object, make it one.
  476. ,
  477.                     thingIsObject = $.isPlainObject( thing ),
  478.                     thingObject = thingIsObject ? thing : {}
  479.  
  480.                // Go through the things to bind to.
  481. // If the thing isn’t an object, make it one.
  482.                     if ( !t// Grab the method of the thing.
  483.         thingObject[ thing ] = method
  484.                     }
  485.  
  486.                     // Go t// If it was an internal binding, prefix it.
  487.      for ( thingName in thingObject ) {
  488.  
  489.                         // Grab the method of the thing.
  490.                         thingMethod = thingObject[ // Make sure the thing methods collection exists.
  491. an internal binding, prefix it.
  492.                         if ( internal ) {
  493.                             thingName// Add the method to the relative method collection.
  494.                  // Make sure the thing methods collection exists.
  495.                         STATE.methods[ thingName ] = STATE.methods[ thingName ] || []//on
  496.                /**
  497.              * Unbind events on the things.
  498.              */                    STATE.methods[ thingName ].push( thingMethod )
  499.                     }
  500.                 }
  501.  
  502.                 return P
  503.             }, //on
  504.  
  505.  
  506.  
  507.             /**
  508.              * Unbind events on the things.
  509.              */
  510.             off: function() {
  511.                 var i, thingName,
  512.                     names = arguments;
  513.                 for ( i = 0, namesCount = names.length; i < namesCount; i += 1 ) {
  514.                 /**
  515.              * Fire off method events.
  516.              */e in STATE.methods ) {
  517.                         delete STATE.methods[thingName]
  518.                     }
  519.                 }
  520.                 return P
  521.             },
  522.  
  523.  
  524.             /**
  525.              * Fire off method events.
  526.              */
  527.             trigger: function( name, data ) {
  528.                 var _trigger = function( name ) {
  529.                     var methodList = STATE.methods[ name ]
  530.                     if ( methodList ) {
  531.                         methodList.map( function( method ) {
  532.                           //trigger
  533. nstructor.//PickerInstance.prototype
  534. a ] )
  535. /**
  536.      * Wrap the picker holder components together.
  537.      */   }
  538.                 _trigger( '_' + name )
  539.       // Create a picker wrapper holder
  540.          return P
  541.             } //trigger
  542.         } //PickerI// Create a picker wrapper node
  543.  * Wrap the picker holder components together.
  544.      */
  545.     fun// Create a picker frame
  546. ent() {
  547.  
  548.         // Create a picker wrapper holder
  549.         return Pick// Create a picker box node
  550.  
  551.  
  552.             // Create a picker wrapper node
  553.             PickerConstructor._.// Create the components nodes.
  554.  Create a picker frame
  555.                 PickerConstructor._.node( 'div',
  556.  
  557.          // The picker box class
  558. icker box node
  559.                     PickerConstructor._.node( 'div',
  560.  
  561.            // Picker wrap class
  562. e the components nodes.
  563.                         P.component.nodes( ST// Picker frame class
  564.                // The picker box class
  565.                    // Picker holder class
  566.               ),
  567.  
  568.                    //endreturn
  569. rap cl//createWrappedComponent
  570. LASSES./**
  571.      * Prepare the input element with all bindings.
  572.      */                CLASSES.frame
  573.             ),
  574.  
  575.             // Pick// Store the picker data by component name.
  576.        ) //endreturn
  577.     } //createWrapp// Add the “input” class name.
  578. re the input element with all bindings.
  579.      */
  580.   // If there’s a `data-value`, update the value of the element.
  581. Store the picker data by component name.
  582.             data(NAME, P).
  583.  
  584.             // Add the “input” class name.
  585.             addClass(CLASSE// On focus/click, open the picker and adjust the root “focused” state.
  586.  of the element.
  587.             val( $ELEMENT.data('value') ?
  588.                 P.get('// Only bind keydown events if the element isn’t editable.
  589.          ).
  590.  
  591.             // On focus/click, open // Handle keyboard event based on the picker being opened or not.
  592.       on('focus.' + STATE.id + ' click.' + STATE.id, focusToOpen)
  593.  
  594.  
  595.         // Only bind keydown events if the element isn’t // Check if one of the delete keys was pressed.
  596.            // Handle keyboard event based on the picker being opened or not.
  597.    // For some reason IE clears the input value on “escape”.
  598.  
  599.                 var keycode = event.keyCode,
  600.  
  601.                     // Check if one of the delete keys was pressed.
  602.                     is// Check if `space` or `delete` was pressed or the picker is closed with a key movement.
  603. e input value on “escape”.
  604.                 if ( keycode == 27 ) {
  605.                     P.close()
  606.               // Prevent it from moving the page and bubbling to doc.
  607.  Check if `space` or `delete` was pressed or the picker is closed with a key movement.
  608.                 if ( // If `delete` was pressed, clear the values and close the picker.
  609. keycode] ) {
  610.  
  611.       // Otherwise open the picker.
  612. om moving the page and bubbling to doc.
  613.                     event.preventDefault()
  614.                     event.stopPropagation()
  615.  
  616.                     // If `d// Update the aria attributes.
  617. alues and close the picker.
  618.                     // Otherwise open the picker.
  619.                     if ( isKeycodeDelete ) { P.clear().close() }
  620.                     else { P.open() }
  621.                 }
  622.             }/**
  623.      * Prepare the root picker element with all bindings.
  624.      */MENT, {
  625.             haspopup: true,
  626.             expanded: false,
  627.             readonly: fal// When something within the root is focused, stop from bubbling
  628. ._hidden.id : ''// to the doc and remove the “focused” state from the root.
  629. ent with all bindings.
  630.      */
  631.     function prepareElementRoot() {
  632.  
  633.         P.$root.
  634.  
  635.             on({
  636.  
  637.                 // When something within the root is focused, stop from bubbling// When something within the root holder is clicked, stop it
  638. € state fro// from bubbling to the doc.
  639. ocusin: function( event ) {
  640.                     P.$root.removeClass( CLASSES.focused )
  641.                     event.stopPropag// Make sure the target isn’t the root holder so it can bubble up.
  642. the root holder is clicked, stop it
  643.                 // from bubbling to the doc.
  644.                 'mousedown click': function( event ) {
  645.  
  646. // * For mousedown events, cancel the default action in order to
  647.   // Make sure the targe//   prevent cases where focus is shifted onto external elements
  648.        if ( target != P.//   when using things like jQuery mobile or MagnificPopup (ref: #249 & #120).
  649.                      // //   Also, for Firefox, don’t prevent action on the `option` element.
  650.               //   prevent cases where focus is shifted onto external elements
  651.                         //   when using things like jQuery mobile or MagnificPopup (ref: #249 & #120).
  652.                     // Re-focus onto the element so that users can click away
  653.  the `option` element.
  654.      // from elements focused within the picker.
  655. down' && !$( target ).is( ':input' ) && target.nodeName != 'OPTION' ) {
  656.  
  657.                             event.preventDefault()
  658.  
  659.               // If there’s a click on an actionable element, carry out the actions.
  660.                            // from elements focused within the picker.
  661.                             ELEMENT.focus()
  662.                         }
  663.                     }
  664.                 }
  665.             }).
  666.  
  667.             // If there’s a click on an actionable element, carry out the actions.
  668.             on( 'click', '[data-pic// * For IE, non-focusable elements can be active elements as well
  669.         var $target //   (http://stackoverflow.com/a/2684561).
  670.  = $target.data(),
  671.                     targetDisabled = $target.hasClass( CLASSES.navDisabled ) || $target.hasClass( CLASSES.disabled ),
  672.  
  673.                     // * For IE, non-focusable elemen// If it’s disabled or nothing inside is actively focused, re-focus the element.
  674. m/a/2684561).
  675.                     activeElement = document.activeElement
  676.                     activeElement = activeElement && ( activeElement.type || activeElement.href ) &// If something is superficially changed, update the `highlight` based on the `nav`.
  677. vely focused, re-focus the element.
  678.                 if ( targetDisabled || activeElement && !$.contains( P.$root[0], activeElement ) ) {
  679.                     ELEMENT.focus()
  680.                // If something is picked, set `select` then close with focus.
  681. pdate the `highlight` based on the `nav`.
  682.                 if ( !targetDisabled && targetData.nav ) {
  683.                     P.set( 'highlight', P.component.item.highlight, { nav// If a “clear” button is pressed, empty the values and close with focus.
  684. ked, set `select` then close with focus.
  685.                 else if ( !targetDisabled && 'pick' in targetData ) {
  686.                     P.set( 'select', targetData.pick ).close( true )
  687.                 }
  688.  
  689.                 // If a â//P.$root
  690. € button is pressed, empty the values and close with /**
  691.       * Prepare the hidden input element along with all bindings.
  692.       */ear().close( true )
  693.                 }
  694.  
  695.                 else if ( targetData.close ) {
  696.                     P.close( true )
  697.                 }
  698.  
  699.             }) //P.$root
  700.  
  701.         aria( P.$root[0], 'hidden', true )
  702.     }
  703.  
  704.  
  705.      /**
  706.       * Prepare the hidden input element along with all bindings.
  707.       */
  708.     function prepareElementHidden() {
  709.  
  710.         var name
  711.  
  712.         if ( SETTINGS.hiddenName === true ) {
  713.             name = ELEMENT.name
  714.             ELEMENT.name = ''
  715.         }
  716.         else {
  717.             name = [
  718.                 typeof SETTINGS.hiddenPrefix // Create the name using the original input’s with a prefix and suffix.
  719. hiddenSuffix == 'string' ? SETTINGS.hiddenSuffix // If the element has a value, set the hidden value as well.
  720. T.name + name[1]
  721.         }
  722.  
  723.         P._hidden = $(
  724.             '<input ' +
  725.             'type=hidden ' +
  726.  
  727.             // Create the name using the original input’s with a prefix and suffix.
  728.             'name="' + name + '"' +
  729.  
  730.             // If t// If the value changes, update the hidden input with the correct format.
  731.           $ELEMENT.data('value') || ELEMENT.value ?
  732.                     ' value="' + P.get('select', SETTINGS.formatSubmit) + 'select'                  ''
  733.             ) +
  734.             '>'
  735.         )[0]
  736.  
  737.         $ELE// Insert the hidden input after the element.
  738. date the hidden input with the correct fo// Separated for IE
  739. ('change.' + STATE.id, function() {
  740.          // Stop the event from propagating to the doc.
  741.               P.get('select', SETTINGS.fo// If it’s a focus event, add the “focused” class to the root.
  742. Insert the hidden input after the element.
  743.             after(P._hidden)
  744.     }
  745.  
  746.  
  747.     // Separated for IE
  748.   // And then finally open the picker.
  749.       // Stop the event from // Return a new picker instance.
  750. vent.stopPropagation()
  751.  
  752.         ////PickerConstructor
  753. cus/**
  754.  * The default classes and prefix to use for the HTML classes.
  755.  */event.type == 'focus' ) {
  756.             P.$root.addClass( CLASSES.focused )
  757.         }
  758.  
  759.         // And then finally open the picker.
  760.         P.open()
  761.     }
  762.  
  763.  
  764.     // Return a new picker instance.
  765.     return new PickerInstance()
  766. } //PickerConstructor
  767.  
  768.  
  769.  
  770. /**
  771.  * The default classes and prefix to use for the HTML classes.
  772.  */
  773. PickerConstructor.klasses = function( prefix ) {
  774.     prefix = prefix || 'picker'
  775.     return {
  776.  
  777.        //PickerConstructor.klasses
  778. ned/**
  779.  * Check if the default theme is being used.
  780.  */focused',
  781.  
  782.         input: prefix + '__input',
  783.         active: prefix + '__input--active',
  784. // For IE.
  785. lder: prefix + '__holder',
  786.  
  787.         frame: prefix + '__frame',
  788.         wrap: prefix + '_// For normal browsers.
  789. efix + '__box'
  790.     }
  791. } //PickerConstructor.klasses
  792.  
  793.  
  794.  
  795. /**
  796.  * Check if the default theme is being used.
  797.  */
  798. function isUsingDefaultTh/**
  799.  * Get the width of the browser’s scrollbar.
  800.  * Taken from: https://github.com/VodkaBears/Remodal/blob/master/src/jquery.remodal.js
  801.  */tyle[prop]
  802.     }
  803.  
  804.     // For normal browsers.
  805.     else if ( window.getComputedStyle ) {
  806.         theme = getComputedStyle( element )[prop]
  807.     }
  808.  
  809.     return theme == 'fixed' ).
  810.        appendTo( 'dth of the br// Get the width without scrollbars.
  811. om: https://github.com/VodkaBears/Remodal/blob/master/sr// Force adding scrollbars.
  812. tion getScrollbarWidth() {
  813.  
  814.     if ( $html.h// Add the inner div.
  815. ght() ) {
  816.         return 0
  817.     }
  818.  
  819.     var $outer = $( '<div style="visibili// Get the width with scrollbars.
  820.     appendTo( 'body' )
  821.  
  822.     // Get the width without // Remove the divs.
  823. widthWithoutScroll = $out// Return the difference between the widths.
  824. bars.
  825.     $outer.css( 'overflow', 'scroll' )
  826.  
  827.     // /**
  828.  * PickerConstructor helper methods.
  829.  */ style="width:100%" />' ).appe/**
  830.      * Create a group of nodes. Expects:
  831.      * `
  832.         {
  833.             min:    {Integer},
  834.             max:    {Integer},
  835.             i:      {Integer},
  836.             node:   {String},
  837.             item:   {Function}
  838.         }
  839.      * `
  840.      */
  841. }
  842.  
  843.  
  844.  
  845. /**
  846.  * PickerConstructor helper methods.
  847.  */
  848. PickerConstr// Scope for the looped object
  849. ate a group of nodes. Expects:
  850.      * `
  851.   // Create the nodes list
  852.    {Integer},
  853.             max:    {Intege// The counter starts from the `min`
  854.           node:   {String},
  855.             item:   {Function}
  856.         }
  857.      * `
  858.      */
  859.     gr// Loop from the `min` to `max`, incrementing by `i`
  860.    // Scope for the looped object
  861.             loopObjectScope,
  862.  
  863.             // Create the nodes list
  864.             nodesList = '',
  865.  
  866.             /// Trigger the `item` function within scope of the object
  867. PickerConstructor._.trigger( groupObject.min, groupObject )
  868.  
  869.  
  870.         // Loop from the `min` to `max`, incrementing b// Splice the subgroup and create nodes out of the sub nodes
  871.  groupObject.max, groupObject, [ counter ] ); counter += groupObject.i ) {
  872.  
  873.             // Trigger the `item` function within// the node
  874. e object
  875.             loopObjectScope = P// the classes
  876. r._.trigger( groupObject.item, groupObje// the attributes
  877.  
  878.             // Splice the subgro// Return the list of nodes
  879. he sub nodes
  880.             nodesLi//group
  881. ckerCo/**
  882.      * Create a dom node string
  883.      */ct.node,
  884.                 loopObjectScope[ 0 ],   // the node
  885.      // If the item is false-y, just return an empty string
  886.            loopObjectScope[ 2 ]    // th// If the item is an array, do a join
  887.         // Return the list of nodes
  888.         return nodesList
  889.     },// Check for the class
  890.  * Create a dom node string
  891.      */
  892.     node: function( wrapper// Check for any attributes
  893.  
  894.  
  895.         // If the item is false-y, just return an empty stri// Return the wrapped item
  896. urn ''
  897.  
  898.         // If the item is an array, do a join
  899.         item = $.isArray( item ) ? ite//node
  900.  '' ) /**
  901.      * Lead numbers below 10 with a zero.
  902.      */= klass ? ' class="' + klass + '"' : ''
  903.  
  904.         // Check for any attributes
  905.         attribute /**
  906.      * Trigger a function otherwise return the value.
  907.      */ item
  908.         return '<' + wrapper + klass + attribute + '>' + item + '</' + wrapper + '>'
  909.     }, //node
  910.  
  911.  
  912.     /**
  913.      * Lead numbers below 10 with a zero.
  914. /**
  915.      * If the second character is a digit, length is 2 otherwise 1.
  916.      */ + number
  917.     },
  918.  
  919.  
  920.     /**
  921.      * Trigger a function otherwise return the value.
  922.      */
  923.     trigge/**
  924.      * Tell if something is a date object.
  925.      */typeof callback == 'function' ? callback.apply( scope, args || [] ) : callback
  926.     },
  927.  
  928.  
  929.     /**
  930.      * If the second character is a digit, length is 2/**
  931.      * Tell if something is an integer.
  932.      */) {
  933.         return ( ion( ).test( string[ 1 ] ) ? 2 : 1
  934.     },
  935.  
  936.  
  937.     /**
  938.      * Tell if something is a date object.
  939.      */
  940.     isDate/**
  941.      * Create ARIA attribute strings.
  942.      */call( value ).indexOf( 'Da//PickerConstructor._
  943. nte/**
  944.  * Extend the picker with a component and defaults.
  945.  */omething is an integer.
  946.      */
  947.     isInteger: function( value // Extend jQuery.
  948.  {}.toString.call( value ).indexOf( 'Number' ) > -1 && val// Grab the component data.
  949. *
  950.      * Create ARIA attribute strings.
  951.      */
  952.     ari// If the picker is requested, return the data object.
  953. d the picker with a component and defaults.
  954.  */
  955. PickerConstructor.extend = function( name// If the component data exists and `options` is a string, carry out the action.
  956. on ) {
  957.  
  958.         // Grab the component data.
  959.         var componentData = this.data( name )
  960.  
  961.         // If the picker is requested, return the data object.
  962.         if ( options == 'pic// Otherwise go through each matched element and if the component
  963.  the com// doesn’t exist, create a new picker using `this` element
  964. ction.
  965.  // and merging the defaults and options with a deep copy.
  966. {
  967.             return PickerConstructor._.trigger( componentData[ options ], componentData, [ action ] )
  968.         }
  969.  
  970.         // Otherwise go through each matched element and if the component
  971.         // doesn’t exist, // Set the defaults.
  972. sing `this` element
  973.         // and merging the de//PickerConstructor.extend
  974. eep copy.
  975.         return this.each( function() {
  976.             var $this = $( this )
  977.             if ( !$this.data( name ) ) {
  978.                 new PickerConstructor( this, name, Component, options )
  979.             }
  980.         })
  981.     }
  982.  
  983.     // Set the defaults.
  984.     $.fn[ name ].defaults = Component.defaults
  985. } //PickerConstructor.extend
  986.  
  987.  
  988.  
  989. function aria(element, attribute, value) {
  990.     if ( $.isPlainObject(attribute) ) {
  991.         for ( var key in attribute ) {
  992.             ariaSet(element, key, attribute[key])
  993.         }
  994.     }
  995.     else {
  996.         ariaSet(element, attribute, value)
  997.     }
  998. }
  999. function ariaSet(element, attribute, value) {
  1000.     element.setAttribute(
  1001.         (attribute == 'role' ? '' : 'aria-') + attribute,
  1002.         value
  1003.     )
  1004. }
  1005. function ariaAttr(attribute, data) {
  1006.   // Expose the picker constructor.
  1007.  ) {
  1008.         attribute = { attr

Raw Paste


Login or Register to edit or fork this paste. It's free.