/
vixel.js
290 lines (236 loc) · 7.84 KB
/
vixel.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
// Little DSL for Vixen Element Creation (Vix-El, hence 'vixel'.)
;(function(glob) {
// We publish a function that makes a new Vixel factory, and binds a var in
// the scope of that factory to the Vixen object which created it...
glob.createVixel = function(self) {
if (!self.ui) self.ui = {};
var Vixel = c = function(kind,place) {
var element;
// Make a new element, or just add convenience functions
// to existing element.
if (typeof kind === "string") {
element = document.createElement(kind);
} else {
element = kind;
}
// Append an element
element.a = function(input) {
element.appendChild(input);
return element;
};
// Set an ARIA role
element.r = function(role) {
element.setAttribute("role",role);
return element;
};
// Add or remove a class from the element
element.c = function(classN,remove) {
var rep = new RegExp(Vixel.namespace(classN),"ig");
if (remove < 0) {
element.className =
element.className
.replace(rep,"")
.replace(/\s+/," ")
.replace(/\s+$/,"")
.replace(/^\s+/,"");
// Class shouldn't already exist!
} else if (!element.className.match(rep)) {
element.className += element.className.length ? " " : "";
element.className += Vixel.namespace(classN);
}
return element;
};
// Set the inner HTML and the title of an element
element.t = function(title) {
element.innerHTML = title;
// I determined that this was annoying for screen reader users.
// Can put it back if required.
// element.setAttribute("title",title);
return element;
};
// Bind an event handler to the element
element.on = function(event,handler) {
element.addEventListener(event,function(evt) {
handler.call(self,evt);
},"false");
return element;
};
// Style an element
element.s = function(styleName,value) {
element.style[styleName] = value;
return element;
};
// Try and replicate the accessibility features of a native control
element.ctrl = function(ariaRole) {
element.setAttribute("tabIndex",1);
// Set a focus class on the element
element
.on("focusin",function() {
element.c("focus");
})
.on("focusout",function() {
element.c("focus",-1);
});
return element.r(ariaRole);
};
// Shorten lengthy get/'setAttribute' calls
element.attr = function(attributeName,attributeValue) {
if (attributeValue === undefined ||
attributeValue === null) {
return element.getAttribute(attributeName);
} else {
element.setAttribute(attributeName,attributeValue);
}
return element;
}
// Little helper for implementing draggable functionality
element.attachDragHandler = function(handler,accommodateThumb) {
// Do we accomodate the thumb width/height in our offset calc?
accommodateThumb
= accommodateThumb !== undefined ? accommodateThumb : true;
element.addEventListener("mousedown",function(evt) {
// We don't listen to anything other than a nice left-click.
if (evt.button !== 0) return;
var width = element.offsetWidth,
height = element.offsetHeight,
on = window.addEventListener,
offsetLeft = 0,
offsetTop = 0,
pointerNode = element,
scrollY = (window.scrollY || window.pageYOffset),
scrollX = (window.scrollX || window.pageXOffset),
thumbDimensionX = accommodateThumb ? height : 0.1,
thumbDimensionY = accommodateThumb ? width : 0.1;
element.c("dragging");
element.dragging = true;
if (pointerNode.offsetParent) {
do {
offsetLeft += pointerNode.offsetLeft;
offsetTop += pointerNode.offsetTop;
} while ((pointerNode = pointerNode.offsetParent));
}
function eventHandler(evt) {
if (evt.preventDefault) evt.preventDefault();
evt.cancelBubble = true;
if (element.dragging) {
// We invert the y calculation to follow the
// logical visual order vertical sliders work...
var currentX = (evt.clientX + scrollX) - offsetLeft,
currentY = (evt.clientY + scrollY) - offsetTop,
x = currentX / width,
y = 1-(currentY / height);
handler.call(element,{
"x": x >= 0 ? x <= 1 ? x : 1 : 0,
"y": y >= 0 ? y <= 1 ? y : 1 : 0
});
}
}
eventHandler(evt);
if (!element.moveListener) {
element.moveListener = on("mousemove",eventHandler);
on("mouseup",function(evt) {
element.dragging = false;
element.c("dragging",-1);
});
}
});
};
// Now add the object to the UI map.
if (place) {
if (typeof self.ui.place !== "undefined")
throw Error("A UI object with this ID already exists!");
self.ui[place] = element;
element.c(place);
}
// Return for chaining!
return element;
};
// Little syntax sugar for replacing a DOM element
Vixel.replace = function(node,replacement) {
return node.parentNode.replaceChild(replacement,node);
};
// Namespaces string input - for classes and such.
Vixel.namespace = function(stringInput) {
return self.namespace + "-" + stringInput;
};
// Function for generating selector elements!
// Helpful since it's a more complicated function that would otherwise
// clutter vixen-core.
Vixel.createSelector = function(labelText,className,self,tabIndex) {
var label = c("label"),
valueSpan = c("span"),
wrapper = c("div"),
selector = c("select"),
idSeed = String(Math.random()).replace(/\D/,""),
id = Vixel.namespace("rs-" + idSeed),
handler = function(){},
optionCount = 0;
label.c("selectorlabel");
wrapper.c("dropdownwrapper");
valueSpan.c("currentselectorvalue");
// Set tab index by DOM order
selector.attr("tabindex",1);
// Don't show the value of the selector to screen readers - they can
// get the value from the popup list instead.
valueSpan.setAttribute("aria-hidden","true");
if (className) {
wrapper.c(className);
self.ui[className] = wrapper;
}
// Add a label if available
if (labelText)
label.t(labelText);
// Build rest of UI
wrapper
.a(label.a(valueSpan))
.a(selector);
// Add label relationship
selector.id = id;
label.setAttribute("for",id);
// Focus styling for accessibility
// (we give the wrapper a class based on whether the control is
// focussed - this gives us more flexibility when styling.)
c(selector).on("focus",function() {
wrapper.c("focus");
});
c(selector).on("blur",function() {
wrapper.c("focus",-1);
});
// Add nice little methods for adding to list
wrapper.addItem = function(text,value,funcSelect) {
var option = c("option");
option.value = value;
option.innerHTML = text;
option.funcSelect = funcSelect;
if (optionCount === 0) {
valueSpan.innerHTML = " " + text;
}
optionCount ++;
selector.a(option);
};
// And for doing something when the value changes...
wrapper.onChange = function(newHandler) {
if (newHandler instanceof Function)
handler = newHandler;
};
var changeHandler = function() {
var index = selector.selectedIndex,
optionList =
[].slice.call(wrapper.querySelectorAll("option"),0),
currentOption = optionList[index];
valueSpan.innerHTML = " " + currentOption.innerHTML;
if (currentOption.funcSelect instanceof Function) {
currentOption.funcSelect.call(currentOption);
}
handler(currentOption);
};
wrapper.on("change",changeHandler);
wrapper.on("keydown",function(eventData) {
// Don't trigger on tab.
if (eventData.keyCode !== 9) changeHandler();
});
return wrapper;
};
return Vixel;
};
})(this);