|
| 1 | +/** |
| 2 | + * @author Lorand Veres user.email "lorand.mast@gmail.com" |
| 3 | + * |
| 4 | + * |
| 5 | + */ |
| 6 | + |
| 7 | + /* |
| 8 | + * Basic comparison |
| 9 | + * and other useful functions |
| 10 | + * |
| 11 | + */ |
| 12 | + |
| 13 | + (function(){ |
| 14 | + |
| 15 | + isObj = function(o) { |
| 16 | + return typeof obj === 'object' || toString.call(o).split(/\W/)[2].toLowerCase() === 'object'; |
| 17 | + }; |
| 18 | + |
| 19 | + isArray = function(a) { |
| 20 | + return Array.isArray(a) || toString.call(a).split(/\W/)[2].toLowerCase() === 'array'; |
| 21 | + }; |
| 22 | + |
| 23 | + isFunc = function (f) { |
| 24 | + return typeof f === 'function'; |
| 25 | + }; |
| 26 | + |
| 27 | + varyArgs = function(arguments){ |
| 28 | + return Array.prototype.slice.call(arguments); |
| 29 | + }; |
| 30 | + |
| 31 | + argsLength = function(arguments){ |
| 32 | + return varyArgs(arguments).length; |
| 33 | + }; |
| 34 | + |
| 35 | + setCss = function(el, css){ |
| 36 | + var k, f =''; |
| 37 | + if(isObj(css)){ |
| 38 | + for(k in css){ |
| 39 | + if(css.hasOwnProperty(k)) |
| 40 | + f += k + ':' + css[k] + ';'; |
| 41 | + } |
| 42 | + el.style.cssText = f; |
| 43 | + } |
| 44 | + |
| 45 | + }; |
| 46 | + |
| 47 | + toggle = function(el){ |
| 48 | + if(el.style.display === 'none'){ |
| 49 | + setCss(el, {display:'block'}); |
| 50 | + }else{ |
| 51 | + setCss(el, {display:'none'}); |
| 52 | + } |
| 53 | + }; |
| 54 | + |
| 55 | + // Useful Object relating functions |
| 56 | + |
| 57 | + MyObj = { |
| 58 | + |
| 59 | + // Chek if the key exist in the named object |
| 60 | + // Return value true or false |
| 61 | + |
| 62 | + keyIn:function(o, k) { |
| 63 | + return k.toString(k) in o ? true : false; |
| 64 | + }, |
| 65 | + |
| 66 | + |
| 67 | + // Return the size of an object |
| 68 | + |
| 69 | + size:function(o) { // o = object |
| 70 | + var count = 0, |
| 71 | + key; |
| 72 | + if ( count = Object.keys(o).length) { |
| 73 | + return count; |
| 74 | + } else { |
| 75 | + for (key in o) { |
| 76 | + o.hasOwnProperty(key) ? count++ : null; |
| 77 | + } |
| 78 | + return count; |
| 79 | + } |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + }());// end of basic functions |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + // the MAIN function |
| 88 | + |
| 89 | + |
| 90 | + $ = ( function() { |
| 91 | + var args = [], |
| 92 | + document = window.document, |
| 93 | + |
| 94 | + idRE = /^#{1}[a-z0-9]+\-*$/i, // id REgex |
| 95 | + classNameRE = /^\.{1}[a-z0-9\-\_\s]+$/i, // class REgex |
| 96 | + tagNameRE = /^<{1}[a-z]+>{1}$/i, // html tag REgex |
| 97 | + |
| 98 | + toType = {}, |
| 99 | + toString = toType.toString, |
| 100 | + extend, |
| 101 | + type; |
| 102 | + |
| 103 | + // Helping functions used inside the MAIN return anonymous function |
| 104 | + |
| 105 | + // Helping function |
| 106 | + // Selecting the element from first parameter |
| 107 | + |
| 108 | + function getEl(arg, item){ |
| 109 | + var el; |
| 110 | + if ( typeof arg == 'string'){ |
| 111 | + if(idRE.test(arg)) el = document.getElementById(arg.substring(1)); |
| 112 | + if(classNameRE.test(arg)) el = document.getElementsByClassName(arg.substring(1))[item]; |
| 113 | + if(tagNameRE.test(arg)) el = document.getElementsByTagName(arg.replace(/^<+|>+$/gm,''))[item]; |
| 114 | + } |
| 115 | + return el; |
| 116 | + } |
| 117 | + |
| 118 | + /* |
| 119 | + * Helping function |
| 120 | + * Selecting the elementTagName item number from parameters |
| 121 | + * |
| 122 | + */ |
| 123 | + |
| 124 | + function getElItemNo(arg){ |
| 125 | + var b; |
| 126 | + typeof arg === 'number' ? b = arg : b = 0; |
| 127 | + return b; |
| 128 | + } |
| 129 | + |
| 130 | + /* |
| 131 | + * Helping function |
| 132 | + * Selecting the text and css from possible |
| 133 | + * second or third parameter handled over |
| 134 | + * in a object |
| 135 | + * |
| 136 | + */ |
| 137 | + |
| 138 | + function getCssText(arg){ |
| 139 | + var csstext; |
| 140 | + if ( arg.length === 2 && isObj(arg[1])) |
| 141 | + csstext = arg[1]; |
| 142 | + if ( arg.length === 3 && isObj(arg[2])) |
| 143 | + csstext = arg[2]; |
| 144 | + return csstext; |
| 145 | + } |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | + return function() { |
| 150 | + args = varyArgs(arguments); |
| 151 | + var el, o, itemNo, key, cssStyle, fcss =''; |
| 152 | + |
| 153 | + if (args.length > 0) { |
| 154 | + itemNo = getElItemNo(args[1]); |
| 155 | + el = getEl(args[0], itemNo); |
| 156 | + o = getCssText(args); |
| 157 | + if(isObj(o)){ |
| 158 | + if(o.text !== undefined || o.hasOwnProprty('text')) el.innerHTML = o.text; |
| 159 | + if(o.style !== undefined || o.hasOwnProprty('style')){ |
| 160 | + setCss(el, o.style); |
| 161 | + } |
| 162 | + } |
| 163 | + return el; |
| 164 | + } |
| 165 | + }; |
| 166 | + |
| 167 | + }()); |
| 168 | + |
| 169 | +var text = 'First text tested introduced inside a html element. Also the css style changed. All this using dardjs.'; |
| 170 | +var css = {width:'95%', height:'45px', margin:'2% 0 2% 0', 'background-color':'#aaa', padding:'1em'}; |
| 171 | + |
| 172 | +$("#small", {text: text , style :css }); |
| 173 | + |
| 174 | + |
| 175 | + |
| 176 | + |
| 177 | + |
| 178 | + |
0 commit comments