1 /*
  2  * classList.js: Cross-browser full element.classList implementation.
  3  * 2011-06-15
  4  *
  5  * By Eli Grey, http://eligrey.com
  6  * Public Domain.
  7  * NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
  8  */
  9 
 10 if (typeof document !== "undefined" && !("classList" in document.createElement("a")))
 11 {
 12 	(function (view){
 13 	
 14 		var classListProp = "classList",
 15 			protoProp = "prototype",
 16 			elemCtrProto = (view.HTMLElement || view.Element)[protoProp],
 17 			objCtr = Object,
 18 			strTrim = String[protoProp].trim ||
 19 			function ()
 20 			{
 21 				return this.replace(/^\s+|\s+$/g, "");
 22 			},
 23 			arrIndexOf = Array[protoProp].indexOf ||
 24 			function (item)
 25 			{
 26 				var
 27 				i = 0,
 28 					len = this.length;
 29 				for (; i < len; i++)
 30 				{
 31 					if (i in this && this[i] === item)
 32 					{
 33 						return i;
 34 					}
 35 				}
 36 				return -1;
 37 			}
 38 			// Vendors: please allow content code to instantiate DOMExceptions
 39 			,
 40 			/**
 41 			 * @private
 42 			 */
 43 			DOMEx = function (type, message)
 44 			{
 45 				this.name = type;
 46 				this.code = DOMException[type];
 47 				this.message = message;
 48 			},
 49 			/**
 50 			 * @private
 51 			 */
 52 			checkTokenAndGetIndex = function (classList, token)
 53 			{
 54 				if (token === "")
 55 				{
 56 					throw new DOMEx("SYNTAX_ERR", "An invalid or illegal string was specified");
 57 				}
 58 				if (/\s/.test(token))
 59 				{
 60 					throw new DOMEx("INVALID_CHARACTER_ERR", "String contains an invalid character");
 61 				}
 62 				return arrIndexOf.call(classList, token);
 63 			},
 64 			/**
 65 			 * @private
 66 			 */
 67 			ClassList = function (elem)
 68 			{
 69 				var
 70 				trimmedClasses = strTrim.call(elem.className),
 71 					classes = trimmedClasses ? trimmedClasses.split(/\s+/) : [],
 72 					i = 0,
 73 					len = classes.length;
 74 				for (; i < len; i++)
 75 				{
 76 					this.push(classes[i]);
 77 				}
 78 				this._updateClassName = function ()
 79 				{
 80 					elem.className = this.toString();
 81 				};
 82 			},
 83 			classListProto = ClassList[protoProp] = [],
 84 			/**
 85 			 * @private
 86 			 */
 87 			classListGetter = function ()
 88 			{
 89 				return new ClassList(this);
 90 			};
 91 		// Most DOMException implementations don't allow calling DOMException's toString()
 92 		// on non-DOMExceptions. Error's toString() is sufficient here.
 93 		DOMEx[protoProp] = Error[protoProp];
 94 		classListProto.item = function (i)
 95 		{
 96 			return this[i] || null;
 97 		};
 98 		classListProto.contains = function (token)
 99 		{
100 			token += "";
101 			return checkTokenAndGetIndex(this, token) !== -1;
102 		};
103 		classListProto.add = function (token)
104 		{
105 			token += "";
106 			if (checkTokenAndGetIndex(this, token) === -1)
107 			{
108 				this.push(token);
109 				this._updateClassName();
110 			}
111 		};
112 		classListProto.remove = function (token)
113 		{
114 			token += "";
115 			var index = checkTokenAndGetIndex(this, token);
116 			if (index !== -1)
117 			{
118 				this.splice(index, 1);
119 				this._updateClassName();
120 			}
121 		};
122 		classListProto.toggle = function (token)
123 		{
124 			token += "";
125 			if (checkTokenAndGetIndex(this, token) === -1)
126 			{
127 				this.add(token);
128 			}
129 			else
130 			{
131 				this.remove(token);
132 			}
133 		};
134 		classListProto.toString = function ()
135 		{
136 			return this.join(" ");
137 		};
138 
139 		if (objCtr.defineProperty)
140 		{
141 			var classListPropDesc = {
142 				get: classListGetter,
143 				enumerable: true,
144 				configurable: true
145 			};
146 			try
147 			{
148 				objCtr.defineProperty(elemCtrProto, classListProp, classListPropDesc);
149 			}
150 			catch (ex)
151 			{ // IE 8 doesn't support enumerable:true
152 				if (ex.number === -0x7FF5EC54)
153 				{
154 					classListPropDesc.enumerable = false;
155 					objCtr.defineProperty(elemCtrProto, classListProp, classListPropDesc);
156 				}
157 			}
158 		}
159 		else if (objCtr[protoProp].__defineGetter__)
160 		{
161 			elemCtrProto.__defineGetter__(classListProp, classListGetter);
162 		}
163 
164 	}(self));
165 }
166 
167 // --------------------------------------------------------------------------
168 
169 (function (){
170 	var d, tag_reg, class_reg;
171 	
172 	tag_reg = /^([\w\-]+)$/;
173 	class_reg = /\.([\w\-]+)$/;
174 	
175 	
176 	//Private function for getting/setting attributes
177 	function _attr(sel, name, value)
178 	{
179 		var oldVal, doAttr;
180 
181 		//Get the value of the attribute, if it exists
182 		if (typeof sel.hasAttribute !== "undefined")
183 		{
184 			if (sel.hasAttribute(name))
185 			{
186 				oldVal = sel.getAttribute(name);
187 			}
188 
189 			doAttr = true;
190 		}
191 		else if (typeof sel[name] !== "undefined")
192 		{
193 			oldVal = sel[name];
194 			doAttr = false;
195 		}
196 		else if (name === "class" && typeof sel.className !== "undefined") //className attribute
197 		{
198 			name = "className";
199 			oldVal = sel.className;
200 			doAttr = false;
201 		}
202 
203 		//Well, I guess that attribute doesn't exist
204 		if (typeof oldVal === "undefined" && (typeof value === "undefined" || value === null))
205 		{
206 			console.log(value);
207 			console.log(sel);
208 			console.log("Element does not have the selected attribute");
209 			return;
210 		}
211 
212 		//No value to set? Return the current value
213 		if (typeof value === "undefined")
214 		{
215 			return oldVal;
216 		}
217 
218 		//Determine what to do with the attribute
219 		if (typeof value !== "undefined" && value !== null)
220 		{
221 			if(doAttr === true)
222 			{
223 				sel.setAttribute(name, value);
224 			}
225 			else
226 			{
227 				sel[name] = value;
228 			} 
229 		}
230 		else if (value === null)
231 		{
232 			if(doAttr === true)
233 			{
234 				sel.removeAttribute(name);
235 			}
236 			else
237 			{
238 				delete sel[name];
239 			} 
240 		}
241 
242 		return (typeof value !== "undefined") ? value : oldVal;
243 	}
244 	
245 	function _toCamel(s)
246 	{
247 		return s.replace(/(\-[a-z])/g, function($1){
248 			return $1.toUpperCase().replace('-','');
249 		});
250 	}
251 
252 	function _css(sel, prop, val)
253 	{
254 		var equi;
255 		
256 		//Camel-case
257 		prop = _toCamel(prop);
258 
259 		//Equivalent properties for 'special' browsers
260 		equi = {
261 			outerHeight: "offsetHeight",
262 			outerWidth: "offsetWidth",
263 			top: "posTop"
264 		};
265 		
266 		
267 		//If you don't define a value, try returning the existing value
268 		if(typeof val === "undefined" && sel.style[prop] !== "undefined")
269 		{
270 			return sel.style[prop];
271 		}
272 		else if(typeof val === "undefined" && sel.style[equi[prop]] !== "undefined")
273 		{
274 			return sel.style[equi[prop]];
275 		}
276 
277 		//Let's try the easy way first
278 		if(typeof sel.style[prop] !== "undefined")
279 		{
280 			sel.style[prop] = val;
281 
282 			//Short circuit
283 			return;
284 		}
285 		else if(sel.style[equi[prop]])
286 		{
287 			sel.style[equi[prop]] = val;
288 			return;
289 		}
290 		
291 		//No matches? Well, lets log it for now
292 		console.log("Property " + prop + " nor an equivalent seems to exist");
293 	}
294 	
295 	// --------------------------------------------------------------------------
296 
297 	/**
298 	 * DOM
299 	 * 
300 	 * Dom manipulation module
301 	 * @namespace
302 	 * @memberOf $_
303 	 * @name dom
304 	 */
305 	d = {
306 		/**
307 		 * Adds a class to the element(s) specified by the current
308 		 * selector
309 		 * 
310 		 * @name addClass
311 		 * @memberOf $_.dom
312 		 * @function
313 		 * @param string class
314 		 * @return void
315 		 */
316 		addClass: function (c)
317 		{
318 			$_.each(function (e){
319 				e.classList.add(c);
320 			});
321 		},
322 		/**
323 		 * Removes a class from the element(s) specified by the current
324 		 * selector
325 		 * 
326 		 * @name removeClass
327 		 * @memberOf $_.dom
328 		 * @function
329 		 * @param string class
330 		 * @return void
331 		 */
332 		removeClass: function (c)
333 		{
334 			$_.each(function (e){
335 				e.classList.remove(c);
336 			});
337 		},
338 		/**
339 		 * Hides the element(s) specified by the current selector
340 		 * 
341 		 * @name hide
342 		 * @memberOf $_.dom
343 		 * @function
344 		 * @return void
345 		 */
346 		hide: function ()
347 		{
348 			this.css('display', 'none');
349 		},
350 		/**
351 		 * Shows the element(s) specified by the current selector. 
352 		 * if type is specified, the element will have it's style
353 		 * property set to "display:[your type]". If type is not
354 		 * specified, the element is set to "display:block".
355 		 * 
356 		 * @name  show
357 		 * @memberOf $_.dom
358 		 * @function
359 		 * @param [string] type
360 		 * @return void
361 		 */
362 		show: function (type)
363 		{
364 			if (typeof type === "undefined")
365 			{
366 				type = "block";
367 			}
368 
369 			this.css("display", type);
370 		},
371 		/**
372 		 * Sets attributes on element(s) specified by the current 
373 		 * selector, or, if name is not specified, returns the 
374 		 * value of the attribute of the element specified by the
375 		 * current selector.
376 		 *
377 		 * @name attr
378 		 * @memberOf $_.dom
379 		 * @function
380 		 * @param string name
381 		 * @param string value
382 		 * @return string
383 		 * @type string
384 		 */
385 		attr: function (name, value)
386 		{
387 			var sel = this.el;
388 
389 			//Make sure you don't try to get a bunch of elements
390 			if (sel.length > 1 && typeof value === "undefined")
391 			{
392 				console.log(sel);
393 				console.log("Must be a singular element");
394 				return;
395 			}
396 			else if (sel.length > 1 && typeof value !== "undefined") //You can set a bunch, though
397 			{
398 				$_.each(function (e){
399 					return _attr(e, name, value);
400 				});
401 			}
402 			else //Normal behavior
403 			{
404 				return _attr(sel, name, value);
405 			}
406 		},
407 		/**
408 		 * Sets or retrieves the text content of the element
409 		 * specified by the current selector. If a value is 
410 		 * passed, it will set that value on the current element,
411 		 * otherwise it will return the value of the current element
412 		 *
413 		 * @name text
414 		 * @memberOf $_.util
415 		 * @function
416 		 * @param [string] value
417 		 * @returns string
418 		 * @type string
419 		 */
420 		text: function (value)
421 		{
422 			var oldValue, set, type, sel;
423 		
424 			sel = this.el;
425 			
426 			set = (typeof value !== "undefined") ? true : false;
427 			
428 			type = (typeof sel.innerText !== "undefined")
429 				? "innerText"
430 				: (typeof sel.textContent !== "undefined")
431 					? "textContent"
432 					: "innerHTML";
433 
434 			oldValue = sel[type];
435 			
436 			if(set)
437 			{
438 				sel[type] = value;
439 				return value;
440 			}
441 			else
442 			{
443 				return oldValue;
444 			}
445 		},
446 		/**
447 		 * Sets or retrieves a css property of the element
448 		 * specified by the current selector. If a value is 
449 		 * passed, it will set that value on the current element,
450 		 * otherwise it will return the value of the css property
451 		 * on the current element
452 		 *
453 		 * @name css
454 		 * @memberOf $_.util
455 		 * @function
456 		 * @param string property
457 		 * @param [string] value
458 		 * @returns string
459 		 * @type string
460 		 */
461 		css: function (prop, val)
462 		{
463 			//Return the current value if a value is not set
464 			if(typeof val === "undefined")
465 			{
466 				return _css(this.el, prop);
467 			}
468 		
469 			$_.each(function (e){
470 				_css(e, prop, val);
471 			});
472 		}
473 	};
474 
475 	$_.ext('dom', d);
476 	
477 }());
478