commit 86e4c56268bedd30dab1f2a06b9d13e18e2d4c83 Author: a-Sansara Date: Wed Dec 16 02:25:54 2015 +0100 initial commit diff --git a/demo.html b/demo.html new file mode 100644 index 0000000..a137239 --- /dev/null +++ b/demo.html @@ -0,0 +1,119 @@ + + + + pluie.org - svan demo - v0.4 + + + + +

pluie.org - svan demo :

+ + + + + +
+ + + + diff --git a/src/svan-min.js b/src/svan-min.js new file mode 100644 index 0000000..1537493 --- /dev/null +++ b/src/svan-min.js @@ -0,0 +1 @@ +/* by a-sansara - https://github.com/pluie-org/svan */!function(){var t=function(t,n){return typeof t==n},n=function(n){return t(n,"undefined")},i=function(n){return t(n,"string")},e=function(n){return t(n,"function")},s=function(n){return t(n,"object")},o=function(t,n){return new o.init(t,n)};o.prototype={regsan:function(t){return t.replace(/[-\/\\^$*+?.()|[\]{}]/g,"\\$&")},first:function(){return this.found?this.list[0]:null},last:function(){return this.found?this.list[this.list.length-1]:b},index:function(t){return this.found&&t>0&&t0,this};a.prototype=o.prototype,window.Svan=o,n(window.$)&&(window.$=o)}(); diff --git a/src/svan.js b/src/svan.js new file mode 100644 index 0000000..c6dea51 --- /dev/null +++ b/src/svan.js @@ -0,0 +1,186 @@ +/*! + * @author : a-Sansara + * @url : https://github.com/pluie-org/svan + * @contributors : + * @copyright : pluie.org + * @date : 2015-12-14 02:45:17 + * @version : 0.4 + * @license : MIT + * @require : html5 + * @desc : Small Vanilla jQuery + */ +(function() { + + var is = function(o, intent) { return typeof o == intent; }, + isNone = function(o) { return is(o, 'undefined'); }, + isStr = function(o) { return is(o, 'string'); }, + isFunc = function(o) { return is(o, 'function'); }, + isObj = function(o) { return is(o, 'object'); }, + Svan = function (selector, context) { + return new Svan.init(selector, context); + }; + + Svan.prototype = { + regsan : function (v) { + return v.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); + }, + first : function() { + return this.found ? this.list[0] : null; + }, + last : function() { + return this.found ? this.list[this.list.length-1] : b; + }, + index : function(i) { + return this.found && i > 0 && i < this.list.length ? this.list[i] : b; + }, + all : function(i) { + return this.list; + }, + // assume uniq selector + find : function(s) { + return this.found ? [].slice.call(this.list[0].querySelectorAll(s)) : []; + }, + foreach : function(f) { + if (this.found) this.list.forEach(f); + }, + // Living Standard cf https://w3c.github.io/DOM-Parsing/#innerhtml + html : function(data) { + if (!data) return this.found ? this.list[0].innerHTML : ''; // assume uniq selector + else this.foreach(function(node) { + node.innerHTML = data; + }); + }, + append : function(data) { + this.foreach(function(node) { + node.innerHTML += data; + }); + }, + on : function(type, fn, capture) { + this.foreach(function(node) { + node.addEventListener(type, fn, capture===true); + }); + }, + val : function(data) { + if (!data) return this.found ? this.list[0].value : null; // assume uniq selector + else this.foreach(function(node) { + node.value = data; + }); + }, + attr : function(key, value) { + if (arguments.length == 1) return this.found ? this.list[0].getAttribute(key) : null; // assume uniq selector + else this.foreach(function(node) { + node.setAttribute(key, value); + }); + }, + toggle : function(cssName) { + this.foreach(function(node) { + node.classList.toggle(cssName); + }); + }, + // assume uniq selector + hasClass : function(cssName) { + return this.found ? this.list[0].contains(cssName) : this.found; + }, + removeClass : function(cssName) { + this.foreach(function(node) { + if (node.classList.contains(cssName)) node.classList.toggle(cssName); + }); + }, + addClass : function(cssName) { + this.foreach(function(node) { + if (!node.classList.contains(cssName)) node.classList.toggle(cssName); + }); + }, + fadeIn : function(duration, fn, display) { + if (this.found) { + if (!duration) duration = this.FADE_DURATION; + var inc = parseFloat(1.0/duration*20); + var n = this.first(); + n.style.opacity = 0; + n.style.display = display || "block"; + (function fade(duration) { + var val = parseFloat(n.style.opacity); + if ((val += inc) < 1) { + n.style.opacity = val; + requestAnimationFrame(fade); + } + else if (typeof fn == "function") fn.call(n); + }(duration)); + } + }, + fadeOut : function(duration, fn) { + if (this.found) { + if (!duration) duration = this.FADE_DURATION; + var inc = parseFloat(1.0/duration*20); + var n = this.first(); + n.style.opacity = 1; + (function fade(duration) { + var val = parseFloat(n.style.opacity); + if ((val -= inc) < 0) { + n.style.display = "none"; + if (typeof fn == "function") fn.call(n); + } else { + n.style.opacity = val; + requestAnimationFrame(fade); + } + }(duration)); + } + }, + ready : function(fn) { + this.context.addEventListener('DOMContentLoaded', fn); + } + }; + Svan.isNone = isNone; + Svan.isStr = isStr; + Svan.isObj = isObj; + Svan.isFunc = isFunc; + Svan.eachObj = function(obj, fn, context) { + for (var prop in obj) { + if (obj.hasOwnProperty(prop)) { + fn.call(context, prop, obj[prop]); + } + } + }; + Svan.ajax = function(def) { + this.eachObj(def, function(k, v) { + console.log(k); + console.log(v); + }); + var xhr = new XMLHttpRequest(); + xhr.onreadystatechange = function(data) { + if (this.readyState == 4) { + if(this.status==200) { + if (isFunc(def.done)) def.done.call(def.context, data, this.responseText, this.statusText); + } + else if (isFunc(def.fail)) def.fail.call(def.context, data, this.responseText, this.statusText); + if (isFunc(def.always)) def.always.call(def.context, data, this.responseText, this.statusText); + } + } + if (!isNone(def.timeout) && def.async) xhr.timeout = def.timeout; + if (isFunc(def.before)) def.before.call(xhr); + xhr.open(def.method, def.url, def.async); + xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); + var qs = ''; + if (isObj(def.data)) { + this.eachObj(def.data, function(k, v) { + qs += k+'='+encodeURIComponent(v); + }); + } + xhr.send(qs); + } + + var init = Svan.init = function(selector, context) { + this.FADE_DURATION = 700; + this.VERSION = 0.4; + this.context = isNone(context) ? document : context; + this.list = isStr(selector) ? [].slice.call(this.context.querySelectorAll(selector)) + : (isObj(selector) && !isNone(selector.nodeType) ? [selector] : []); + this.found = this.list.length > 0; + return this; + }; + + init.prototype = Svan.prototype; + window.Svan = Svan; + if (isNone(window.$)) window.$ = Svan; + +}());