-
Notifications
You must be signed in to change notification settings - Fork 0
/
jscss.js
95 lines (89 loc) · 3.44 KB
/
jscss.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
(function($){
var Util = {
List : function(array){
this.array = array;
this.contains = function(value){
for(var i=0;i<this.array.length;i++){
if(array[i] == value) return true;
}
return false;
};
},
EXP : {
ID : function(val){
return '#'+val;
},
CLS : function(val){
return '.'+val;
},
TAG : function(val){
return val;
}
}
};
$.fn.jscss = function(options){
var defaults = {
// nothing
};
var settings = $.extend(defaults, options);
var event_type_of = function(obj){
return ($.isArray(obj) && obj.length == 2)? { is_multi : true } : { is_multi : false };
};
var key_type_of = function(key){
if(key[0] == '#'){
return { is_id : true, is_cls : false, is_tag : false };
}else if(key[0] == '.'){
return { is_id : false, is_cls : true, is_tag : false };
}else{
return { is_id : false, is_cls : false, is_tag : true };
}
};
var execute = function(element, e, target, hash, key){
if(hash[key] && hash[key].event[e.type]){
for(el_name in hash[key].event[e.type]){
if(el_name != 'js' && hash[key].event[e.type][el_name]){
for(css_key in hash[key].event[e.type][el_name]){
if(el_name != 'this'){
$(element).find(el_name).css(css_key, hash[key].event[e.type][el_name][css_key]);
}else{
$(target).css(css_key, hash[key].event[e.type][el_name][css_key]);
}
}
}else if(el_name == 'js' && hash[key].event[e.type].js){
hash[key].event[e.type].js(e);
}
}
}
};
var apply = function(element, hash){
if(!element || !hash) return;
for(key in hash) {
for(ev_name in hash[key].event){
if(key_type_of(key).is_id){
element.find(key)[ev_name](function(e){
if($(this).attr('id')){
execute(element, e, this, hash, Util.EXP.ID($(this).attr('id')));
}
});
}
if(key_type_of(key).is_cls){
element.find(key)[ev_name](function(e){
if($(this).attr('class')){
execute(element, e, this, hash, Util.EXP.CLS($(this).attr('class')));
}
});
}
if(key_type_of(key).is_tag){
element.find(key)[ev_name](function(e){
if($(this)[0].nodeName){
execute(element, e, this, hash, Util.EXP.TAG($(this)[0].nodeName.toLowerCase()));
}
});
}
}
}
};
apply(this, settings);
return this;
}
})(jQuery);