連載第7回は,
スライダーの各部の名称
コードの解説に入る前に,
実際の動作例もご覧ください
Control.Slider
それではコードを見ていきましょう。
0001:// script.aculo.us slider.js v1.8.1, Thu Jan 03 22:07:12 -0500 2008
0002:
0003:// Copyright (c) 2005-2007 Marty Haught, Thomas Fuchs
0004://
0005:// script.aculo.us is freely distributable under the terms of an MIT-style license.
0006:// For details, see the script.aculo.us web site: http://script.aculo.us/
0007:
1~7行目は,
0008:if (!Control) var Control = { };
0009:
8行目で,
0010:// options:
0011:// axis: 'vertical', or 'horizontal' (default)
0012://
0013:// callbacks:
0014:// onChange(value)
0015:// onSlide(value)
10~15行目で,
0016:Control.Slider = Class.create({
0017: initialize: function(handle, track, options) {
0018: var slider = this;
0019:
0020: if (Object.isArray(handle)) {
0021: this.handles = handle.collect( function(e) { return $(e) });
0022: } else {
0023: this.handles = [$(handle)];
0024: }
0025:
0026: this.track = $(track);
0027: this.options = options || { };
0028:
0029: this.axis = this.options.axis || 'horizontal';
0030: this.increment = this.options.increment || 1;
0031: this.step = parseInt(this.options.step || '1');
0032: this.range = this.options.range || $R(0,1);
0033:
0034: this.value = 0; // assure backwards compat
0035: this.values = this.handles.map( function() { return 0 });
0036: this.spans = this.options.spans ? this.options.spans.map(function(s){ return $(s) }) : false;
0037: this.options.startSpan = $(this.options.startSpan || null);
0038: this.options.endSpan = $(this.options.endSpan || null);
0039:
0040: this.restricted = this.options.restricted || false;
0041:
0042: this.maximum = this.options.maximum || this.range.end;
0043: this.minimum = this.options.minimum || this.range.start;
0044:
0045: // Will be used to align the handle onto the track, if necessary
0046: this.alignX = parseInt(this.options.alignX || '0');
0047: this.alignY = parseInt(this.options.alignY || '0');
0048:
0049: this.trackLength = this.maximumOffset() - this.minimumOffset();
0050:
0051: this.handleLength = this.isVertical() ?
0052: (this.handles[0].offsetHeight != 0 ?
0053: this.handles[0].offsetHeight : this.handles[0].style.height.replace(/px$/,"")) :
0054: (this.handles[0].offsetWidth != 0 ? this.handles[0].offsetWidth :
0055: this.handles[0].style.width.replace(/px$/,""));
0056:
0057: this.active = false;
0058: this.dragging = false;
0059: this.disabled = false;
0060:
0061: if (this.options.disabled) this.setDisabled();
0062:
0063: // Allowed values array
0064: this.allowedValues = this.options.values ? this.options.values.sortBy(Prototype.K) : false;
0065: if (this.allowedValues) {
0066: this.minimum = this.allowedValues.min();
0067: this.maximum = this.allowedValues.max();
0068: }
0069:
0070: this.eventMouseDown = this.startDrag.bindAsEventListener(this);
0071: this.eventMouseUp = this.endDrag.bindAsEventListener(this);
0072: this.eventMouseMove = this.update.bindAsEventListener(this);
0073:
0074: // Initialize handles in reverse (make sure first handle is active)
0075: this.handles.each( function(h,i) {
0076: i = slider.handles.length-1-i;
0077: slider.setValue(parseFloat(
0078: (Object.isArray(slider.options.sliderValue) ?
0079: slider.options.sliderValue[i] : slider.options.sliderValue) ||
0080: slider.range.start), i);
0081: h.makePositioned().observe("mousedown", slider.eventMouseDown);
0082: });
0083:
0084: this.track.observe("mousedown", this.eventMouseDown);
0085: document.observe("mouseup", this.eventMouseUp);
0086: document.observe("mousemove", this.eventMouseMove);
0087:
0088: this.initialized = true;
0089: },
16~89行目の,
20行目で,
23行目で,
26行目で,
29行目で,
30行目で,
31行目で,
32行目で,
34行目で,
35行目で,
36行目で,
37行目で,
38行目で,
40行目で,
42行目で,
43行目で,
46行目で,
47行目で,
49行目で,
51行目で,
57行目で,
58行目で,
59行目で,
61行目で,
64行目で,
66,
70~72行目で,
75~83行目で,
76行目で,
77行目で,
81行目で,
84行目で,
85行目で,
86行目で,
88行目で,