diff --git a/src/framework/core.js b/src/framework/core.js index 4282935..991a933 100644 --- a/src/framework/core.js +++ b/src/framework/core.js @@ -1,141 +1,264 @@ -var Framework=function(el){ - return Framework.fn.init(el); -} -Framework.__proto__ = Framework.prototype = {}; -Framework.v={}; -Framework.f={}; -Framework.f.init=function(el){ +import * as CoreUtils from "helpers"; -}; -Framework.fn.extend=function(name,func){ - if((typeof name) == "string"){ - Framework.extend(Framework.fn,{name:func}); - }else if((typeof name) == "object"){ - for (var n in name){ - Framework.fn.extend(n,name[n]); - } +(function (global, factory) { + + "use strict"; + + if (typeof module === "object" && typeof module.exports === "object") { + module.exports = global.document ? + factory(global, true) : + function (w) { + if (!w.document) { + throw new Error("Framework requires a window with a document"); + } + return factory(w); + }; + } else { + factory(global); } - return Framework.fn; -}; -Framework.extend=function(name,func){ - if((typeof name) == "string"){ - Framework[name] = func; - }else if(typeof name === typeof func === "object"){ - $keys=Object.keys(func); - for(var i=0; i<$keys.length; i++){ - var n=$keys[i]; - var v=func[n]; - name[n] = v; - } + + // Pass this if window is not defined yet +})(typeof window !== "undefined" ? window : this, function (window, noGlobal) { + + "use strict"; + + var Framework = function (el) { + return Framework.fn.init(el); } - return Framework; -}; -Framework.extend(Framework.fn,{ - each: function(callback) { - if (callback && typeof(callback) == 'function') { - for (let i = 0; i < this.length; i++) { - callback(this[i], i); + Framework.__proto__ = Framework.prototype = {}; + Framework.v = {}; + Framework.f = {}; + Framework.f.init = function (el) { + + }; + Framework.fn.extend = function (name, func) { + if ((typeof name) == "string") { + Framework.extend(Framework.fn, { + name: func + }); + } else if ((typeof name) == "object") { + for (var n in name) { + Framework.fn.extend(n, name[n]); + } + } + return Framework.fn; + }; + Framework.extend = function (name, func) { + if ((typeof name) == "string") { + Framework[name] = func; + } else if (typeof name === typeof func === "object") { + $keys = Object.keys(func); + for (var i = 0; i < $keys.length; i++) { + var n = $keys[i]; + var v = func[n]; + name[n] = v; + } + } + return Framework; + }; + Framework.extend(Framework,{ + merge: function( first, second ) { + var len = +second.length, + j = 0, + i = first.length; + + for ( ; j < len; j++ ) { + first[ i++ ] = second[ j ]; + } + + first.length = i; + + return first; + }, + // results is for internal usage only + makeArray: function( arr, results ) { + var ret = results || []; + + if ( arr != null ) { + if ( isArrayLike( Object( arr ) ) ) { + Framework.merge( ret, + typeof arr === "string" ? + [ arr ] : arr + ); + } else { + Array.push.call( ret, arr ); + } + } + + return ret; + }, + }); + function isArrayLike( obj ) { + + var length = !!obj && "length" in obj && obj.length, + var type = CoreUtils.to_type(obj); + + if ( CoreUtils.is_function( obj ) || CoreUtils.is_window( obj ) ) { + return false; + } + + return type === "array" || length === 0 || + CoreUtils.is_number(length) && length > 0 && ( length - 1 ) in obj; + } + Framework.extend(Framework.fn, { + each: function (callback) { + if (callback && typeof (callback) == 'function') { + for (let i = 0; i < this.length; i++) { + callback(this[i], i); + } + return this; + } + }, + siblings: function () { + var a = [] + this.each(function(el){ + a= [...a,...el.parentNode.children].filter(c=> c != el); + }) + return a; + }, + class_add: function(cn){ + this.each(function(el){ + el.classList.add(cn); + }); + return this; + }, + class_has: function (className) { + return this[0].classList.contains(className); + }, + classes: function(){ + var list = []; + this.each(function(el){ + for(var i = 0; i < el.classList.length; i++){ + var item=el.classList[i]; + if(list.indexOf(item) == -1){ + list.push(item); + } + } + }); + return list; + }, + class_list: function(){ + var list = []; + this.each(function(el){ + for(var i = 0; i < el.classList.length; i++){ + var item=el.classList[i]; + if(list.indexOf(item) == -1){ + list.push(item); + } + } + }); + return list; + }, + class_remove: function (className) { + this.each(function (el) { + el.classList.remove(className); + }) + return this; + }, + css: function (propertyObject) { + this.each(function (el) { + Object.assign(el.style, propertyObject); + }) + return this; + }, + attr: function (attr, value = null) { + let getattr = this; + if (value) { + this.each(function (el) { + el.setAttribute(attr, value); + + }); + } else { + getattr = this[0].getAttribute(attr); + } + return getattr; + }, + html: function (data) { + if (data) { + this.each(function (el) { + el.innerHTML = data; + }) + } else { + return this[0].innerHTML; } return this; - } - }, - siblings: function(){ - return [...this[0].parentNode.children].filter(c=>c!=this[0]) - }, - addClass: function(className) { - this.each(function(el) { - el.classList.add(className); - }) - return this; - }, - removeClass: function(className) { - this.each(function(el) { - el.classList.remove(className); - }) - return this; - }, - hasClass: function(className) { - return this[0].classList.contains(className); - }, - css: function(propertyObject) { - this.each(function(el) { - Object.assign(el.style,propertyObject); - }) - return this; - }, - attr: function(attr, value = null) { - let getattr = this; - if (value){ - this.each(function(el) { - el.setAttribute(attr, value); - + }, + prepend: function (el) { + this.each(function (E) { + E.prepend(el) }); - } else { - getattr = this[0].getAttribute(attr); + return this; + }, + append: function (el) { + this.each(function (E) { + E.append(el) + }); + return this; + }, + hide: function () { + this.each(function (el) { + el.style.display = "none"; + }); + return this; + }, + show: function () { + this.each(function (el) { + el.style.display = ""; + }); + return this; + }, + trigger: function (Event_Name, Event_Data = {}) { + var e = new CustomEvent(Event_Name, Event_Data); + this.each(function(el){ + el.dispatchEvent(e); + }); + return this; + }, + remove: function() { + this.each(function(el){ + el.remove(); + }); + return this; + }, + parent: function() { + var p = []; + this.each(function(el){ + p.push(el.parentNode); + }); + return this; } - return getattr; - }, - html: function(data) { - if (data) { - this.each(function(el) { - el.innerHTML = data; - }) - } else { - return this[0].innerHTML; - } - return this; - }, - prepend: function(el){ - this.each(function(E){ - E.prepend(el) + }); + + var selector = function(string){}; + + if (typeof define === "function" && define.amd) { + define("framework", [], function () { + return Framework; }); - return this; - }, - append: function(el){ - this.each(function(E){ - E.append(el) - }); - return this; - }, - hide: function() { - this.each(function(el) { - el.style.display = "none"; - }); - return this; - }, - show: function() { - this.each(function(el) { - el.style.display = ""; - }); - return this; - }, - on: function(event, child, callback = null) { - if (callback != null) { - let selector = child; - this.each(function(element) { - element.addEventListener(event, function(event) { - if (event.target.matches(selector + ', ' + selector + ' *')) { - callback.apply(event.target.closest(selector), arguments); - } - }) - }) - } else { - //if the callback argument is not present then assume the child argument is being use as callback - callback = child; - this.each(function(element) { - element.addEventListener(event, callback); - }) - } - - return this; - }, - trigger: function(Event_Name,Event_Data={}) { - var e = new CustomEvent(Event_Name, Event_Data); - for(var i=0; i< this.length; i++){ - this[i].dispatchEvent(e); - } } + + var _Framework = window.Framework, + _f = window.f; + + Framework.noConflict = function (deep) { + if (window.f === Framework) { + window.f = _f; + } + + if (deep && window.Framework === Framework) { + window.Framework = _Framework; + } + + return Framework; + }; + + if (typeof noGlobal === "undefined") { + window.Framework = window.F = window.f = Framework; + } + + return Framework; }); - -export {Framework}; \ No newline at end of file +export { + Framework +}; \ No newline at end of file diff --git a/src/framework/helpers.js b/src/framework/helpers.js index 5ea9d4c..01620a2 100644 --- a/src/framework/helpers.js +++ b/src/framework/helpers.js @@ -131,6 +131,40 @@ var is_function = (obj) => { return isset(obj) && (obj instanceof Function); } +var is_window = ( obj ) => { + return obj != null && obj === obj.window; +}; +var is_element = (obj) => { + if(!isset(obj)){ + return false; + } + if(obj instanceof HTMLElement){ + //Using W3 DOM2 (works for FF, Opera and Chrome) + return true; + } + return (typeof obj==="object") && + (obj.nodeType===1) && (typeof obj.style === "object") && + (typeof obj.ownerDocument ==="object"); +} +var isElement = is_element; + +var is_number = (obj) => { + return isset(obj) && (is_float(obj) || is_int(obj) || obj == NaN); +} + +var to_type = function(obj){ + if(is_array(obj)) + return "array"; + if(is_number(obj)) + return "number"; + if(is_object(obj)) + return "object"; + if(is_string(obj)) + return "string"; + if(is_window(obj)) + return "window"; + return false; +} var foreach = (arg, func) => { if(!is_function(func)){ @@ -151,17 +185,5 @@ } } } -var isElement = (obj) => { - if(!isset(obj)){ - return false; - } - if(obj instanceof HTMLElement){ - //Using W3 DOM2 (works for FF, Opera and Chrome) - return true; - } - return (typeof obj==="object") && - (obj.nodeType===1) && (typeof obj.style === "object") && - (typeof obj.ownerDocument ==="object"); -} export { intval, floatval, json_decode, json_encode, explode, implode, str_replace, instance_of, isset, foreach, array_search, strpos, - is_int, is_float, is_array, in_array, is_object, is_function, isElement}; \ No newline at end of file + is_int, is_float, is_array, in_array, is_object, is_function, is_window, is_element, is_number, to_type, isElement}; \ No newline at end of file