JAVASCRIPT   8

picker.time.js

Guest on 20th July 2021 03:37:29 PM

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

Raw Paste


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