1
1
/**
2
2
* @author Lorand Veres user.email "lorand.mast@gmail.com"
3
3
*
4
- *
5
- * Select elements by ID , CLASSNAME, TAGNAME
6
- *
7
- * PARAMETER 1 selector should be in the following formats: #ID , .CLASSNAME , TAGNAME
8
- * PARAMETER 2 Optinal, item number should be digit
9
- * PARAMETER 3 Optional, text, attributs and css should be of the format of
10
- *
11
- * Syntax
12
- * $('.className', 0, {text:'text', id:'yourId', style:{bgcolor:blue, font-size:14px}})
13
- *
14
4
*
15
5
*/
16
6
17
- var start , end , $ ;
18
- start = Date ( ) . now || new Date ( ) . getTime ( ) ;
19
-
20
- $ = ( function ( ) {
21
- var selector ,
22
- args = [ ] ,
23
- document = window . document ,
24
-
25
- idRE = / ^ # { 1 } [ a - z 0 - 9 ] + \- * $ / i, // id REgex
26
- classNameRE = / ^ \. { 1 } [ a - z 0 - 9 \- \_ \s ] + $ / i, // class REgex
27
- tagNameRE = / ^ < { 1 } [ a - z ] + > { 1 } $ / i, // html tag REgex
28
-
29
- $ = { } ,
30
- fn ,
31
- Obj ,
32
- toType = { } ,
33
- toString = toType . toString ,
34
- extend ,
35
- type ;
36
-
7
+
37
8
/*
38
9
* Basic comparison
39
- * and other small useful functions
10
+ * and other useful functions
40
11
*
41
12
*/
42
-
43
- function isObj ( o ) {
13
+
14
+ ( function ( ) {
15
+
16
+ isObj = function ( o ) {
44
17
return typeof obj === 'object' || toString . call ( o ) . split ( / \W / ) [ 2 ] . toLowerCase ( ) === 'object' ;
45
- }
18
+ } ;
46
19
47
- function isArray ( a ) {
20
+ isArray = function ( a ) {
48
21
return Array . isArray ( a ) || toString . call ( a ) . split ( / \W / ) [ 2 ] . toLowerCase ( ) === 'array' ;
49
- }
22
+ } ;
50
23
51
- function isFunc ( f ) {
24
+ isFunc = function ( f ) {
52
25
return typeof f === 'function' ;
53
- }
26
+ } ;
54
27
55
- function varyArgs ( arguments ) {
28
+ varyArgs = function ( arguments ) {
56
29
return Array . prototype . slice . call ( arguments ) ;
57
- }
30
+ } ;
58
31
59
- function argsLength ( arguments ) {
32
+ argsLength = function ( arguments ) {
60
33
return varyArgs ( arguments ) . length ;
61
- }
62
-
63
- /*
64
- * Useful Object relating functions
65
- *
66
- */
67
-
68
- Obj = function ( ) {
34
+ } ;
35
+
36
+ setCss = function ( el , css ) {
37
+ var k , f = '' ;
38
+ if ( isObj ( css ) ) {
39
+ for ( k in css ) {
40
+ if ( css . hasOwnProperty ( k ) )
41
+ f += k + ':' + css [ k ] + ';' ;
42
+ }
43
+ el . style . cssText = f ;
44
+ }
69
45
70
- /*
71
- * Chek if the key exist in the maned object
72
- * Return true or false
73
- * Usage Obj.keyIn(objectName , "keyToLookFor")
74
- */
46
+ } ;
47
+
48
+ toggle = function ( el ) {
49
+ if ( el . style . display === 'none' ) {
50
+ setCss ( el , { display :'block' } ) ;
51
+ } else {
52
+ setCss ( el , { display :'none' } ) ;
53
+ }
54
+ } ;
55
+
56
+ // Useful Object relating functions
57
+
58
+ MyObj = {
75
59
76
- this . keyIn = function ( o , k ) {
60
+ // Chek if the key exist in the named object
61
+ // Return value true or false
62
+
63
+ keyIn :function ( o , k ) {
77
64
return k . toString ( k ) in o ? true : false ;
78
- } ;
65
+ } ,
79
66
80
- /*
81
- * Return the size of an object
82
- * Usage Obj.size(object);
83
- */
84
- this . size = function ( o ) { // o = object
67
+
68
+ // Return the size of an object
69
+
70
+ size :function ( o ) { // o = object
85
71
var count = 0 ,
86
72
key ;
87
73
if ( count = Object . keys ( o ) . length ) {
@@ -92,76 +78,39 @@ start = Date().now || new Date().getTime();
92
78
}
93
79
return count ;
94
80
}
95
- } ;
96
-
97
- this . keyInline = function ( o , callback ) {
98
- var p ,
99
- kn = [ ] ;
100
- for ( p in o ) p in o ? typeof callback !== undefined && ! isObj ( o ) ? callback ( o [ p ] ) : kn . push ( p . toString ( p ) ) : kn = false ;
101
- return kn ;
102
- } ;
103
-
104
- this . keyDeep = function ( o ) {
105
- var p ,
106
- kn = [ ] ;
107
- for ( p in o ) {
108
- if ( ! isObj ( o [ p ] ) ) {
109
- kn . push ( p . toString ( p ) ) ;
110
- } else if ( isObj ( o [ p ] ) ) {
111
- kn . push ( this . keyDeep ( o [ p ] ) ) ;
112
- }
113
- }
114
- return kn ;
115
- } ;
116
-
117
- this . keyValue = function ( o , k ) {
118
- return k . toString ( k ) in o ? o [ k . toString ( k ) ] : false ;
119
- } ;
120
- } ; // end objKeyName
121
- this . Obj = new Obj ( ) ;
122
-
123
-
124
- function Ajax ( ) {
125
- return ;
126
- }
127
-
128
- /*
129
- * Helping functions used inside the MAIN return anonymous function
130
- *
131
- */
132
-
133
- extend = function ( obj ) {
134
- if ( typeof Object . assign === 'function' ) {
135
- return Object . assign ( { } , obj ) ;
136
81
}
137
82
} ;
83
+
84
+ } ( ) ) ; // end of basic functions
85
+
86
+
87
+
88
+ // the MAIN function
89
+
90
+
91
+ var $ = ( function ( ) {
92
+ var args = [ ] ,
93
+ document = window . document ,
138
94
95
+ idRE = / ^ # { 1 } [ a - z 0 - 9 ] + \- * $ / i, // id REgex
96
+ classNameRE = / ^ \. { 1 } [ a - z 0 - 9 \- \_ \s ] + $ / i, // class REgex
97
+ tagNameRE = / ^ < { 1 } [ a - z ] + > { 1 } $ / i, // html tag REgex
98
+
99
+ toType = { } ,
100
+ toString = toType . toString ,
101
+ extend ,
102
+ type ;
139
103
140
- /*
141
- * Helping function
142
- * A primitive yet practical metod
143
- * Assigning functions to the window object
144
- */
145
- fn = function ( ) {
146
- this . isObj = isObj ;
147
- this . isArray = isArray ;
148
- this . isFunc = isFunc ;
149
- this . varyArgs = varyArgs ;
150
- this . argsLength = argsLength ;
151
- } ;
152
-
153
-
154
- /*
155
- * Helping function
156
- * Selecting the element from first parameter
157
- *
158
- */
104
+ // Helping functions used inside the MAIN return anonymous function
159
105
106
+ // Helping function
107
+ // Selecting the element from first parameter
108
+
160
109
function getEl ( arg , item ) {
161
110
var el ;
162
111
if ( typeof arg == 'string' ) {
163
112
if ( idRE . test ( arg ) ) el = document . getElementById ( arg . substring ( 1 ) ) ;
164
- if ( classNameRE . test ( arg ) ) el = document . getElementsByClassName ( arg . substring ( 1 ) ) ;
113
+ if ( classNameRE . test ( arg ) ) el = document . getElementsByClassName ( arg . substring ( 1 ) ) [ item ] ;
165
114
if ( tagNameRE . test ( arg ) ) el = document . getElementsByTagName ( arg . replace ( / ^ < + | > + $ / gm, '' ) ) [ item ] ;
166
115
}
167
116
return el ;
@@ -174,8 +123,9 @@ start = Date().now || new Date().getTime();
174
123
*/
175
124
176
125
function getElItemNo ( arg ) {
177
- if ( typeof arg == 'number' )
178
- return arg ;
126
+ var b ;
127
+ typeof arg === 'number' ? b = arg : b = 0 ;
128
+ return b ;
179
129
}
180
130
181
131
/*
@@ -199,56 +149,22 @@ start = Date().now || new Date().getTime();
199
149
200
150
return function ( ) {
201
151
args = varyArgs ( arguments ) ;
202
- fn ( ) ;
203
- var el , o ;
152
+ var el , o , itemNo , key , cssStyle , fcss = '' ;
204
153
205
154
if ( args . length > 0 ) {
206
155
itemNo = getElItemNo ( args [ 1 ] ) ;
207
156
el = getEl ( args [ 0 ] , itemNo ) ;
208
157
o = getCssText ( args ) ;
209
158
if ( isObj ( o ) ) {
210
- if ( o . text !== undefined ) el . innerHTML = o . text ;
211
- if ( o . style ) {
212
- //console
159
+ if ( o . text !== undefined || o . hasOwnProprty ( 'text' ) ) el . innerHTML = o . text ;
160
+ if ( o . style !== undefined || o . hasOwnProprty ( 'style' ) ) {
161
+ setCss ( el , o . style ) ;
213
162
}
214
163
}
215
164
return el ;
216
165
}
217
166
} ;
218
167
219
168
} ( ) ) ;
220
-
221
-
222
- /*
223
- ( function() {
224
- if (Dard !== undefined && Dard == window.Dard ) {
225
- if (typeof $ !== undefined) {
226
- var $ = Dard;
227
- }
228
- }
229
- }());
230
- */
231
-
232
-
233
- var a = '#one' , d , e ,
234
- ade = {
235
- text : 'text text aditional to add' ,
236
- id : 'yourId' ,
237
- style : {
238
- bgcolor : 'blue' ,
239
- fontsize : '14px'
240
- }
241
- } ;
242
- $ ( '#one' , { text : 'first text tested' } ) ;
243
- //if(d = $('<div>', 2, {text: 'first text tested'})) d.innerHTML = "<p>Dard.js is a small and good function </p>";
244
- //c[ c.length -1 ].innerHTML = "that's a div 3 text.";
245
-
246
-
247
- //if(e = $('#small'))e.innerHTML = "that's just a note around footer area.";
248
-
249
-
250
-
251
- end = Date . now ( ) || new Date ( ) . getTime ( ) ;
252
- console . log ( ( end - start ) + ' miliseconds = time to load the js inside' ) ;
253
169
254
170
0 commit comments