jquery-timepicker-rails 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  module Jquery
2
2
  module Timepicker
3
3
  module Rails
4
- VERSION = "0.0.1"
4
+ VERSION = "0.0.2"
5
5
  end
6
6
  end
7
7
  end
@@ -8,7 +8,8 @@ requires jQuery 1.6+
8
8
 
9
9
  !(function($)
10
10
  {
11
- var _baseDate = new Date(); _baseDate.setHours(0); _baseDate.setMinutes(0); _baseDate.setSeconds(0);
11
+
12
+ var _baseDate = _generateBaseDate();
12
13
  var _ONE_DAY = 86400;
13
14
  var _defaults = {
14
15
  className: null,
@@ -28,6 +29,7 @@ requires jQuery 1.6+
28
29
  hr: 'hr',
29
30
  hrs: 'hrs'
30
31
  };
32
+ var globalInit = false;
31
33
 
32
34
  var methods =
33
35
  {
@@ -74,33 +76,42 @@ requires jQuery 1.6+
74
76
  _lang = $.extend(_lang, settings.lang);
75
77
  }
76
78
 
77
- self.data("settings", settings);
79
+ self.data('timepicker-settings', settings);
78
80
  self.attr('autocomplete', 'off');
79
- self.click(methods.show).focus(methods.show).keydown(_keyhandler);
81
+ self.on('click.timepicker focus.timepicker', methods.show);
82
+ self.on('blur.timepicker', _formatValue);
83
+ self.on('keydown.timepicker', _keyhandler);
80
84
  self.addClass('ui-timepicker-input');
81
85
 
82
- if (self.val()) {
83
- var prettyTime = _int2time(_time2int(self.val()), settings.timeFormat);
84
- self.val(prettyTime);
85
- }
86
-
87
- var container = $('<span class="ui-timepicker-container" />');
88
- self.wrap(container);
89
-
90
- // close the dropdown when container loses focus
91
- $("body").attr("tabindex", -1).focusin(function(e) {
92
- if ($(e.target).closest('.ui-timepicker-container').length == 0) {
93
- methods.hide();
94
- }
95
- });
86
+ _formatValue.call(self.get(0));
96
87
 
88
+ if (!globalInit) {
89
+ // close the dropdown when container loses focus
90
+ $('body').on('click', function(e) {
91
+ if ($(e.target).closest('.ui-timepicker-input').length == 0 && $(e.target).closest('.ui-timepicker-list').length == 0) {
92
+ methods.hide();
93
+ }
94
+ });
95
+ globalInit = true;
96
+ }
97
97
  });
98
98
  },
99
99
 
100
100
  show: function(e)
