JAVASCRIPT   7

picker.time.js

Guest on 24th May 2021 03:42:18 AM

  1.  
  2. /*!
  3.  * Time picker for pickadate.js v3.5.4
  4.  * http://amsul.github.io/pickadate.js/time.htm
  5.  */
  6.  
  7. (function ( factory ) {
  8.  
  9.     // AMD.
  10.     if ( typeof define == 'function' && define.amd )
  11.         define( ['picker','jquery'], factory )
  12.  
  13.     // Node.js/browserify.
  14.     else if ( typeof exports == 'object' )
  15.         module.exports = factory( require('./picker.js'), require('jquery') )
  16.  
  17.     // Browser globals.
  18.     else factory( Picker, jQuery )
  19.  
  20. }(function( Picker, $ ) {
  21.  
  22.  
  23. /**
  24.  * Globals and constants
  25.  */
  26. var HOURS_IN_DAY = 24,
  27.     MINUTES_IN_HOUR = 60,
  28.     HOURS_TO_NOON = 12,
  29.     MINUTES_IN_DAY = HOURS_IN_DAY * MINUTES_IN_HOUR,
  30.     _ = Picker._
  31.  
  32.  
  33.  
  34. /**
  35.  * The time picker constructor
  36.  */
  37. function TimePicker( picker, settings ) {
  38.  
  39.     var clock = this,
  40.         elementValue = picker.$node[ 0 ].value,
  41.         elementDataValue = picker.$node.data( 'value' ),
  42.         valueString = elementDataValue || elementValue,
  43.         formatString = elementDataValue ? settings.formatSubmit : settings.format
  44.  
  45.     clock.settings = settings
  46.     clock.$node = picker.$node
  47.  
  48.     // The queue of methods that will be used to build item objects.
  49.     clock.queue = {
  50.         interval: 'i',
  51.         min: 'measure create',
  52.         max: 'measure create',
  53.         now: 'now create',
  54.         select: 'parse create validate',
  55.         highlight: 'parse create validate',
  56.         view: 'parse create validate',
  57.         disable: 'deactivate',
  58.         enable: 'activate'
  59.     }
  60.  
  61.     // The component's item object.
  62.     clock.item = {}
  63.  
  64.     clock.item.clear = null
  65.     clock.item.interval = settings.interval || 30
  66.     clock.item.disable = ( settings.disable || [] ).slice( 0 )
  67.     clock.item.enable = -(function( collectionDisabled ) {
  68.         return collectionDisabled[ 0 ] === true ? collectionDisabled.shift() : -1
  69.     })( clock.item.disable )
  70.  
  71.     clock.
  72.         set( 'min', settings.min ).
  73.         set( 'max', settings.max ).
  74.         set( 'now' )
  75.  
  76.     // When there’s a value, set the `select`, which in turn
  77. turn// also sets the `highlight` and `view`.
  78. ew`.
  79.     if ( valueString ) {
  80.         clock.set( 'select', valueString, {
  81.             format: formatString,
  82.             fromValue: !!elementValue
  83.         })
  84.     }
  85. // If there’s no value, default to highlighting “today”.
  86. œtoday”.
  87.     else {
  88.         clock.
  89.             set( 'select', null ).
  90.             set( 'highlight', clock.item// The keycode to movement mapping.
  91. movement mapping.
  92.     clock.key =// Down
  93.    40: 1, // Dow// Up
  94.     38: -1, // // Right
  95.   39: 1, // Righ// Left
  96.   37: -1, // Left
  97.         go: function( timeChange ) {
  98.             clock.set(
  99.                 'highlight',
  100.                 clock.item.highlight.pick + timeChange * clock.item.interval,
  101.                 { interval: timeChange * clock.item.interval }
  102.             )
  103.             this.render()
  104.     // Bind some picker events.
  105. me picker events.
  106.     picker.
  107.         on( 'render', function() {
  108.             var $pickerHolder = picker.$root.children(),
  109.                 $viewset = $pickerHolder.find( '.' + settings.klass.viewset ),
  110.                 vendors = function( prop ) {
  111.                     return ['webkit', 'moz', 'ms', 'o', ''].map(function( vendor ) {
  112.                         return ( vendor ? '-' + vendor + '-' : '' ) + prop
  113.                     })
  114.                 },
  115.                 animations = function( $el, state ) {
  116.                     vendors( 'transform' ).map(function( prop ) {
  117.                         $el.css( prop, state )
  118.                     })
  119.                     vendors( 'transition' ).map(function( prop ) {
  120.                         $el.css( prop, state )
  121.                     })
  122.                 }
  123.             if ( $viewset.length ) {
  124.                 animations( $pickerHolder, 'none' )
  125.                 $pickerHolder[ 0 ].scrollTop = ~~$viewset.position().top - ( $viewset[ 0 ].clientHeight * 2 )
  126.                 animations( $pickerHolder, '' )
  127.             }
  128.         }, 1 ).
  129.         on( 'open', function() {
  130.             picker.$root.find( 'button' ).attr( 'disabled', false )
  131.         }, 1 ).
  132.         on( 'close', function() {
  133.             picker.$root.find( 'button' ).attr( 'disabled', true )
  134. //TimePicker
  135. )
  136. /**
  137.  * Set a timepicker item object.
  138.  */r item object.
  139.  */
  140. TimePicker.prototype.set = function( type, value, options ) {
  141.  
  142.     var clock = this,
  143.         clockItem // If the value is `null` just set it immediately.
  144. t it immediately.
  145.     if ( value === null ) {
  146.         if ( type == 'clear'
  147.        clockItem[ type ] = value
  148.        return clock
  149.    }
  150.  
  151.    // Otherwise go through the queue of methods, and invoke the functions.
  152.    // Update this as the time unit, and set the final value as this item.
  153.    // * In the case of `enable`, keep the queue but set `disable` instead.
  154.    //   And in the case of `flip`, keep the queue but set `enable` instead.
  155.    clockItem[ ( type == 'kItem[ ( type == 'enable' ? 'disable' : type == 'flip' ? 'enable' : type ) ] = clock.queue[ type ].split( ' ' ).map( function( method ) {
  156.         value = clock[ method ]( type, value, options )
  157.         return value
  158. // Check if we need to cascade through more updates.
  159. ugh more updates.
  160.     if ( type == 'select' ) {
  161.         clock.set( 'highlight', clockItem.select, options )
  162.     }
  163.     else if ( type == 'highlight' ) {
  164.         clock.set( 'view', clockItem.highlight, options )
  165.     }
  166.     else if ( type == 'interval' ) {
  167.         clock.
  168.             set( 'min', clockItem.min, options ).
  169.             set( 'max', clockItem.max, options )
  170.     }
  171.     else if ( type.match( sable|enable)$/ ) ) {
  172.         if ) ) {
  173.         if ( type == 'min' ) {
  174.             clock.set( 'max', clockItem.max, options )
  175.         }
  176.         if ( clockItem.select && clock.disabled( clockItem.select ) ) {
  177.             clock.set( 'select', clockItem.select, options )
  178.         }
  179.         if ( clockItem.highlight && clock.disabled( clockItem.highlight ) ) {
  180.             clock.set( 'highlight', clockItem.highlight, options )
  181.         }
  182.     }
  183.  
  184.  //TimePicker.prototype.set
  185. ke/**
  186.  * Get a timepicker item object.
  187.  */r item object.
  188.  */
  189. TimePicker.prototype.get = function( type ) {
  190.     return th//TimePicker.prototype.get
  191. ke/**
  192.  * Create a picker time object.
  193.  */r time object.
  194.  */
  195. TimePicker.prototype.create = function( type, value, options ) {
  196.  
  197.     var // If there’s no value, use the type as the value.
  198. the type as the value.
  199.     value = value === undefin// If it’s a date object, convert it into an array.
  200. , convert it into an array.
  201.     if ( _.isDate( value ) ) {
  202.         value = [ value.getHours(), valu// If it’s an object, use the “pick” value.
  203. bject, use the “pick” value.
  204.     if ( $.isPlainObject( value ) && _.isInteger( value.pick ) )// If it’s an array, convert it into minutes.
  205. t’s an array, convert it into minutes.
  206.     else if ( $.isArray( value ) ) {
  207.         value = +value[ 0 ]// If no valid value is passed, set it to “now”.
  208. o valid value is passed, set it to “now”.
  209.     else if ( !_.isInteger( value ) ) {
  210.        // If we’re setting the max, make sure it’s greater than the min.
  211. re setting the max, make sure it’s greater than the min.
  212.     if ( type == 'max' && value < clo// If the value doesn’t fall directly on the interval,
  213. }
  214.  
  215.  // add one interval to indicate it as “passed”.
  216. he interval,
  217.     // add one interval to indicate it as “passed”.
  218.     if ( type != 'min' && type != 'max' && (value - clock.item.min.pick) % c// Normalize it into a “reachable” interval.
  219. ck.item.interval
  220.     }
  221.  
  222.     // Normalize it into a â€// Return the compiled object.
  223.  value = clock.normali// Divide to get hours from minutes.
  224. turn the compiled object.
  225.     return {
  226.  
  227.         // Divide to get hours from minutes.// The remainder is the minutes.
  228.  value / MINUTES_IN_HOUR ) % HOURS_IN_DAY,
  229.  
  230.         // The remainder is the minutes.
  231.    // The time in total minutes.
  232.  value % MINUTES_IN_HOUR ) % MINUTES_IN_HOUR,
  233.  
  234.         // The time i// Reference to the “relative” value to pick.
  235. value ) % MINUTES_IN_DAY,
  236.  
  237.  //TimePicker.prototype.create
  238. /**
  239.  * Create a range limit object using an array, date object,
  240.  * literal “true”, or integer relative to another time.
  241.  */ using an array, date object,
  242.  * literal “true”, or integer relative to another time.
  243.  */
  244. TimePicker.prototype.createRange = function( from, to ) {
  245.  
  246.     var clock = this,
  247.         createTime = function( time ) {
  248.             if ( time === true || $.isArray( time ) || _.isDate( time ) // Create objects if possible.
  249. k.create( time )
  250.             }
  251.             return time
  252.         }
  253.  
  254.     // Create objects if possible.
  255.     if ( !_.isInteger( from ) ) {
  256.         from// Create relative times.
  257. }
  258.     if ( !_.isInteger( to ) ) {
  259.         to = createTime( to )
  260.     }
  261.  
  262.     // Create relative times.
  263.     if ( _.isInteger( from ) && $.isPlainObject( to ) ) {
  264.         from = [ to.hour, to.mins + ( from * clock.settings.interval ) ];
  265.     }
  266.     else if ( _.isInteger( to ) && $.isPlainObject( from ) ) {
  267.         to = [ from.hour, from.mins + ( to * clock.settings.inte//TimePicker.prototype.createRange
  268.   /**
  269.  * Check if a time unit falls within a time range object.
  270.  *//TimePicker.prototype.createRange
  271.  
  272.  
  273. /**
  274.  * Check if a time unit falls within a time range object.
  275.  */
  276. TimePicker.prototype.withinRange = function( range, timeUnit ) {
  277.     range = this.createRange(ran/**
  278.  * Check if two time range objects overlap.
  279.  */e.from.pick && timeUnit.pick <= range.to.pick
  280. }
  281.  
  282.  
  283. /**
  284.  * Check if two time range objects// Convert the ranges into comparable times.
  285. ges = function( one, two ) {
  286.  
  287.     var clock = this
  288.  
  289.     // Convert the ranges into comparable times.
  290.     one = clock.createRange( one.from, one.to )
  291.     two = clock.createRange( two.from, two.to )
  292.  
  293.     return clock.withinRange( one, two.from ) || clock.withinRange(/**
  294.  * Get the time relative to now.
  295.  */nge( two, one.from ) || clock.withinRange( two, o/*, options*/**
  296.  * Get the time relative to now.
  297.  */
  298. TimePicker.prototype.now = function( type, value/*, options*/ ) {
  299.  
  300.     var interval = this.item.interval,
  301.         date = new Date(),
  302.         nowMinutes = date.getHours() * MINUTES_IN_H// Make sure “now” falls within the interval range.
  303. teger( value ),
  304.         isBelowInterval
  305.  
  306.     // Check if the difference is less than the interval itself.
  307. e.
  308.     nowMinutes -= nowMinutes % interval
  309.  
  310.     // Check if the difference is less // Add an interval because the time has “passed”.
  311. < 0 && interval * value + nowMinutes <= -interval
  312.  
  313.     // Add an interv// If the value is a number, adjust by that many intervals.
  314.  += type == 'min' && isBelowInterval ? 0 : interval
  315.  
  316.     // If the value is a number, adjust by that many intervals.
  317.     if ( isValueInteger ) {
  318.         nowMinutes += interval * (
  319.      // Return the final calculation.
  320.  'max' ?
  321.                //TimePicker.prototype.now
  322.   /**
  323.  * Normalize minutes to be “reachable” based on the min and interval.
  324.  */utes
  325. } //TimePicker.prototype.now
  326.  
  327.  
  328. /**
  329.  * Normalize mi/*, options*/€œreachable” based on the min and interval.
  330.  */
  331. TimePicker.prototype.normalize = function( type, va// If setting min time, don’t shift anything.
  332. em.i// Otherwise get the value and min difference and then
  333. in.p// normalize the difference with the interval.
  334. t shift anything.
  335.     // Otherwise get the value and min difference// Return the adjusted value.
  336.  difference with th//TimePicker.prototype.normalize
  337. 'm/**
  338.  * Measure the range of minutes.
  339.  */
  340.  
  341.     // Return the adjusted value.
  342.     return value
  343. } //TimePicker.prototype.normalize
  344.  
  345.  
  346. /**
  347. // If it’s anything false-y, set it to the default.
  348. ype.measure = function( type, value, options ) {
  349.  
  350.     var clock = this
  351.  
  352.     // If it’s anything false-y, set // If it’s a string, parse it.
  353. e ) {
  354.         value = type == 'min' ? [ 0, 0 ] : [ HOURS_IN_DAY - 1, MINUTES_IN_HOUR - 1 ]
  355.  // If it’s a literal true, or an integer, make it relative to now.
  356.  == 'string' ) {
  357.         value = clock.parse( type, value )
  358.     }
  359.  
  360.     // If it’s a literal true, or an integer// If it’s an object already, just normalize it.
  361. true || _.isInteger( value ) ) {
  362.         value = clock.now( type, value, options )
  363.     }
  364.  
  365.     // If it’s an object already, just normalize it.
  366.     else ///TimePicker.prototype.measure
  367.  _/**
  368.  * Validate an object as enabled.
  369.  */e = clock.normalize( type, value.pick, options )
  370.     }
  371.  
  372.     return value
  373. } ///TimePicker.prototype.measure
  374.  
  375.  
  376. /**
  377.  * Validate an object as enabled.
  378.  */
  379. TimePicker.prototype.validate = functi// Check if the object is disabled.
  380.    var clock = this,
  381.         interval = options && // Shift with the interval until we reach an enabled time.
  382.     // Check if the object is disabled.
  383.     if ( clock.disabled( tim// Scope the object into range.
  384. with the interval until we reach an enabled time// Do a second check to see if we landed on a disabled min/max.
  385.  
  386.  
  387.   // In that case, shift using the opposite interval as before.
  388. ( timeObject )
  389.  
  390.     // Do a second check to see if we landed on a disabled min/max.
  391.     // In that case, shift usin// Return the final object.
  392. efore.
  393.     if ( clock.di//TimePicker.prototype.validate
  394.  t/**
  395.  * Check if an object is disabled.
  396.  */val * -1 )
  397.     }
  398.  
  399.     // Return the final object.
  400.     return timeObject
  401. } //TimePicker.proto// Filter through the disabled times to check if this is one.
  402. ePicker.prototype.disabled = function( timeToVerify ) {
  403.  
  404.     var clock = this,
  405.  
  406.         // Fil// If the time is a number, match the hours.
  407. is is one.
  408.         isDisabledMatch = clock.item.disable.filter( function( timeToDisable ) {
  409.  
  410.             // If the time is a number, ma// If it’s an array, create the object and match the times.
  411.  {
  412.                 return timeToVerify.hour == timeToDisable
  413.             }
  414.  
  415.             // If it’s an array, create the object and match the times.
  416.             if ( $.isArray( // If it’s an object, match a time within the “from” and “to” range.
  417. erify.pick == clock.create( timeToDisable ).pick
  418.             }
  419.  
  420.             // If it’s an object, match a time within the “from” and â// If this time matches a disabled time, confirm it’s not inverted.
  421. ) {
  422.                 return clock.withinRange( timeToDisable, timeToVerify )
  423.             }
  424.         })
  425.  
  426.     // If this time matches a disabled time, confirm it’s not inverted.
  427.     isDisabledMatch = isDisabledMatch.length && !isDisabledMatch.filter(function( timeTo// If the clock is "enabled" flag is flipped, flip the condition.
  428. able[2] == 'inverted' ||
  429.             $.isPlainObject( timeToDisable ) && timeToDisable.inverted
  430.     }).length
  431.  
  432.     // If the clock is "enabled" flag is flipped, flip the conditi//TimePicker.prototype.disabled
  433.  =/**
  434.  * Shift an object by an interval until we reach an enabled object.
  435.  */lock.item.min.pick ||
  436.         timeToVerify.pick > clock.item.max.pick
  437. } //TimePicker.prototype.disabled
  438.  
  439.  
  440. /**
  441.  * Shift an object by an interval until we reach an enab/*,
  442.         safety = 1000*/prototype.shift = function( timeObject, interval ) {
  443.  
  444. // Keep looping as long as the time is disabled.
  445. em.min.pick,/*safety &&*/imit = clock.item.max.pick/*,
  446.         safet/*safety -= 1
  447.         if ( !safety ) {
  448.             throw 'Fell into an infinite loop while shifting to ' + timeObject.hour + ':' + timeObject.mins + '.'
  449.         }*/t ) ) {
  450.  
  451.  // Increase/decrease the time by the interval and keep looping.
  452. 'Fell into an infinite loop while shifting to ' + timeObject.hour + ':' + // If we've looped beyond the limits, break out of the loop.
  453. se the time by the interval and keep looping.
  454.         timeObject = clock.create( timeObject.pick += interval )
  455.  
  456.    // Return the final object.
  457. nd the limits, break out//TimePicker.prototype.shift
  458. me/**
  459.  * Scope an object to be within range of min and max.
  460.  */          break
  461.         }
  462.     }
  463.  
  464.     // Return the final object.
  465.     return timeObject
  466. } //TimePicker.prototype.shift
  467.  
  468.  
  469. /**
  470.  * Scope an object to be within range of min and max.
  471.  */
  472. TimePicker.prototype.scope = function( timeObject ) {
  473.     var minLimi//TimePicker.prototype.scope
  474.   /**
  475.  * Parse a string into a usable type.
  476.  */s.create( timeObject.pick > maxLimit ? maxLimit : timeObject.pick < minLimit ? minLimit : timeObject )
  477. } //TimePicker.prototype.scope
  478.  
  479.  
  480. /**
  481.  * Parse a string into a us// If it’s already parsed, we’re good.
  482.  function( type, value, options ) {
  483.  
  484.     var hour, minutes, isPM, item, parseVal// We need a `.format` to parse the value with.
  485. = {}
  486.  
  487.     // If it’s already parsed, we’re good.
  488.     if ( !value || typeof value != 'string' ) {
  489.         return value
  490.   // Convert the format into an array and then map through it.
  491. if ( !( options && options.format ) ) {
  492.         options = options || {}
  493.         options.format = clock.settings.format// Grab the formatting label.
  494. mat into an array and then map through it.
  495.     clock.formats.toArra// The format length is from the formatting label function or the
  496.     substrin// label length without the escaping exclamation (!) mark.
  497. ormattingLabel = clock.formats[ label ],
  498.  
  499.             // The format length is from the formatting label function or the
  500.             // label length without the escaping exclamation (!// If there's a format label, split the value up to the format length.
  501. trigger(// Then add it to the parsing object with appropriate label.
  502.          label.replace( abel, '' ).length
  503.  
  504.         // If there's a format label, split the value up to the format length.
  505.         // Then add it to the parsing object with appropriate label.
  506.     // Update the time value as the substring from format length to end.
  507.  formatLength )
  508.             parsingObject[ label ] = subs// Grab the hour and minutes from the parsing object.
  509. }
  510.  
  511.         // Update the time value as the substring from format length to end.
  512.         value = value.substr( formatLength )
  513.     })
  514.  
  515.     // Grab the hour and minutes from the parsing object.
  516.     for ( item in parsingObject ) {
  517.         parseValue = parsingObject[item]
  518.         if ( _.isInteger(parseValue) ) {
  519.             if ( item.match(em == 'i' )) ) {
  520.                 hour = parseValue
  521.                 if ( item == 'h' || item == 'hh' ) {
  522.                     hour %= 12
  523.                 }
  524.             }
  525.             else if ( item == 'i' ) {
  526.                 minutes = parseVal// Calculate it in minutes and return.
  527.  if ( item.match(? hour) && parseValue.match(S_IN_) && ('h' in pars//TimePicker.prototype.parse
  528. bj/**
  529.  * Various formats to display the object in.
  530.  */  // Calculate it in minutes and return.
  531.     return (isPM ? hour + 12 : hour) * MINU// If there's string, then get the digits length.
  532. se
  533.  
  534.  
  535. /**// Otherwise return the selected hour in "standard" format.
  536. .prototype.formats = {
  537.  
  538.     h: function( string, timeObject ) {
  539.  
  540.         // If there's string, then get the digits length.
  541.         // Otherwise return // If there's a string, then the length is always 2.
  542. n string// Otherwise return the selected hour in "standard" format with a leading zero.
  543.     hh: function( string, timeObject ) {
  544.  
  545.         // If there's a string, then the length is always 2.
  546.         // Otherwise return the selected// If there's string, then get the digits length.
  547.       re// Otherwise return the selected hour in "military" format as a string.
  548. ON )
  549.     },
  550.     H: function( string, timeObject ) {
  551.  
  552.         // If there's string, then get the digits length.
  553.         // Otherwise // If there's string, then get the digits length.
  554.  string.// Otherwise return the selected hour in "military" format with a leading zero.
  555. },
  556.     HH: function( string, timeObject ) {
  557.  
  558.         // If there's string, then get the digits length.
  559.         // Otherwise return t// If there's a string, then the length is always 2.
  560. zero.
  561.   // Otherwise return the selected minutes.
  562.  _.lead( timeObject.hour % 24 )
  563.     },
  564.     i: function( string, timeObject ) {
  565.  
  566.         // If there's a string// If there's a string, then the length is always 4.
  567. eturn th// Otherwise check if it's more than "noon" and return either am/pm.
  568. ns )
  569.     },
  570.     a: function( string, timeObject ) {
  571.  
  572.         // If there's a string, then the length is always 4.
  573.         // Otherwise check if it's more th// If there's a string, then the length is always 2.
  574. ing ? 4 // Otherwise check if it's more than "noon" and return either am/pm.
  575. .m.'
  576.     },
  577.     A: function( string, timeObject ) {
  578.  
  579.         // If there's a string, then the length is alwa// Create an array by splitting the formatting string passed.
  580. return either am/pm.
  581.         return string ? 2 : MINUTES_IN_DAY / 2 > timeObject.time % MINUTES_IN_DAY ? // Format an object into a string using the formatting options.
  582. atting string passed.
  583.     toArray: function( formatString ) { return formatString.split( turn clock.formats.toArray( ) },
  584.  
  585.     // Format an object into a string using the formatting options.
  586.     toString: function ( formatString, itemObject ) {
  587.         var clock = this
  588.         return clock.format//TimePicker.prototype.formats
  589. func/**
  590.  * Check if two time units are the exact.
  591.  */ck.formats[ label ], clock, [ 0, itemObject ] ) || label.replace( r cl, '' )
  592.         }// When we’re working with minutes, do a direct comparison.
  593.  Check if two time units are the exact.
  594.  */
  595. TimePicker.prototype.isTimeExact = function( one, two ) {
  596.  
  597.     var clock = this
  598.  
  599.     // When we’re working with minutes, do // When we’re working with time representations, compare the “pick” value.
  600. ) ||
  601.         ( typeof one == 'boolean' && typeof two == 'boolean' )
  602.      ) {
  603.         return one === two
  604.     }
  605.  
  606.     // When we’re working with time representations, compare the “pickâ// When we’re working with range objects, compare the “from” and “to”.
  607. ( _.isDate( two ) || $.isArray( two ) )
  608.     ) {
  609.         return clock.create( one ).pick === clock.create( two ).pick
  610.     }
  611.  
  612.     // When we’re working with range objects, compare/**
  613.  * Check if two time units overlap.
  614.  */.
  615.     if ( $.isPlainObject( one ) && $.isPlainObject( two ) ) {
  616.         return clock.is// When we’re working with an integer, compare the hours.
  617. , two.to )
  618.     }
  619.  
  620.     return false
  621. }
  622.  
  623.  
  624. /**
  625.  * Check if two time units overlap.
  626.  */
  627. TimePicker.prototype.isTimeOverlap = function( one, two ) {
  628.  
  629.     var clock = this
  630.  
  631.     // When we’re working with an integer, compare the hours.
  632.     if ( _.isInteger( one ) // When we’re working with range objects, check if the ranges overlap.
  633. ck.create( two ).hour
  634.     }
  635.     if ( _.isInteger( two ) && ( _.isDate( one ) || $.isArray( one ) ) ) {
  636.         return two === clock.creat/**
  637.  * Flip the “enabled” state.
  638.  */™re working with range objects, check if the ranges overlap.
  639.     if ( $.isPlainObject( one ) && $.isPlainObject( two ) ) {
  640.         return clock.overla/**
  641.  * Mark a collection of times as “disabled”.
  642.  */lip the “enabled” state.
  643.  */
  644. TimePicker.prototype.flipEnable = function(val) {
  645.     var itemObject = this.item
  646.     itemObject.enable = val |// If we’re flipping, that’s all we need to do.
  647.  a collection of times as “disabled”.
  648.  */
  649. TimePicker.prototype.deactivate = function( type, timesToDisable ) {
  650.  
  651.     var clock = this,
  652.         disabledItems = clock.item.disable.slice(0)
  653.  
  654.  
  655.     // If we’re flipping, that’s all we need to do.
  656.     if ( timesToDisa// Otherwise go through the times to disable.
  657.    }
  658.  
  659.     else if ( timesToDisable === false ) {
  660.         clock.flipEnable(1)
  661.         disabledItems = []
  662.     // When we have disabled items, check for matches.
  663.   clock.flip// If something is matched, immediately break out.
  664.  Otherwise go through the times to disable.
  665.     else {
  666.  
  667.         timesToDisable.map(function( unitToDisable ) {
  668.  
  669.             var matchFound
  670.  
  671.             // When we have disabled items, check for matches.
  672.             // If something is matched, immediately break out.
  673.    // If nothing was found, add the validated unit to the collection.
  674. = 1 ) {
  675.                 if ( clock.isTimeExact( unitToDisable, disabledItems[index] ) ) {
  676.                     matchFound = true
  677.                     break
  678.                 }
  679.             }
  680.  
  681.             // If nothing was found, add the validated unit to the collection.
  682.             if ( !matchFound ) {
  683.                 if (
  684.                     _.isInteger( unitToDisable ) ||
  685.                     _.isDate( unitToDisable ) ||
  686.              // Return the updated collection.
  687. ||
  688.                     ( $.//TimePicker.prototype.deactivate
  689. un/**
  690.  * Mark a collection of times as “enabled”.
  691.  */) {
  692.                     disabledItems.push( unitToDisable )
  693.                 }
  694.             }
  695.         })
  696.     }
  697.  
  698.     // Return the updated collection.
  699.     return disabledItems
  700. } //TimePicker.p// If we’re flipping, that’s all we need to do.
  701. mes as “enabled”.
  702.  */
  703. TimePicker.prototype.activate = function( type, timesToEnable ) {
  704.  
  705.     var clock = this,
  706.         disabledItems = clock.item.disable,
  707.         disabledItemsCount = disabledItems.length
  708.  
  709.     // If we’re flipping, that’s all we need to do.// Otherwise go through the disabled times.
  710.   clock.flipEnable()
  711.     }
  712.  
  713.     else if ( timesToEnable === true ) {
  714.         clock.flipEnable(1)
  715.         disabledItems = []
  716.     }
  717.  
  718.     else if ( timesToEnable === false ) {
  719.         clock.flip// Go through the disabled items and try to find a match.
  720. ise go through the disabled times.
  721.     else {
  722.  
  723.         timesToEnable.map(function( unitToEnable ) {
  724.  
  725.             var matchFound,
  726.              // When an exact match is found, remove it from the collection.
  727. atched
  728.  
  729.             // Go through the disabled items and try to find a match.
  730.             for ( index = 0; index < disabledItemsCount; index += 1 ) {
  731.  
  732.                 disabledUnit = disabledItems[index]
  733.  
  734.                 // When an exact // When an overlapped match is found, add the “inverted” state to it.
  735. isTimeExact( disabledUnit, unitToEnable ) ) {
  736.                     matchFound = disabledItems[index] = null
  737.                     isRangeMatched = true
  738.                     break
  739.                 }
  740.  
  741.                 // When an overlapped match is found, add the “inverted” state to it.
  742.                 else if ( clock.isTimeOverlap( disabledUnit, unitToEnable ) ) {
  743.                     if ( $.isPlainObject( unitToEnable ) ) {
  744.                         unitToEnable.inverted = true
  745.                         matchFound = unitToEnable
  746.                     }
  747.                     else if ( $.isArray( unitToEnable ) ) {
  748.                         matchFound = unitToEnable
  749.                         if ( !matchFound[2] ) matchFound.push( 'inverted' )
  750.            // If a match was found, remove a previous duplicate entry.
  751. ble ) ) {
  752.                         matchFound = [ unitToEnable.getFullYear(), unitToEnable.getMonth(), unitToEnable.getDate(), 'inverted' ]
  753.                     }
  754.                     break
  755.                 }
  756.             }
  757.  
  758.             // If a match was found, remove a previous duplicate entry// In the event that we’re dealing with an overlap of range times,
  759. sCount; inde// make sure there are no “inverted” times because of it.
  760. ms[index], unitToEnable ) ) {
  761.                     disabledItems[index] = null
  762.                     break
  763.                 }
  764.             }
  765.  
  766.             // In the event that we’re dealing with an overlap of range times,
  767.             // make sure there are no “inverted” times becaus// If something is still matched, add it into the collection.
  768. dex < disabledItemsCount; index += 1 ) {
  769.                 if ( clock.isTimeOverlap( disabledItems[index], unitToEnable// Return the updated collection.
  770. dItems[index] = null
  771.                     break
  772.                 }
  773.          //TimePicker.prototype.activate
  774. g /**
  775.  * The division to use for the range intervals.
  776.  */   if ( matchFound ) {
  777.                 disabled/*, options*/atchFound )
  778.             }
  779.         })
  780.     }
  781.  
  782.     // Return the updated collection.
  783.  /**
  784.  * Create a string for the nodes in the picker.
  785.  */ val != null })
  786. } //TimePicker.prototype.activate
  787.  
  788.  
  789. /**
  790.  * The division to use for the range intervals.
  791.  */
  792. TimePicker.prototype.i = function( type, value/*, options*/ ) {
  793.     return _.isInteger( value ) && value > 0 ? value : this.item.interval
  794. }
  795.  
  796.  
  797. /**
  798.  * Create a string for the nodes in the picker.
  799.  */
  800. TimePicker.prototype.nodes = function( isOpen ) {
  801.  
  802.     var
  803.         clock = this,
  804.         settings = clock.settings,
  805.         selectedObject = clock.item.select,
  806.         highlightedObject = clock.item.highlight,
  807.         viewsetObject = clock.item.view,
  808.         disabledCollection = clock.item.disable
  809.  
  810.     return _.node(
  811.         'ul',
  812.         _.group({
  813.             min: clock.item.min.pick,
  814.             max: clock.item.max.pick,
  815.             i: clock.item.interval,
  816.             node: 'li',
  817.             item: function( loopedTime ) {
  818.                 loopedTime = clock.create( loopedTime )
  819.                 var timeMinutes = loopedTime.pick,
  820.                     isSelected = selectedObject && selectedObject.pick == timeMinutes,
  821.                     isHighlighted = highlightedObject && highlightedObject.pick == timeMinutes,
  822.                     isDisabled = disabledCollection && clock.disabled( loopedTime )
  823.                 return [
  824.                     _.trigger( clock.formats.toString, clock, [ _.trigger( settings.formatLabel, clock, [ loopedTime ] ) || settings.format, loopedTime ] ),
  825.                     (function( klasses ) {
  826.  
  827.                         if ( isSelected ) {
  828.                             klasses.push( settings.klass.selected )
  829.                         }
  830.  
  831.                         if ( isHighlighted ) {
  832.                             klasses.push( settings.klass.highlighted )
  833.                         }
  834.  
  835.                         if ( viewsetObject && viewsetObject.pick == timeMinutes ) {
  836.                             klasses.push( settings.klass.viewset )
  837.                         }
  838.  
  839.                         if ( isDisabled ) {
  840.                             klasses.push( settings.klass.disabled )
  841.                         }
  842.  
  843.                         return klasses.join( ' ' )
  844.                     })( [ settings.klass.listItem ] ),
  845.                     'data-pick=' + loopedTime.pick + ' ' + _.ariaAttr({
  846.                         role: 'option',
  847.                         selected: isSelected && clock.$node.val() === _.trigger(
  848.                                 clock.formats.toString,
  849.                                 clock,
  850.            // * For Firefox forms to submit, make sure to set the button’s `type` attribute as “button”.
  851.                       activedescendant: isHighlighted ? true : null,
  852.                         disabled: isDisabled ? true : null
  853.                     })
  854.                 ]
  855.             }
  856.         }) +
  857.  
  858.         // * For Firefox forms to submit, make sure to set the button’s `type` attribute as “button”.
  859.         _.node(
  860.             'li'presentation' _.node(
  861.                 'button',
  862.                 settings.clear,
  863.                 settings.klass.buttonClear,
  864.           //TimePicker.prototype.nodes
  865. =1' + (/* ==========================================================================
  866.    Extend the picker to add the component with the defaults.
  867.    ========================================================================== */Attr({ role: 'listbox', controls: clock.$node[0].id })
  868.     )
  869. } //Time// Clear
  870. ototype.nodes
  871.  
  872.  
  873.  
  874.  
  875.  
  876.  
  877.  
  878. /* =========// The format to show on the `input` element
  879. ====================
  880.    Extend the// The interval between each time
  881. the defaults.
  882.    ==============// Classes
  883. ================================================= */
  884.  
  885. TimePicker.defaults = (function( prefix ) {
  886.  
  887.     return {
  888.  
  889.         // Clear
  890.         clear: 'Clear',
  891.  
  892.         // The format to show on the `input` element
  893.         format: 'h:i A',
  894.  
  895.         // The interval between each time
  896.         interval: 30,
  897.  
  898.         // Classes
  899.         klass: {
  900.  
  901.             picker: prefix + ' ' + prefix + '--time',
  902.             holder: prefix + '__holder',
  903.  
  904.             list: prefix + '__list',
  905.             listItem: prefix + '__list-item',
  906.  
  907.             disabled: prefix + '__list-item--disabled',
  908.     /**
  909.  * Extend the picker to add the time picker.
  910.  */            highlighted: prefix + '__list-item--

Raw Paste


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