com-admob-plugin-pgp
Version:
Google AdMob plugin for Cordova, Phonegap and Intel XDK ,support ios and android,support admob v1 and admob v2 ,Monetize your app with one javascript line.simple and easy to use api.build on admob ios 7.6 and admob android sdk 8 project home:https
1,291 lines (1,200 loc) • 118 kB
JavaScript
/*! intel-appframework - v2.1.0 - 2014-03-25 */
/**
* App Framework query selector class for HTML5 mobile apps on a WebkitBrowser.
* Since most mobile devices (Android, iOS, webOS) use a WebKit browser, you only need to target one browser.
* We are able to increase the speed greatly by removing support for legacy desktop browsers and taking advantage of browser features, like native JSON parsing and querySelectorAll
* MIT License
* @author Intel
* @copyright Intel
* @api private
*/
/* jshint eqeqeq:false */
/* global af: false */
if (!window.af || typeof(af) !== "function") {
/**
* This is our master af object that everything is built upon.
* $ is a pointer to this object
* @title appframework
* @api private
*/
var af = (function(window) {
"use strict";
var nundefined, document = window.document,
emptyArray = [],
slice = emptyArray.slice,
classCache = {},
eventHandlers = [],
_eventID = 1,
jsonPHandlers = [],
_jsonPID = 1,
fragmentRE = /<(\w+)[^>]*>/,
classSelectorRE = /^\.([\w-]+)$/,
tagSelectorRE = /^[\w-]+$/,
_attrCache = {},
_propCache = {},
cssNumber = {
"columncount": true,
"fontweight": true,
"lineheight": true,
"column-count": true,
"font-weight": true,
"line-height": true,
"opacity": true,
"orphans": true,
"widows": true,
"zIndex": true,
"z-index": true,
"zoom": true
},
isWin8 = (typeof(MSApp) === "object");
/**
* internal function used for $().css - checks to see if it is a number and the css property
* needs "px" added to it
* @api private
*/
function _addPx(prop, val) {
return (typeof(val) === "number") && !cssNumber[prop.toLowerCase()] ? val + "px" : val;
}
/**
* internal function to use domfragments for insertion
*
* @api private
*/
function _insertFragments(afm, container, insert) {
var frag = document.createDocumentFragment();
if (insert) {
for (var j = afm.length - 1; j >= 0; j--) {
frag.insertBefore(afm[j], frag.firstChild);
}
container.insertBefore(frag, container.firstChild);
} else {
for (var k = 0; k < afm.length; k++)
frag.appendChild(afm[k]);
container.appendChild(frag);
}
frag = null;
}
/**
* Internal function to test if a class name fits in a regular expression
* @param {String} name to search against
* @return {Boolean}
* @api private
*/
function classRE(name) {
return name in classCache ? classCache[name] : (classCache[name] = new RegExp("(^|\\s)" + name + "(\\s|$)"));
}
/**
* Internal function that returns a array of unique elements
* @param {Array} arr array to compare against
* @return {Array} array of unique elements
* @api private
*/
function unique(arr) {
for (var i = 0; i < arr.length; i++) {
if (arr.indexOf(arr[i]) !== i) {
arr.splice(i, 1);
i--;
}
}
return arr;
}
/**
* Given a set of nodes, it returns them as an array. Used to find
* siblings of an element
* @param {Nodelist} Node list to search
* @param {Object} [element] to find siblings off of
* @return {Array} array of sibblings
* @api private
*/
function siblings(nodes, element) {
var elems = [];
if (nodes == nundefined)
return elems;
for (; nodes; nodes = nodes.nextSibling) {
if (nodes.nodeType === 1 && nodes !== element) {
elems.push(nodes);
}
}
return elems;
}
/**
* This is the internal appframework object that gets extended and added on to it
* This is also the start of our query selector engine
* @param {String|Element|Object|Array} selector
* @param {String|Element|Object} [context]
* @api private
*/
var $afm = function(toSelect, what) {
this.length = 0;
if (!toSelect) {
return this;
} else if (toSelect instanceof $afm && what == nundefined) {
return toSelect;
} else if (af.isFunction(toSelect)) {
return af(document).ready(toSelect);
} else if (af.isArray(toSelect) && toSelect.length != nundefined) { //Passing in an array or object
for (var i = 0; i < toSelect.length; i++)
this[this.length++] = toSelect[i];
return this;
} else if (af.isObject(toSelect) && af.isObject(what)) { //var tmp=$("span"); $("p").find(tmp);
if (toSelect.length == nundefined) {
if (toSelect.parentNode == what)
this[this.length++] = toSelect;
} else {
for (var j = 0; j < toSelect.length; j++)
if (toSelect[j].parentNode == what)
this[this.length++] = toSelect[j];
}
return this;
} else if (af.isObject(toSelect) && what == nundefined) { //Single object
this[this.length++] = toSelect;
return this;
} else if (what !== nundefined) {
if (what instanceof $afm) {
return what.find(toSelect);
}
} else {
what = document;
}
return this.selector(toSelect, what);
};
/**
* This calls the $afm function
* @param {String|Element|Object|Array} selector
* @param {String|Element|Object} [context]
* @title $()
* @return {Object} an appframework object
*/
var $ = function(selector, what) {
return new $afm(selector, what);
};
/**
* this is the engine for "all" and is only exposed internally
* @api private
*/
function _selectorAll(selector, what) {
try {
return what.querySelectorAll(selector);
} catch (e) {
return [];
}
}
/**
* this is the query selector engine for elements
* @param {String} selector
* @param {String|Element|Object} [context]
* @api private
*/
function _selector(selector, what) {
/*jshint validthis:true*/
selector = selector.trim();
if (selector[0] === "#" && selector.indexOf(".") === -1 &&selector.indexOf(",") === -1 && selector.indexOf(" ") === -1 && selector.indexOf(">") === -1) {
if (what === document)
_shimNodes(what.getElementById(selector.replace("#", "")), this);
else
_shimNodes(_selectorAll(selector, what), this);
} else if ((selector[0] === "<" && selector[selector.length - 1] === ">") || (selector.indexOf("<") !== -1 && selector.indexOf(">") !== -1)) //html
{
var tmp = document.createElement("div");
if (isWin8) {
MSApp.execUnsafeLocalFunction(function() {
tmp.innerHTML = selector.trim();
});
} else
tmp.innerHTML = selector.trim();
_shimNodes(tmp.childNodes, this);
} else {
_shimNodes((_selectorAll(selector, what)), this);
}
return this;
}
function _shimNodes(nodes, obj) {
if (!nodes)
return;
if (nodes.nodeType) {
obj[obj.length++] = nodes;
return;
}
for (var i = 0, iz = nodes.length; i < iz; i++)
obj[obj.length++] = nodes[i];
}
/**
* Checks to see if the parameter is a $afm object
```
var foo=$("#header");
$.is$(foo);
```
* @param {*} obj
* @return {boolean}
* @title $.is$(param)
*/
$.is$ = function(obj) {
return (obj instanceof $afm);
};
/**
* Map takes in elements and executes a callback function on each and returns a collection
```
$.map([1,2],function(ind){return ind+1});
```
* @param {Array|Object} elements
* @param {Function} callback
* @return {array} array with elements
* @title $.map(elements,callback)
*/
$.map = function(elements, callback) {
var value, values = [],
i, key;
if ($.isArray(elements)){
for (i = 0; i < elements.length; i++) {
value = callback.apply(elements[i],[elements[i],i]);
if (value !== nundefined)
values.push(value);
}
} else if ($.isObject(elements)){
for (key in elements) {
if (!elements.hasOwnProperty(key) || key === "length")
continue;
value = callback(elements[key],[elements[key],key]);
if (value !== nundefined)
values.push(value);
}
}
return values;
};
/**
* Iterates through elements and executes a callback. Returns if false
```
$.each([1,2],function(ind){console.log(ind);});
```
* @param {Array|Object} elements
* @param {Function} callback
* @return {Array} elements
* @title $.each(elements,callback)
*/
$.each = function(elements, callback) {
var i, key;
if ($.isArray(elements)){
for (i = 0; i < elements.length; i++) {
if (callback(i, elements[i]) === false)
return elements;
}
} else if ($.isObject(elements)){
for (key in elements) {
if (!elements.hasOwnProperty(key) || key === "length")
continue;
if (callback(key, elements[key]) === false)
return elements;
}
}
return elements;
};
/**
* Extends an object with additional arguments
```
$.extend({foo:"bar"});
$.extend(element,{foo:"bar"});
```
* @param {Object} [target] element
* @param any number of additional arguments
* @return {Object} [target]
* @title $.extend(target,{params})
*/
$.extend = function(target) {
if (target == nundefined)
target = this;
if (arguments.length === 1) {
for (var key in target)
this[key] = target[key];
return this;
} else {
slice.call(arguments, 1).forEach(function(source) {
for (var key in source)
target[key] = source[key];
});
}
return target;
};
/**
* Checks to see if the parameter is an array
```
var arr=[];
$.isArray(arr);
```
* @param {*} obj
* @return {boolean}
* @example $.isArray([1]);
* @title $.isArray(param)
*/
$.isArray = function(obj) {
return obj instanceof Array && obj.push != nundefined; //ios 3.1.3 doesn"t have Array.isArray
};
/**
* Checks to see if the parameter is a function
```
var func=function(){};
$.isFunction(func);
```
* @param {*} obj
* @return {boolean}
* @title $.isFunction(param)
*/
$.isFunction = function(obj) {
return typeof obj === "function" && !(obj instanceof RegExp);
};
/**
* Checks to see if the parameter is a object
```
var foo={bar:"bar"};
$.isObject(foo);
```
* @param {*} obj
* @returns {boolean}
* @title $.isObject(param)
*/
$.isObject = function(obj) {
return typeof obj === "object" && obj !== null;
};
/**
* Prototype for afm object. Also extends $.fn
*/
$.fn = $afm.prototype = {
namespace: "appframework",
constructor: $afm,
forEach: emptyArray.forEach,
reduce: emptyArray.reduce,
push: emptyArray.push,
indexOf: emptyArray.indexOf,
concat: emptyArray.concat,
selector: _selector,
oldElement: undefined,
slice: emptyArray.slice,
length: 0,
/**
* This is a utility function for .end()
* @param {Object} params
* @return {Object} an appframework with params.oldElement set to this
* @api private
*/
setupOld: function(params) {
if (params == nundefined)
return $();
params.oldElement = this;
return params;
},
/**
* This is a wrapper to $.map on the selected elements
```
$().map(function(){this.value+=ind});
```
* @param {Function} callback
* @return {Object} an appframework object
* @title $().map(function)
*/
map: function(fn) {
var value, values = [],
i;
for (i = 0; i < this.length; i++) {
value = fn.apply(this[i],[i,this[i]]);
if (value !== nundefined)
values.push(value);
}
return $(values);
},
/**
* Iterates through all elements and applys a callback function
```
$().each(function(){console.log(this.value)});
```
* @param {Function} callback
* @return {Object} an appframework object
* @title $().each(function)
*/
each: function(callback) {
this.forEach(function(el, idx) {
callback.call(el, idx, el);
});
return this;
},
/**
* This is executed when DOMContentLoaded happens, or after if you"ve registered for it.
```
$(document).ready(function(){console.log("I'm ready");});
```
* @param {Function} callback
* @return {Object} an appframework object
* @title $().ready(function)
*/
ready: function(callback) {
if (document.readyState === "complete" || document.readyState === "loaded" || (!$.os.ie && document.readyState === "interactive")) //IE10 fires interactive too early
callback();
else
document.addEventListener("DOMContentLoaded", callback, false);
return this;
},
/**
* Searches through the collection and reduces them to elements that match the selector
```
$("#foo").find(".bar");
$("#foo").find($(".bar"));
$("#foo").find($(".bar").get(0));
```
* @param {String|Object|Array} selector
* @return {Object} an appframework object filtered
* @title $().find(selector)
*/
find: function(sel) {
if (this.length === 0)
return this;
var elems = [];
var tmpElems;
for (var i = 0; i < this.length; i++) {
tmpElems = ($(sel, this[i]));
for (var j = 0; j < tmpElems.length; j++) {
elems.push(tmpElems[j]);
}
}
return $(unique(elems));
},
/**
* Gets or sets the innerHTML for the collection.
* If used as a get, the first elements innerHTML is returned
```
$("#foo").html(); //gets the first elements html
$("#foo").html("new html");//sets the html
$("#foo").html("new html",false); //Do not do memory management cleanup
```
* @param {String} html to set
* @param {Bool} [cleanup] - set to false for performance tests and if you do not want to execute memory management cleanup
* @return {Object} an appframework object
* @title $().html([html])
*/
html: function(html, cleanup) {
var msFix=function(){
item.innerHTML=html;
};
if (this.length === 0)
return this;
if (html === nundefined)
return this[0].innerHTML;
for (var i = 0; i < this.length; i++) {
if (cleanup !== false)
$.cleanUpContent(this[i], false, true);
if (isWin8) {
var item=this[i];
MSApp.execUnsafeLocalFunction(msFix);
} else
this[i].innerHTML = html;
}
return this;
},
/**
* Gets or sets the innerText for the collection.
* If used as a get, the first elements innerText is returned
```
$("#foo").text(); //gets the first elements text;
$("#foo").text("new text"); //sets the text
```
* @param {String} text to set
* @return {Object} an appframework object
* @title $().text([text])
*/
text: function(text) {
if (this.length === 0)
return this;
if (text === nundefined)
return this[0].textContent;
for (var i = 0; i < this.length; i++) {
this[i].textContent = text;
}
return this;
},
/**
* Gets or sets a css property for the collection
* If used as a get, the first elements css property is returned
* This will add px to properties that need it.
```
$().css("background"); // Gets the first elements background
$().css("background","red") //Sets the elements background to red
```
* @param {String} attribute The attribute to get
* @param {String} value Value to set as
* @return {Object} obj An appframework object
* @title $().css(attribute,[value])
*/
css: function(attribute, value, obj) {
var toAct = obj != nundefined ? obj : this[0];
if (this.length === 0)
return this;
if (value == nundefined && typeof(attribute) === "string") {
var styles = window.getComputedStyle(toAct);
return toAct.style[attribute] ? toAct.style[attribute] : window.getComputedStyle(toAct)[attribute];
}
for (var i = 0; i < this.length; i++) {
if ($.isObject(attribute)) {
for (var j in attribute) {
this[i].style[j] = _addPx(j, attribute[j]);
}
} else {
this[i].style[attribute] = _addPx(attribute, value);
}
}
return this;
},
/**
* Gets or sets css vendor specific css properties
* If used as a get, the first elements css property is returned
```
$().vendorCss("transform"); // Gets the first elements background
$().vendorCss("transform","Translate3d(0,40,0)") //Sets the elements background to red
```
* @param {String} attribute to get
* @param {String} value to set as
* @return {Object} an appframework object
* @title $().vendorCss(attribute,[value])
*/
vendorCss: function(attribute, value, obj) {
return this.css($.feat.cssPrefix + attribute, value, obj);
},
/**
* Performs a css vendor specific transform:translate operation on the collection.
```
$("#main").cssTranslate("200px,0,0");
```
* @param {String} Transform values
* @return {Object} an appframework object
* @title $().cssTranslate(value)
*/
cssTranslate: function(val) {
return this.vendorCss("Transform", "translate" + $.feat.cssTransformStart + val + $.feat.cssTransformEnd);
},
/**
* Gets the computed style of CSS values
```
$("#main").computedStyle("display");
```
* @param {String} css property
* @return {Int|String|Float|} css vlaue
* @title $().computedStyle()
*/
computedStyle: function(val) {
if (this.length === 0 || val == nundefined) return;
return window.getComputedStyle(this[0], "")[val];
},
/**
* Sets the innerHTML of all elements to an empty string
```
$().empty();
```
* @return {Object} an appframework object
* @title $().empty()
*/
empty: function() {
for (var i = 0; i < this.length; i++) {
$.cleanUpContent(this[i], false, true);
this[i].textContent = "";
}
return this;
},
/**
* Sets the elements display property to "none".
* This will also store the old property into an attribute for hide
```
$().hide();
```
* @return {Object} an appframework object
* @title $().hide()
*/
hide: function() {
if (this.length === 0)
return this;
for (var i = 0; i < this.length; i++) {
if (this.css("display", null, this[i]) !== "none") {
this[i].setAttribute("afmOldStyle", this.css("display", null, this[i]));
this[i].style.display = "none";
}
}
return this;
},
/**
* Shows all the elements by setting the css display property
* We look to see if we were retaining an old style (like table-cell) and restore that, otherwise we set it to block
```
$().show();
```
* @return {Object} an appframework object
* @title $().show()
*/
show: function() {
if (this.length === 0)
return this;
for (var i = 0; i < this.length; i++) {
if (this.css("display", null, this[i]) === "none") {
this[i].style.display = this[i].getAttribute("afmOldStyle") ? this[i].getAttribute("afmOldStyle") : "block";
this[i].removeAttribute("afmOldStyle");
}
}
return this;
},
/**
* Toggle the visibility of a div
```
$().toggle();
$().toggle(true); //force showing
```
* @param {Boolean} [show] -force the hiding or showing of the element
* @return {Object} an appframework object
* @title $().toggle([show])
*/
toggle: function(show) {
if(this.length === 0)
return this;
var show2 = show===true;
for (var i = 0; i < this.length; i++) {
if (this.css("display", null, this[i]) !== "none" && (show == nundefined || show2 === false)) {
this[i].setAttribute("afmOldStyle", this.css("display", null, this[i]));
this[i].style.display = "none";
} else if (this.css("display", null, this[i]) === "none" && (show == nundefined || show2 === true)) {
this[i].style.display = this[i].getAttribute("afmOldStyle") ? this[i].getAttribute("afmOldStyle") : "block";
this[i].removeAttribute("afmOldStyle");
}
}
return this;
},
/**
* Gets or sets an elements value
* If used as a getter, we return the first elements value. If nothing is in the collection, we return undefined
```
$().value; //Gets the first elements value;
$().value="bar"; //Sets all elements value to bar
```
* @param {String} [value] to set
* @return {String|Object} A string as a getter, appframework object as a setter
* @title $().val([value])
*/
val: function(value) {
if (this.length === 0)
return (value === nundefined) ? undefined : this;
if (value == nundefined)
return this[0].value;
for (var i = 0; i < this.length; i++) {
this[i].value = value;
}
return this;
},
/**
* Gets or sets an attribute on an element
* If used as a getter, we return the first elements value. If nothing is in the collection, we return undefined
```
$().attr("foo"); //Gets the first elements "foo" attribute
$().attr("foo","bar");//Sets the elements "foo" attribute to "bar"
$().attr("foo",{bar:"bar"}) //Adds the object to an internal cache
```
* @param {String|Object} attribute to act upon. If it is an object (hashmap), it will set the attributes based off the kvp.
* @param {String|Array|Object|function} [value] to set
* @return {String|Object|Array|Function} If used as a getter, return the attribute value. If a setter, return an appframework object
* @title $().attr(attribute,[value])
*/
attr: function(attr, value) {
if (this.length === 0)
return (value === nundefined) ? undefined : this;
if (value === nundefined && !$.isObject(attr)) {
var val = (this[0].afmCacheId && _attrCache[this[0].afmCacheId] && _attrCache[this[0].afmCacheId][attr]) ? _attrCache[this[0].afmCacheId][attr] : this[0].getAttribute(attr);
return val;
}
for (var i = 0; i < this.length; i++) {
if ($.isObject(attr)) {
for (var key in attr) {
$(this[i]).attr(key, attr[key]);
}
} else if ($.isArray(value) || $.isObject(value) || $.isFunction(value)) {
if (!this[i].afmCacheId)
this[i].afmCacheId = $.uuid();
if (!_attrCache[this[i].afmCacheId])
_attrCache[this[i].afmCacheId] = {};
_attrCache[this[i].afmCacheId][attr] = value;
} else if (value === null) {
this[i].removeAttribute(attr);
if (this[i].afmCacheId && _attrCache[this[i].afmCacheId][attr])
delete _attrCache[this[i].afmCacheId][attr];
} else {
this[i].setAttribute(attr, value);
if (this[i].afmCacheId && _attrCache[this[i].afmCacheId][attr])
delete _attrCache[this[i].afmCacheId][attr];
}
}
return this;
},
/**
* Removes an attribute on the elements
```
$().removeAttr("foo");
```
* @param {String} attributes that can be space delimited
* @return {Object} appframework object
* @title $().removeAttr(attribute)
*/
removeAttr: function(attr) {
var removeFixer=function(param) {
that[i].removeAttribute(param);
if (that[i].afmCacheId && _attrCache[that[i].afmCacheId])
delete _attrCache[that[i].afmCacheId][attr];
};
var that = this;
for (var i = 0; i < this.length; i++) {
attr.split(/\s+/g).forEach(removeFixer);
}
return this;
},
/**
* Gets or sets a property on an element
* If used as a getter, we return the first elements value. If nothing is in the collection, we return undefined
```
$().prop("foo"); //Gets the first elements "foo" property
$().prop("foo","bar");//Sets the elements "foo" property to "bar"
$().prop("foo",{bar:"bar"}) //Adds the object to an internal cache
```
* @param {String|Object} property to act upon. If it is an object (hashmap), it will set the attributes based off the kvp.
* @param {String|Array|Object|function} [value] to set
* @return {String|Object|Array|Function} If used as a getter, return the property value. If a setter, return an appframework object
* @title $().prop(property,[value])
*/
prop: function(prop, value) {
if (this.length === 0)
return (value === nundefined) ? undefined : this;
if (value === nundefined && !$.isObject(prop)) {
var res;
var val = (this[0].afmCacheId && _propCache[this[0].afmCacheId] && _propCache[this[0].afmCacheId][prop]) ? _propCache[this[0].afmCacheId][prop] : !(res = this[0][prop]) && prop in this[0] ? this[0][prop] : res;
return val;
}
for (var i = 0; i < this.length; i++) {
if ($.isObject(prop)) {
for (var key in prop) {
$(this[i]).prop(key, prop[key]);
}
} else if ($.isArray(value) || $.isObject(value) || $.isFunction(value)) {
if (!this[i].afmCacheId)
this[i].afmCacheId = $.uuid();
if (!_propCache[this[i].afmCacheId])
_propCache[this[i].afmCacheId] = {};
_propCache[this[i].afmCacheId][prop] = value;
} else if (value === null && value !== undefined) {
$(this[i]).removeProp(prop);
} else {
$(this[i]).removeProp(prop);
this[i][prop] = value;
}
}
return this;
},
/**
* Removes a property on the elements
```
$().removeProp("foo");
```
* @param {String} properties that can be space delimited
* @return {Object} appframework object
* @title $().removeProp(attribute)
*/
removeProp: function(prop) {
var removePropFn=function(param) {
if (that[i][param])
that[i][param] = undefined;
if (that[i].afmCacheId && _propCache[that[i].afmCacheId]) {
delete _propCache[that[i].afmCacheId][prop];
}
};
var that = this;
for (var i = 0; i < this.length; i++) {
prop.split(/\s+/g).forEach(removePropFn);
}
return this;
},
/**
* Removes elements based off a selector
```
$().remove(); //Remove all
$().remove(".foo");//Remove off a string selector
var element=$("#foo").get(0);
$().remove(element); //Remove by an element
$().remove($(".foo")); //Remove by a collection
```
* @param {String|Object|Array} selector to filter against
* @return {Object} appframework object
* @title $().remove(selector)
*/
remove: function(selector) {
var elems = $(this).filter(selector);
if (elems == nundefined)
return this;
for (var i = 0; i < elems.length; i++) {
$.cleanUpContent(elems[i], true, true);
if (elems[i] && elems[i].parentNode) {
elems[i].parentNode.removeChild(elems[i]);
}
}
return this;
},
/**
* Adds a css class to elements.
```
$().addClass("selected");
```
* @param {String} classes that are space delimited
* @return {Object} appframework object
* @title $().addClass(name)
*/
addClass: function(name) {
var addClassLoop=function(cname) {
if (!that.hasClass(cname, that[i]))
classList.push(cname);
};
if (name == nundefined) return this;
for (var i = 0; i < this.length; i++) {
var cls = this[i].className;
var classList = [];
var that = this;
name.split(/\s+/g).forEach(addClassLoop);
this[i].className += (cls ? " " : "") + classList.join(" ");
this[i].className = this[i].className.trim();
}
return this;
},
/**
* Removes a css class from elements.
```
$().removeClass("foo"); //single class
$().removeClass("foo selected");//remove multiple classess
```
* @param {String} classes that are space delimited
* @return {Object} appframework object
* @title $().removeClass(name)
*/
removeClass: function(name) {
if (name == nundefined) return this;
var removeClassLoop=function(cname) {
classList = classList.replace(classRE(cname), " ");
};
for (var i = 0; i < this.length; i++) {
if (name == nundefined) {
this[i].className = "";
return this;
}
var classList = this[i].className;
//SGV LINK EVENT
if (typeof this[i].className === "object") {
classList = " ";
}
name.split(/\s+/g).forEach(removeClassLoop);
if (classList.length > 0)
this[i].className = classList.trim();
else
this[i].className = "";
}
return this;
},
/**
* Adds or removes a css class to elements.
```
$().toggleClass("selected");
```
* @param {String} classes that are space delimited
* @param {Boolean} [state] force toggle to add or remove classes
* @return {Object} appframework object
* @title $().toggleClass(name)
*/
toggleClass: function(name, state) {
if (name == nundefined) return this;
for (var i = 0; i < this.length; i++) {
if (typeof state !== "boolean") {
state = this.hasClass(name, this[i]);
}
$(this[i])[state ? "removeClass" : "addClass"](name);
}
return this;
},
/**
* Replaces a css class on elements.
```
$().replaceClass("on", "off");
```
* @param {String} classes that are space delimited
* @param {String} classes that are space delimited
* @return {Object} appframework object
* @title $().replaceClass(old, new)
*/
replaceClass: function(name, newName) {
if (name == nundefined || newName == nundefined) return this;
var replaceClassFn=function(cname) {
classList = classList.replace(classRE(cname), " ");
};
for (var i = 0; i < this.length; i++) {
if (name == nundefined) {
this[i].className = newName;
continue;
}
var classList = this[i].className;
name.split(/\s+/g).concat(newName.split(/\s+/g)).forEach(replaceClassFn);
classList = classList.trim();
if (classList.length > 0) {
this[i].className = (classList + " " + newName).trim();
} else
this[i].className = newName;
}
return this;
},
/**
* Checks to see if an element has a class.
```
$().hasClass("foo");
$().hasClass("foo",element);
```
* @param {String} class name to check against
* @param {Object} [element] to check against
* @return {Boolean}
* @title $().hasClass(name,[element])
*/
hasClass: function(name, element) {
if (this.length === 0)
return false;
if (!element)
element = this[0];
return classRE(name).test(element.className);
},
/**
* Appends to the elements
* We boil everything down to an appframework object and then loop through that.
* If it is HTML, we create a dom element so we do not break event bindings.
* if it is a script tag, we evaluate it.
```
$().append("<div></div>"); //Creates the object from the string and appends it
$().append($("#foo")); //Append an object;
```
* @param {String|Object} Element/string to add
* @param {String|Object} Element/string to add
* @param {Boolean} [insert] insert or append
* @return {Object} appframework object
* @title $().append(element,[insert],[content])
*/
append: function(element, content,insert) {
if (element && element.length != nundefined && element.length === 0)
return this;
if ($.isArray(element) || $.isObject(element))
element = $(element);
var i, node;
if(content)
$(this).add(content);
for (i = 0; i < this.length; i++) {
if (element.length && typeof element !== "string") {
element = $(element);
_insertFragments(element, this[i], insert);
} else {
var obj = fragmentRE.test(element) ? $(element) : undefined;
if (obj == nundefined || obj.length === 0) {
obj = document.createTextNode(element);
}
if (obj instanceof $afm) {
for (var k=0,lenk=obj.length; k<lenk; k++) {
node = obj[k];
if (node.nodeName != nundefined && node.nodeName.toLowerCase() === "script" && (!node.type || node.type.toLowerCase() === "text/javascript")) {
window["eval"](node.innerHTML);
} else {
_insertFragments($(node), this[i], insert);
}
}
} else {
insert != nundefined ? this[i].insertBefore(obj, this[i].firstChild) : this[i].appendChild(obj);
}
}
}
return this;
},
/**
* Appends the current collection to the selector
```
$().appendTo("#foo"); //Append an object;
```
* @param {String|Object} Selector to append to
* @param {Boolean} [insert] insert or append
* @title $().appendTo(element,[insert])
*/
appendTo: function(selector, insert) {
var tmp = $(selector);
tmp.append(this);
return this;
},
/**
* Prepends the current collection to the selector
```
$().prependTo("#foo"); //Prepend an object;
```
* @param {String|Object} Selector to prepent to
* @title $().prependTo(element)
*/
prependTo: function(selector) {
var tmp = $(selector);
tmp.append(this, null,true);
return this;
},
/**
* Prepends to the elements
* This simply calls append and sets insert to true
```
$().prepend("<div></div>");//Creates the object from the string and appends it
$().prepend($("#foo")); //Prepends an object
```
* @param {String|Object} Element/string to add
* @return {Object} appframework object
* @title $().prepend(element)
*/
prepend: function(element) {
return this.append(element,null, 1);
},
/**
* Inserts collection before the target (adjacent)
```
$().insertBefore(af("#target"));
```
* @param {String|Object} Target
* @title $().insertBefore(target);
*/
insertBefore: function(target, after) {
if (this.length === 0)
return this;
target = $(target).get(0);
if (!target)
return this;
for (var i = 0; i < this.length; i++) {
after ? target.parentNode.insertBefore(this[i], target.nextSibling) : target.parentNode.insertBefore(this[i], target);
}
return this;
},
/**
* Inserts collection after the target (adjacent)
```
$().insertAfter(af("#target"));
```
* @param {String|Object} target
* @title $().insertAfter(target);
*/
insertAfter: function(target) {
this.insertBefore(target, true);
},
/**
* Returns the raw DOM element.
```
$().get(0); //returns the first element
$().get(2);// returns the third element
```
* @param {Int} [index]
* @return {Object} raw DOM element
* @title $().get([index])
*/
get: function(index) {
index = index == nundefined ? null : index;
if (index < 0)
index += this.length;
if(index===null){
var elems=[];
for(var i=0;i<this.length;i++){
elems.push(this[i]);
}
return elems;
}
return (this[index]) ? this[index] : undefined;
},
/**
* Returns the offset of the element, including traversing up the tree
```
$().offset();
```
* @return {Object} with left, top, width and height properties
* @title $().offset()
*/
offset: function() {
var obj;
if (this.length === 0)
return this;
if (this[0] === window)
return {
left: 0,
top: 0,
right: 0,
bottom: 0,
width: window.innerWidth,
height: window.innerHeight
};
else
obj = this[0].getBoundingClientRect();
return {
left: obj.left + window.pageXOffset,
top: obj.top + window.pageYOffset,
right: obj.right + window.pageXOffset,
bottom: obj.bottom + window.pageYOffset,
width: obj.right - obj.left,
height: obj.bottom - obj.top
};
},
/**
* returns the height of the element, including padding on IE
```
$().height();
```
* @return {string} height
* @title $().height()
*/
height: function(val) {
if (this.length === 0)
return this;
if (val != nundefined)
return this.css("height", val);
if (this[0] === this[0].window)
return window.innerHeight;
if (this[0].nodeType === this[0].DOCUMENT_NODE)
return this[0].documentElement.offsetHeight;
else {
var tmpVal = this.css("height").replace("px", "");
if (tmpVal)
return +tmpVal;
else
return this.offset().height;
}
},
/**
* returns the width of the element, including padding on IE
```
$().width();
```
* @return {string} width
* @title $().width()
*/
width: function(val) {
if (this.length === 0)
return this;
if (val != nundefined)
return this.css("width", val);
if (this[0] === this[0].window)
return window.innerWidth;
if (this[0].nodeType === this[0].DOCUMENT_NODE)
return this[0].documentElement.offsetWidth;
else {
var tmpVal = this.css("width").replace("px", "");
if (tmpVal)
return +tmpVal;
else
return this.offset().width;
}
},
/**
* Returns the parent nodes of the elements based off the selector
```
$("#foo").parent(".bar");
$("#foo").parent($(".bar"));
$("#foo").parent($(".bar").get(0));
```
* @param {String|Array|Object} [selector]
* @return {Object} appframework object with unique parents
* @title $().parent(selector)
*/
parent: function(selector, recursive) {
if (this.length === 0)
return this;