101
101
  {
102
102
  var self = $(this);
103
- var list = self.siblings('.ui-timepicker-list');
103
+ var list = self.data('timepicker-list');
104
+
105
+ // check if input is readonly
106
+ if (self.attr('readonly')) {
107
+ return;
108
+ }
109
+
110
+ // check if list needs to be rendered
111
+ if (!list || list.length == 0) {
112
+ _render(self);
113
+ list = self.data('timepicker-list');
114
+ }
104
115
 
105
116
  // check if a flag was set to close this picker
106
117
  if (self.hasClass('ui-timepicker-hideme')) {
@@ -116,24 +127,20 @@ requires jQuery 1.6+
116
127
  // make sure other pickers are hidden
117
128
  methods.hide();
118
129
 
119
- // check if list needs to be rendered
120
- if (list.length == 0) {
121
- _render(self);
122
- list = self.siblings('.ui-timepicker-list');
123
- }
124
-
125
130
  var topMargin = parseInt(self.css('marginTop').slice(0, -2));
131
+ if (!topMargin) topMargin = 0; // correct for IE returning "auto"
132
+
126
133
  if ((self.offset().top + self.outerHeight(true) + list.outerHeight()) > $(window).height() + $(window).scrollTop()) {
127
134
  // position the dropdown on top
128
- list.css({ "top": self.position().top + topMargin - list.outerHeight() });
135
+ list.css({ 'left':(self.offset().left), 'top': self.offset().top + topMargin - list.outerHeight() });
129
136
  } else {
130
137
  // put it under the input
131
- list.css({ "top": self.position().top + topMargin + self.outerHeight() });
138
+ list.css({ 'left':(self.offset().left), 'top': self.offset().top + topMargin + self.outerHeight() });
132
139
  }
133
140
 
134
141
  list.show();
135
142
 
136
- var settings = self.data("settings");
143
+ var settings = self.data('timepicker-settings');
137
144
  // position scrolling
138
145
  var selected = list.find('.ui-timepicker-selected');
139
146
 
@@ -161,9 +168,10 @@ requires jQuery 1.6+
161
168
  {
162
169
  $('.ui-timepicker-list:visible').each(function() {
163
170
  var list = $(this);
164
- var self = list.siblings('.ui-timepicker-input');
165
- var settings = self.data("settings");
166
- if (settings.selectOnBlur) {
171
+ var self = list.data('timepicker-input');
172
+ var settings = self.data('timepicker-settings');
173
+
174
+ if (settings && settings.selectOnBlur) {
167
175
  _selectValue(self);
168
176
  }
169
177
 
@@ -175,8 +183,8 @@ requires jQuery 1.6+
175
183
  option: function(key, value)
176
184
  {
177
185
  var self = $(this);
178
- var settings = self.data("settings");
179
- var list = self.siblings('.ui-timepicker-list');
186
+ var settings = self.data('timepicker-settings');
187
+ var list = self.data('timepicker-list');
180
188
 
181
189
  if (typeof key == 'object') {
182
190
  settings = $.extend(settings, key);
@@ -200,8 +208,13 @@ requires jQuery 1.6+
200
208
  settings.durationTime = _time2int(settings.durationTime);
201
209
  }
202
210
 
203
- self.data("settings", settings);
204
- list.remove();
211
+ self.data('timepicker-settings', settings);
212
+
213
+ if (list) {
214
+ list.remove();
215
+ self.data('timepicker-list', false);
216
+ }
217
+
205
218
  },
206
219
 
207
220
  getSecondsFromMidnight: function()
@@ -217,21 +230,43 @@ requires jQuery 1.6+
217
230
  setTime: function(value)
218
231
  {
219
232
  var self = $(this);
220
- var prettyTime = _int2time(_time2int(value), self.data('settings').timeFormat);
233
+ var prettyTime = _int2time(_time2int(value), self.data('timepicker-settings').timeFormat);
221
234
  self.val(prettyTime);
222
- }
235
+ },
223
236
 
237
+ remove: function()
238
+ {
239
+ var self = $(this);
240
+
241
+ // check if this element is a timepicker
242
+ if (!self.hasClass('ui-timepicker-input')) {
243
+ return;
244
+ }
245
+
246
+ self.removeAttr('autocomplete', 'off');
247
+ self.removeClass('ui-timepicker-input');
248
+ self.removeData('timepicker-settings');
249
+ self.off('.timepicker');
250
+
251
+ // timepicker-list won't be present unless the user has interacted with this timepicker
252
+ if (self.data('timepicker-list')) {
253
+ self.data('timepicker-list').remove();
254
+ }
255
+
256
+ self.removeData('timepicker-list');
257
+ }
224
258
  };
225
259
 
226
260
  // private methods
227
261
 
228
262
  function _render(self)
229
263
  {
230
- var settings = self.data("settings");
231
- var list = self.siblings('.ui-timepicker-list');
264
+ var settings = self.data('timepicker-settings');
265
+ var list = self.data('timepicker-list');
232
266
 
233
267
  if (list && list.length) {
234
268
  list.remove();
269
+ self.data('timepicker-list', false);
235
270
  }
236
271
 
237
272
  list = $('<ul />');
@@ -241,9 +276,7 @@ requires jQuery 1.6+
241
276
  list.addClass(settings.className);
242
277
  }
243
278
 
244
- var zIndex = self.css('zIndex');
245
- zIndex = (zIndex+0 == zIndex) ? zIndex+2 : 2;
246
- list.css({'display':'none', 'position': 'absolute', "left":(self.position().left), 'zIndex': zIndex });
279
+ list.css({'display':'none', 'position': 'absolute' });
247
280
 
248
281
  if (settings.minTime !== null && settings.showDuration) {
249
282
  list.addClass('ui-timepicker-with-duration');
@@ -274,10 +307,13 @@ requires jQuery 1.6+
274
307
  list.append(row);
275
308
  }
276
309
 
277
- self.after(list);
310
+ list.data('timepicker-input', self);
311
+ self.data('timepicker-list', list);
312
+
313
+ $('body').append(list);
278
314
  _setSelected(self, list);
279
315
 
280
- list.delegate('li', 'click', { 'timepicker': self }, function(e) {
316
+ list.on('click', 'li', function(e) {
281
317
  self.addClass('ui-timepicker-hideme');
282
318
  self[0].focus();
283
319
 
@@ -290,13 +326,23 @@ requires jQuery 1.6+
290
326
  });
291
327
  };
292
328
 
329
+ function _generateBaseDate()
330
+ {
331
+ var _baseDate = new Date();
332
+ var _currentTimezoneOffset = _baseDate.getTimezoneOffset()*60000;
333
+ _baseDate.setHours(0); _baseDate.setMinutes(0); _baseDate.setSeconds(0);
334
+ var _baseDateTimezoneOffset = _baseDate.getTimezoneOffset()*60000;
335
+
336
+ return new Date(_baseDate.valueOf() - _baseDateTimezoneOffset + _currentTimezoneOffset);
337
+ }
338
+
293
339
  function _findRow(self, list, value)
294
340
  {
295
341
  if (!value && value !== 0) {
296
342
  return false;
297
343
  }
298
344
 
299
- var settings = self.data("settings");
345
+ var settings = self.data('timepicker-settings');
300
346
  var out = false;
301
347
 
302
348
  // loop through the menu items
@@ -321,10 +367,29 @@ requires jQuery 1.6+
321
367
  if (selected) selected.addClass('ui-timepicker-selected');
322
368
  }
323
369
 
370
+
371
+ function _formatValue()
372
+ {
373
+ if (this.value == '') {
374
+ return;
375
+ }
376
+
377
+ var self = $(this);
378
+ var timeInt = _time2int(this.value);
379
+
380
+ if (timeInt === null) {
381
+ self.trigger('timeFormatError');
382
+ return;
383
+ }
384
+
385
+ var prettyTime = _int2time(timeInt, self.data('timepicker-settings').timeFormat);
386
+ self.val(prettyTime);
387
+ }
388
+
324
389
  function _keyhandler(e)
325
390
  {
326
391
  var self = $(this);
327
- var list = self.siblings('.ui-timepicker-list');
392
+ var list = self.data('timepicker-list');
328
393
 
329
394
  if (!list.is(':visible')) {
330
395
  if (e.keyCode == 40) {
@@ -396,7 +461,10 @@ requires jQuery 1.6+
396
461
  list.hide();
397
462
  break;
398
463
 
399
- case 9:
464
+ case 9: //tab
465
+ methods.hide();
466
+ break;
467
+
400
468
  case 16:
401
469
  case 17:
402
470
  case 18:
@@ -419,8 +487,8 @@ requires jQuery 1.6+
419
487
 
420
488
  function _selectValue(self)
421
489
  {
422
- var settings = self.data('settings')
423
- var list = self.siblings('.ui-timepicker-list');
490
+ var settings = self.data('timepicker-settings')
491
+ var list = self.data('timepicker-list');
424
492
  var timeValue = null;
425
493
 
426
494
  var cursor = list.find('.ui-timepicker-selected');
@@ -465,6 +533,10 @@ requires jQuery 1.6+
465
533
 
466
534
  function _int2time(seconds, format)
467
535
  {
536
+ if (seconds === null) {
537
+ return;
538
+ }
539
+
468
540
  var time = new Date(_baseDate.valueOf() + (seconds*1000));
469
541
  var output = '';
470
542
 
@@ -529,11 +601,11 @@ requires jQuery 1.6+
529
601
  if (timeString+0 == timeString) return timeString;
530
602
 
531
603
  if (typeof(timeString) == 'object') {
532
- timeString = timeString.getHours()+':'+timeString.getMinutes();
604
+ timeString = timeString.getHours()+':'+timeString.getMinutes()+':'+timeString.getSeconds();
533
605
  }
534
606
 
535
607
  var d = new Date(0);
536
- var time = timeString.toLowerCase().match(/(\d+)(?::(\d\d))?\s*([pa]?)/);
608
+ var time = timeString.toLowerCase().match(/(\d{1,2})(?::(\d{2}))?(?::(\d{2}))?\s*([pa]?)/);
537
609
 
538
610
  if (!time) {
539
611
  return null;
@@ -541,11 +613,11 @@ requires jQuery 1.6+
541
613
 
542
614
  var hour = parseInt(time[1]*1);
543
615
 
544
- if (time[3]) {
616
+ if (time[4]) {
545
617
  if (hour == 12) {
546
- var hours = (time[3] == 'p') ? 12 : 0;
618
+ var hours = (time[4] == 'p') ? 12 : 0;
547
619
  } else {
548
- var hours = (hour + (time[3] == 'p' ? 12 : 0));
620
+ var hours = (hour + (time[4] == 'p' ? 12 : 0));
549
621
  }
550
622
 
551
623
  } else {
@@ -553,7 +625,8 @@ requires jQuery 1.6+
553
625
  }
554
626
 
555
627
  var minutes = ( time[2]*1 || 0 );
556
- return hours*3600 + minutes*60;
628
+ var seconds = ( time[3]*1 || 0 );
629
+ return hours*3600 + minutes*60 + seconds;
557
630
  };
558
631
 
559
632
  // Plugin entry
@@ -11,6 +11,7 @@
11
11
  -moz-box-shadow:0 5px 10px rgba(0,0,0,0.2);
12
12
  box-shadow:0 5px 10px rgba(0,0,0,0.2);
13
13
  outline: none;
14
+ z-index: 10001;
14
15
  }
15
16
 
16
17
  .ui-timepicker-list.ui-timepicker-with-duration {
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jquery-timepicker-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-06 00:00:00.000000000 Z
12
+ date: 2012-11-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: railties
16
- requirement: &21232188 !ruby/object:Gem::Requirement
16
+ requirement: &26367564 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: 3.1.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *21232188
24
+ version_requirements: *26367564
25
25
  description: A jQuery timepicker plugin inspired by Google Calendar
26
26
  email:
27
27
  - tkrotoff@gmail.com