forked from thednp/kute.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkute-css.js
154 lines (144 loc) · 6.13 KB
/
kute-css.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* KUTE.js - The Light Tweening Engine
* package CSS Plugin
* by dnp_theme
* Licensed under MIT-License
*/
(function(root,factory){
if (typeof define === 'function' && define.amd) {
define(["./kute.js"], function(KUTE){ factory(KUTE); return KUTE; });
} else if(typeof module == "object" && typeof require == "function") {
var KUTE = require("./kute.js");
// Export the modified one. Not really required, but convenient.
module.exports = factory(KUTE);
} else if (typeof root.KUTE !== "undefined") {
factory(KUTE);
} else {
throw new Error("CSS Plugin require KUTE.js.")
}
})(this, function(KUTE){
'use strict';
var g = window, K = g.KUTE, DOM = g.dom, parseProperty = K.pp, prepareStart = K.prS, getComputedStyle = K.gCS, trueDimension = K.truD,
_br = K.property('borderRadius'), _brtl = K.property('borderTopLeftRadius'), _brtr = K.property('borderTopRightRadius'), // all radius props prefixed
_brbl = K.property('borderBottomLeftRadius'), _brbr = K.property('borderBottomRightRadius'),
_cls = ['borderColor', 'borderTopColor', 'borderRightColor', 'borderBottomColor', 'borderLeftColor', 'outlineColor'], // colors 'hex', 'rgb', 'rgba' -- #fff / rgb(0,0,0) / rgba(0,0,0,0)
_rd = ['borderRadius', 'borderTopLeftRadius', 'borderTopRightRadius', 'borderBottomLeftRadius', 'borderBottomRightRadius'], // border radius px/any
_bm = ['right', 'bottom', 'minWidth', 'minHeight', 'maxWidth', 'maxHeight',
'padding', 'margin', 'paddingTop','paddingBottom', 'paddingLeft', 'paddingRight', 'marginTop','marginBottom', 'marginLeft', 'marginRight',
'borderWidth', 'borderTopWidth', 'borderRightWidth', 'borderBottomWidth', 'borderLeftWidth', 'outlineWidth'], // dimensions / box model
_tp = ['fontSize','lineHeight','letterSpacing','wordSpacing'], // text properties
_clp = ['clip'], _bg = ['backgroundPosition'], // clip | background position
_mg = _rd.concat(_bm,_tp), // a merge of all properties with px|%|em|rem|etc unit
_all = _cls.concat(_clp, _rd, _bm, _tp, _bg), al = _all.length,
number = g.Interpolate.number, unit = g.Interpolate.unit,
_d = _d || {}; //all properties default values
//populate default values object
for ( var i=0; i< al; i++ ){
var p = _all[i];
if (_cls.indexOf(p) !== -1){
_d[p] = 'rgba(0,0,0,0)'; // _d[p] = {r:0,g:0,b:0,a:1};
} else if ( _mg.indexOf(p) !== -1 ) {
_d[p] = 0;
} else if ( _bg.indexOf(p) !== -1 ){
_d[p] = [50,50];
} else if ( p === 'clip' ){
_d[p] = [0,0,0,0];
}
}
// create prepare/process/render functions for additional colors properties
for (var i = 0, l = _cls.length; i<l; i++) {
p = _cls[i];
parseProperty[p] = function(p,v) {
return parseProperty.cls(p,v);
};
prepareStart[p] = function(el,p,v){
return getComputedStyle(el,p) || _d[p];
};
}
// create prepare/process/render functions for additional box model properties
for (var i = 0, l = _mg.length; i<l; i++) {
p = _mg[i];
parseProperty[p] = function(p,v){
return parseProperty.box(p,v);
};
prepareStart[p] = function(el,p,v){
return getComputedStyle(el,p) || _d[p];
};
}
//create prepare/process/render functions for radius properties
for (var i = 0, l = _rd.length; i<l; i++) {
p = _rd[i];
parseProperty[p] = function(p,v){
if ( (!(p in DOM)) ) {
if (p === 'borderRadius') {
DOM[p] = function(l,p,a,b,v){
l.style[_br] = unit(a.v,b.v,b.u,v);
}
} else if (p === 'borderTopLeftRadius') {
DOM[p] = function(l,p,a,b,v){
l.style[_brtl] = unit(a.v,b.v,b.u,v);
}
} else if (p === 'borderTopRightRadius') {
DOM[p] = function(l,p,a,b,v){
l.style[_brtr] = unit(a.v,b.v,b.u,v);
}
} else if (p === 'borderBottomLeftRadius') {
DOM[p] = function(l,p,a,b,v){
l.style[_brbl] = unit(a.v,b.v,b.u,v);
}
} else if (p === 'borderBottomRightRadius') {
DOM[p] = function(l,p,a,b,v){
l.style[_brbr] = unit(a.v,b.v,b.u,v);
}
}
}
return trueDimension(v);
};
prepareStart[p] = function(el,p,v){
return getComputedStyle(el,p) || _d[p];
};
}
// clip
parseProperty['clip'] = function(p,v){
if ( !(p in DOM) ) {
DOM[p] = function(l,p,a,b,v) {
var h = 0, cl = [];
for (h;h<4;h++){
var c1 = a[h].v, c2 = b[h].v, cu = b[h].u || 'px';
cl[h] = unit(c1,c2,cu,v);
}
l.style[p] = 'rect('+cl+')';
};
}
if ( v instanceof Array ){
return [ trueDimension(v[0]), trueDimension(v[1]), trueDimension(v[2]), trueDimension(v[3]) ];
} else {
var ci = v.replace(/rect|\(|\)/g,'');
ci = /\,/g.test(ci) ? ci.split(/\,/g) : ci.split(/\s/g);
return [ trueDimension(ci[0]), trueDimension(ci[1]), trueDimension(ci[2]), trueDimension(ci[3]) ];
}
};
prepareStart['clip'] = function(el,p,v){
var c = getComputedStyle(el,p), w = getComputedStyle(el,'width'), h = getComputedStyle(el,'height');
return !/rect/.test(c) ? [0, w, h, 0] : c;
};
// background position
parseProperty['backgroundPosition'] = function(p,v) {
if ( !(p in DOM) ) {
DOM[p] = function(l,p,a,b,v) {
l.style[p] = unit(a.x.v,b.x.v,'%',v) + ' ' + unit(a.y.v,b.y.v,'%',v);
};
}
if ( v instanceof Array ){
return { x: trueDimension(v[0])||{ v: 50, u: '%' }, y: trueDimension(v[1])||{ v: 50, u: '%' } };
} else {
var posxy = v.replace(/top|left/g,0).replace(/right|bottom/g,100).replace(/center|middle/g,50), xp, yp;
posxy = /\,/g.test(posxy) ? posxy.split(/\,/g) : posxy.split(/\s/g); posxy = posxy.length === 2 ? posxy : [posxy[0],50];
xp = trueDimension(posxy[0]); yp = trueDimension(posxy[1]);
return { x: xp, y: yp };
}
}
prepareStart['backgroundPosition'] = function(el,p,v){
return getComputedStyle(el,p) || _d[p];
}
return this;
});