-
Notifications
You must be signed in to change notification settings - Fork 2
/
jquery.quinn.js
1203 lines (1001 loc) · 37.5 KB
/
jquery.quinn.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// vim: set sw=4 ts=4 et:
(function ($, _) {
"use strict";
// Event names used for setting up drag events.
var DRAG_E = 'mousemove',
DRAG_START_E = 'mousedown',
DRAG_END_E = 'mouseup',
IS_TOUCH_ENABLED = false;
if ('ontouchstart' in document.documentElement) {
DRAG_E = 'touchmove';
DRAG_START_E = 'touchstart';
DRAG_END_E = 'touchend';
IS_TOUCH_ENABLED = true;
}
/**
* Given an event, returns the horizontal location on the page on which
* the event occurred.
*/
function locationOfEvent (event) {
var oEvent = event.originalEvent;
if (oEvent.touches && oEvent.touches.length) {
return oEvent.touches[0].pageX;
} else if (oEvent.changedTouches && oEvent.changedTouches.length) {
return oEvent.changedTouches[0].pageX;
}
return event.pageX;
}
/**
* Determines to what minimum and maximum value the slider should be
* drawn. Defaults to whatever minimum and maximum the user gave, but will
* prefer an explicit "drawTo" option is one is provided.
*/
function drawToOpts (drawTo, min, max) {
drawTo = drawTo || {};
return {
left: _.has(drawTo, 'left') ? drawTo.left : min,
right: _.has(drawTo, 'right') ? drawTo.right : max
};
}
/**
* When progressively enhancing an HTML input, this Quinn object's
* "wrapper", which is initially the input element, will be swapped with a
* new <div/> which can be used as the Quinn element.
*/
function createReplacementEl (element) {
return $('<div />').css({
'width': element.outerWidth(),
'margin': element.css('margin'),
'display': 'inline-block'
});
}
/**
* When progressively-enhancing an HTML input, we need to use its min/max
* values. This will read them, falling back to data- attributes, or the
* Quinn defaults.
*/
function readInputAttribute (el, quinn, attribute) {
var value = el.attr(attribute) || el.data('quinn-' + attribute);
return (value ? parseFloat(value) : quinn.options[attribute]);
}
/**
* ## Quinn
*
* Quinn is the main slider class, and handles setting up the slider UI,
* the element events, values, etc.
*
* `wrapper` is expected to be a DOM Element wrapped in a jQuery instance.
* The slider will be placed into the wrapper element, respecting it's
* width, padding etc.
*/
function Quinn (wrapper, options) {
var opts;
_.bindAll(this, 'clickBar', 'startDrag', 'drag', 'endDrag',
'handleKeyboardEvent', 'enableKeyboardEvents',
'disableKeyboardEvents');
this.wrapper = wrapper;
this.options = opts = _.extend({}, Quinn.defaults, options);
this.callbacks = {};
this.disabled = false;
this.activeHandle = null;
this.previousValue = null;
if (wrapper.is('input')) {
this.enhance(wrapper);
}
this.drawTo = drawToOpts(opts.drawTo, opts.min, opts.max);
this.model = new Model(this, this.options.strict);
this.renderer = new this.options.renderer(this);
this.keyFloodTimer = null;
this.wrapperWidth = 0;
this.wrapperOffset = 0;
this.on('setup', opts.setup);
this.on('begin', opts.begin);
this.on('drag', opts.drag);
this.on('change', opts.change);
this.on('abort', opts.abort);
if (_.isFunction(this.renderer.render)) {
this.renderer.render();
}
if (opts.disable === true) {
this.disable();
}
// Finish off by triggering the setup callback.
this.trigger('setup', this.model.value);
}
// The current Quinn version.
Quinn.VERSION = '1.2.2';
// ### Event Handling
/**
* ### on
*
* Binds a `callback` to be run whenever the given `event` occurs. Returns
* the Quinn instance permitting chaining.
*/
Quinn.prototype.on = function (event, callback) {
if (_.isString(event) && _.isFunction(callback)) {
if (! this.callbacks[event]) {
this.callbacks[event] = [];
}
this.callbacks[event].push(callback);
}
return this;
};
/**
* ### trigger
*
* Runs the callbacks of the given evengt type.
*
* If any of the callbacks return false, other callbacks will not be run,
* and trigger will return false; otherwise true is returned.
*/
Quinn.prototype.trigger = function (event, value) {
var callbacks = this.callbacks[event] || [],
i = 0,
callback;
if (value === void 0) {
value = this.value;
}
while (callback = callbacks[i++]) {
if (callback(value, this) === false) {
return false;
}
}
return true;
};
// ## Values, and Domain Logic
/**
* ### enable
*
* Enables the slider so that a user may change its value. Triggers the
* "enabled" event unless the instance was already enabled.
*/
Quinn.prototype.enable = function () {
if (this.disabled) {
this.disabled = false;
this.trigger('enabled');
}
};
/**
* ### disable
*
* Disables the slider so that a user may not change its value. Triggers
* the "disabled" event unless the instance was already disabled.
*/
Quinn.prototype.disable = function () {
if (! this.disabled) {
this.disabled = true;
this.trigger('disabled');
}
};
/**
* ### setValue
*
* Updates the value of the slider to `newValue`. If the `animate`
* argument is truthy, the change in value will be animated when updating
* the slider position. The drag callback may be skipped if `silent` is
* true.
*/
Quinn.prototype.setValue = function (newValue, animate, silent) {
if (this.start()) {
if (this.setTentativeValue(newValue, animate, silent) !== false) {
this.resolve();
} else {
this.reject();
}
}
return this.model.value;
};
/**
* ### setTentativeValue
*
* Used internally to set the model value while ensuring that the
* necessary callbacks are fired.
*
* See `setValue`.
*/
Quinn.prototype.setTentativeValue = function (newValue, animate, silent) {
var preDragValue = this.model.value,
prevScalar = null,
nextScalar = null,
scalar;
if (newValue === undefined || newValue === null) {
return false;
}
// If the slider is a range (more than one value), but only a number
// was given, we need to alter the given value so that we set the
// other values also.
if (this.model.values.length > 1 && _.isNumber(newValue)) {
if (this.activeHandle === null) {
// Without an active handle, we don't know which value we are
// supposed to set.
return false;
}
scalar = this.model.sanitizeValue(newValue);
newValue = _.clone(this.model.values);
// Ensure that the handle doesn't "cross over" a higher or
// lower handle.
prevScalar = newValue[this.activeHandle - 1];
nextScalar = newValue[this.activeHandle + 1];
if (prevScalar !== null && scalar <= prevScalar) {
scalar = prevScalar + this.options.step;
}
if (nextScalar !== null && scalar >= nextScalar) {
scalar = nextScalar - this.options.step;
}
newValue[this.activeHandle] = scalar;
}
newValue = this.model.setValue(newValue);
if (newValue === false ||
(! silent && ! this.trigger('drag', newValue))) {
this.model.setValue(preDragValue);
// If the drag callback failed, we need to send another drag
// event so that the developer has the chance to respond.
(newValue !== false && this.trigger('drag', preDragValue));
return false;
}
this.trigger('redraw', animate);
return newValue;
};
/**
* ### stepUp
*
* Increases the value of the slider by `step`. Does nothing if the slider
* is alredy at its maximum value.
*
* The optional argument is an integer which indicates the number of steps
* by which to increase the value.
*
* Returns the new slider value
*/
Quinn.prototype.stepUp = function (count, animate, tentative) {
var func = (tentative ? this.setTentativeValue : this.setValue);
if (this.model.values.length > 1) {
// Cannot step a range-based slider.
return this.model.value;
}
return _.bind(func, this)(
this.model.value + this.model.step * (count || 1), animate);
};
/**
* ### stepDown
*
* Decreases the value of the slider by `step`. Does nothing if the slider
* is alredy at its minimum value.
*
* The optional argument is an integer which indicates the number of steps
* by which to decrease the value.
*
* Returns the new slider value
*/
Quinn.prototype.stepDown = function (count, animate, tentative) {
return this.stepUp(-(count || 1), animate, tentative);
};
/**
* ### start
*
* Tells the Quinn instance that the user is about to make a change to the
* slider value. The calling function should check the return value of
* start -- if false, no changes are permitted to the slider.
*/
Quinn.prototype.start = function () {
if (this.disabled === true || ! this.trigger('begin')) {
return false;
}
this.previousValue = this.model.value;
// These attributes are cached so that we don't have to look them up
// every time the user drags the handle.
this.wrapperWidth = this.wrapper.width();
this.wrapperOffset = this.wrapper.offset().left;
return true;
};
/**
* ### resolve
*
* Tells the Quinn instance that the user has finished making their
* changes to the slider and that the new value should be retained.
*/
Quinn.prototype.resolve = function () {
this.deactivateActiveHandle();
if (_.isEqual(this.previousValue, this.model.value)) {
// The user reset the slider back to where it was.
this.reject();
return false;
}
/* Run the change callback; if the callback returns false then we
* revert the slider change, and restore everything to how it was
* before. Note that reverting the change will also fire an change
* event when the value is reverted.
*/
if (! this.trigger('change', this.model.value)) {
this.setTentativeValue(this.previousValue);
this.reject();
return false;
}
this.previousValue = null;
};
/**
* ### reject
*
* Aborts a slider change.
*/
Quinn.prototype.reject = function () {
this.previousValue = null;
this.deactivateActiveHandle();
return this.trigger('abort');
};
/**
* ### valueFromMouse
*
* Determines the value of the slider at the position indicated by the
* mouse cursor.
*/
Quinn.prototype.valueFromMouse = function (mousePosition) {
var percent = this.positionFromMouse(mousePosition),
delta = this.drawTo.right - this.drawTo.left;
return this.drawTo.left + delta * percent;
};
/**
* ### positionFromMouse
*
* Determines how far along the bar the mouse cursor is as a fraction of
* the bar's width.
*
* TODO Cache the width and offset when the drag operation begins.
*/
Quinn.prototype.positionFromMouse = function (mousePosition) {
var maxRight = this.wrapperOffset + this.wrapperWidth,
barPosition;
if (mousePosition < this.wrapperOffset) {
// Mouse is to the left of the bar.
barPosition = 0;
} else if (mousePosition > maxRight) {
// Mouse is to the right of the bar.
barPosition = this.wrapperWidth;
} else {
barPosition = mousePosition - this.wrapperOffset;
}
return barPosition / this.wrapperWidth;
};
// ## User Interaction
/**
* ### startDrag
*
* Begins a drag event which permits a user to move the slider handle in
* order to adjust the slider value.
*
* When `skipPreamble` is true, startDrag will not run `start()` on the
* assumption that it has already been run (see `clickBar`).
*/
Quinn.prototype.startDrag = function (event, skipPreamble) {
// Only enable dragging when the left mouse button is used.
if (! IS_TOUCH_ENABLED && event.which !== 1) {
return true;
}
if (! skipPreamble && ! this.start()) {
return false;
}
this.activateHandleWithEvent(event);
// These events are bound for the duration of the drag operation and
// keep track of the value changes made, with the events being removed
// when the mouse button is released.
$(document).
on(DRAG_END_E + '.quinn', this.endDrag).
on(DRAG_E + '.quinn', this.drag).
// The mouse may leave the window while dragging, and the mouse
// button released. Watch for the mouse re-entering, and see what
// the button is doing.
on('mouseenter.quinn', this.endDrag);
return false;
};
/**
* ### drag
*
* Bound to the mousemove event, alters the slider value while the user
* contiues to hold the left mouse button.
*/
Quinn.prototype.drag = function (event) {
this.setTentativeValue(
this.valueFromMouse(locationOfEvent(event)), false);
return event.preventDefault();
};
/**
* ### endDrag
*
* Run when the user lifts the mouse button after completing a drag.
*/
Quinn.prototype.endDrag = function (event) {
// Remove the events which were bound in `startDrag`.
$(document).
off(DRAG_END_E + '.quinn').
off(DRAG_E + '.quinn').
off('mouseenter.quinn');
this.resolve();
return event.preventDefault();
};
/**
* ### clickBar
*
* Event handler which is used when the user clicks part of the slider bar
* to instantly change the value.
*/
Quinn.prototype.clickBar = function (event) {
// Ignore the click if the left mouse button wasn't used.
if (! IS_TOUCH_ENABLED && event.which !== 1) {
return true;
}
if (this.start()) {
this.activateHandleWithEvent(event);
this.setTentativeValue(
this.valueFromMouse(locationOfEvent(event)));
this.startDrag(event, true);
}
return event.preventDefault();
};
/**
* Given a click or drag event, determines which model "value" is closest
* to the clicked location and tells the view to activate the handle.
* Does nothing if a handle is already active.
*/
Quinn.prototype.activateHandleWithEvent = function (event) {
var value, closestValue, handleIndex;
if (this.activeHandle) {
return false;
}
value = this.valueFromMouse(locationOfEvent(event));
closestValue = _.min(this.model.values, function (handleValue) {
return Math.abs(handleValue - value);
});
handleIndex = _.indexOf(this.model.values, closestValue);
if (handleIndex !== -1) {
this.activeHandle = handleIndex;
this.trigger('handleOn', this.activeHandle);
}
};
/**
* Deactivates the currently active handle. Does nothing if no handle is
* active.
*/
Quinn.prototype.deactivateActiveHandle = function () {
if (this.activeHandle !== null && this.activeHandle !== -1) {
this.trigger('handleOff', this.activeHandle);
this.activeHandle = null;
}
};
/**
* When an handle is focused, allows left/right to change the value.
*/
Quinn.prototype.enableKeyboardEvents = function (event) {
$(event.target).on('keydown', this.handleKeyboardEvent);
$(event.target).on('keyup', this.handleKeyboardEvent);
};
/**
* Unsets keyboard event handlers after the handle is blurred.
*/
Quinn.prototype.disableKeyboardEvents = function (event) {
$(event.target).off('keydown', this.handleKeyboardEvent);
$(event.target).off('keyup', this.handleKeyboardEvent);
};
/**
* Receives events from the keyboard and adjusts the Quinn value.
*/
Quinn.prototype.handleKeyboardEvent = function (event) {
if (event.type === 'keydown') {
if (this.keyFloodTimer) {
window.clearTimeout(this.keyFloodTimer);
this.keyFloodTimer = null;
}
if (this.previousValue == null && ! this.start()) {
return false;
}
} else if (event.type === 'keyup') {
if (this.previousValue != null) {
if (this.options.keyFloodWait) {
// Prevent multiple successive keydowns from repeatedly
// triggering resolve.
this.keyFloodTimer = window.setTimeout(
_.bind(this.resolve, this),
this.options.keyFloodWait);
} else {
this.resolve();
}
}
return true;
}
switch (event.which) {
case 33: // Page up.
this.stepUp(10, false, true); break;
case 34: // Page down.
this.stepDown(10, false, true); break;
case 37: // Left arrow.
case 40: // Down arrow.
if (event.metaKey) {
this.setTentativeValue(this.model.minimum, false);
} else {
this.stepDown(event.altKey ? 10 : 1, false, true);
}
break;
case 39: // Right arrow.
case 38: // Up arrow.
if (event.metaKey) {
this.setTentativeValue(this.model.maximum, false);
} else {
this.stepUp(event.altKey ? 10 : 1, false, true);
}
break;
default:
return true;
}
event.preventDefault();
};
/**
* Used during initialization to progressively-enhance an existing HTML
* input, instead of simply drawing into a <div/>.
*
* Afters the Quinn options, renders the replacement elements in the DOM,
* and sets up events to send the value back-and-forth between Quinn and the
* original input element.
*/
Quinn.prototype.enhance = function (element) {
this.options.disabled = element.attr('disabled');
this.options.step = readInputAttribute(element, this, 'step');
this.options.value = readInputAttribute(element, this, 'value');
this.options.min = readInputAttribute(element, this, 'min');
this.options.max = readInputAttribute(element, this, 'max');
this.wrapper = createReplacementEl(element).insertAfter(element.hide());
this.on('change', function (value) { element.val(value) });
element.on('change', _.bind(function () {
this.setValue(element.val());
}, this));
};
/**
* ## Model
*
* Holds the current Quinn value, ensures that the value set is valid
* (within the `range` bounds, one of the `only` values, etc).
*/
function Model (quinn, initiallyStrict) {
var opts, initialValue, length, i;
this.options = opts = quinn.options;
this.step = opts.step;
this.only = opts.only;
this.values = [];
/* The minimum and maximum need to be "fixed" so that they are a
* multiple of the "step" option. For example, if given a step of 5
* then we must round a minimum value of 2 up to 5, and a maximum
* value of 97 down to 95.
*
* Note that values are always rounded so that they stay within the
* given minimum and maximum range. 2 cannot be rounded down to 0,
* since that is lower than the value provided by the user.
*
* TODO Provided this.min and this.max are set, isn't it possible
* to just use sanitizeValue?
*/
this.minimum = this.roundToStep(opts.min);
this.maximum = this.roundToStep(opts.max);
if (this.minimum < opts.min) {
this.minimum += this.step;
}
if (this.maximum > opts.max) {
this.maximum -= this.step;
}
/* Determine the initial value of the slider. Prefer an explicitly set
* value, whether a scalar or an array. If no value is provided by the
* developer, instead fall back to using the minimum.
*/
if (opts.value === undefined || opts.value === null) {
initialValue = this.minimum;
} else if (_.isArray(opts.value)) {
initialValue = opts.value;
} else {
initialValue = [ opts.value ];
}
for (i = 0, length = initialValue.length; i < length; i++) {
this.values[i] = null;
}
this.setValue(initialValue, initiallyStrict);
}
/**
* An internal method which sets the value of the slider during a drag
* event. `setValue` should be called only after `start` in the Quinn
* instance.
*
* Only when `resolve` is called is the value considered final. If
* `reject` is called, the tentative value is discarded and the
* previous "good" value is restored.
*
* The new value will be returned (which may differ slightly from the
* value you set, if it had to be adjusted to fit the step option, or stay
* within the minimum / maximum range). The method will return false if
* the value you set resulted in no changes.
*/
Model.prototype.setValue = function (newValue, strict) {
var originalValue = this.values, length, i;
if (! _.isArray(newValue)) {
newValue = [ newValue ];
} else {
// Don't mutate the original array.
newValue = _.clone(newValue);
}
for (i = 0, length = newValue.length; i < length; i++) {
newValue[i] = this.sanitizeValue(newValue[i], strict);
}
if (_.isEqual(newValue, originalValue)) {
return false;
}
this.value = this.values = newValue;
if (this.values.length === 1) {
// When the slider represents only a single value, instead of
// setting an array as the value, just use the number.
this.value = newValue[0];
}
return this.value;
};
/**
* ### roundToStep
*
* Given a number, rounds it to the nearest step.
*
* For example, if options.step is 5, given 4 will round to 5. Given
* 2 will round to 0, etc. Does not take account of the minimum and
* maximum range options.
*/
Model.prototype.roundToStep = function (number) {
var multiplier = 1 / this.step,
rounded = Math.round(number * multiplier) / multiplier;
if (_.isArray(this.only)) {
rounded = _.min(this.only, function (value) {
return Math.abs(value - number);
});
}
if (rounded > this.maximum) {
return rounded - this.step;
} else if (rounded < this.minimum) {
return rounded + this.step;
}
return rounded;
};
/**
* ### sanitizeValue
*
* Given a numberic value, snaps it to the nearest step, and ensures that
* it is within the selectable minima and maxima.
*/
Model.prototype.sanitizeValue = function (value, strict) {
if (strict !== false) {
value = this.roundToStep(value);
}
if (value > this.maximum) {
return this.maximum;
} else if (value < this.minimum) {
return this.minimum;
}
return value;
};
/**
* ## Renderer
*
* Handles creation of the DOM nodes used by Quinn, as well as redrawing
* those elements when the slider value is changed.
*
* You may write your own renderer class and provide it to Quinn using the
* `renderer: myRenderer` option.
*
* Your class needs to define only two public methods:
*
* render:
* Creates the DOM elements for displaying the slider, and inserts them
* into the tree.
*
* redraw:
* Alters DOM elements (normally CSS) so that the visual representation
* of the slider matches the value.
*/
Quinn.Renderer = function (quinn) {
_.bindAll(this, 'render', 'redraw');
var self = this;
this.quinn = quinn;
this.model = quinn.model;
this.wrapper = quinn.wrapper;
this.options = quinn.options;
this.handles = [];
this.lastDraw = [];
quinn.on('redraw', this.redraw);
quinn.on('handleOn', function (handleIndex) {
self.handles[handleIndex].addClass('active');
});
quinn.on('handleOff', function (handleIndex) {
self.handles[handleIndex].removeClass('active');
});
quinn.on('enabled', function () {
self.wrapper.removeClass('disabled');
if (self.options.disabledOpacity !== 1.0) {
self.wrapper.css('opacity', 1.0);
}
});
quinn.on('disabled', function () {
self.wrapper.addClass('disabled');
if (self.options.disabledOpacity !== 1.0) {
self.wrapper.css('opacity', self.options.disabledOpacity);
}
});
};
/**
* ### render
*
* Quinn is initialized with an empty wrapper element; render adds the
* necessary DOM elements in order to display the slider UI.
*
* render() is called automatically when creating a new Quinn instance.
*/
Quinn.Renderer.prototype.render = function () {
var i, length;
this.width = this.wrapper.width();
function addRoundingElements (element) {
element.append($('<div class="left" />'));
element.append($('<div class="main" />'));
element.append($('<div class="right" />'));
}
this.bar = $('<div class="bar" />');
this.deltaBar = $('<div class="delta-bar" />');
if (this.model.values.length > 1) {
this.wrapper.addClass('multiple');
}
addRoundingElements(this.bar);
if (this.model.values.length <= 2) {
addRoundingElements(this.deltaBar);
this.bar.append(this.deltaBar);
}
this.wrapper.html(this.bar);
this.wrapper.addClass('quinn');
// Add each of the handles to the bar, and bind the click events.
for (i = 0, length = this.model.values.length; i < length; i++) {
this.handles[i] = $('<span class="handle" tabindex="0" role="slider"></span>')
.attr('aria-valuemin', this.model.minimum)
.attr('aria-valuemax', this.model.maximum)
.attr('aria-valuenow', this.model.values[i]);
this.handles[i].on(DRAG_START_E, this.quinn.startDrag);
if (this.quinn.model.values.length < 2) {
this.handles[i].on('focus', this.quinn.enableKeyboardEvents);
this.handles[i].on('blur', this.quinn.disableKeyboardEvents);
}
this.bar.append(this.handles[i]);
}
// Finally, these events are triggered when the user seeks to
// update the slider.
this.wrapper.on(DRAG_START_E, this.quinn.clickBar);
this.barHeight = this.bar.height();
// If the handles have left and right margins, it indicates that the
// user wants the handle to "overhang" the edges of the bar. We have to
// account for this when positioning the handles.
this.handleOverhang =
parseInt(this.handles[0].css('margin-left'), 10) +
parseInt(this.handles[0].css('margin-right'), 10);
this.redraw(false);
};
/**
* ### redraw
*
* Moves the slider handle and the delta-bar background elements so that
* they accurately represent the value of the slider.
*/
Quinn.Renderer.prototype.redraw = function (animate) {
var self = this;
if (animate !== false) {
animate = true;
}
_.each(this.model.values, function (value, i) {
var handle, position;
if (value === self.lastDraw[i]) {
return true;
}
handle = self.handles[i].stop();
position = self.position(value) + 'px';
handle.attr('aria-valuenow', value);
if (animate && self.options.effects) {
handle.animate({ left: position }, {
duration: self.options.effectSpeed,
step: self.redrawDeltaBarInStep(handle)
});
} else {
handle.css('left', position);
self.redrawDeltaBar(value, handle);
}
});
this.lastDraw = _.clone(this.model.values);
};
/**
* ### redrawDeltaBar
*
* Positions the blue delta bar so that it originates at a position where
* the value 0 is. Accepts a `value` argument so that it may be used
* within a `step` callback in a jQuery `animate` call.
*/
Quinn.Renderer.prototype.redrawDeltaBar = function (value, handle) {
var left = null,
right = null,
drawAt = parseInt(handle.css('left'), 10) + this.barHeight;
this.deltaBar.stop(true);
if (this.model.values.length > 1) {
if (handle) {
if (handle === this.handles[0]) {
left = drawAt;
} else {
right = drawAt;
}
} else {
left = value[0];
right = value[1];
}
} else if (value < 0) {
// position with the left edge underneath the handle, and the
// right edge at 0