-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
141 lines (127 loc) · 3.4 KB
/
functions.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
var New = function ( arg ) {
var doc = arg.doc || document;
var type = arg.type || 'div';
var className = arg.className || arg.class || arg.c;
var id = arg.id;
var text = arg.text || arg.t;
var childs = arg.childs || arg.ch || [];
var parent = arg.parent || arg.p;
var value = arg.value || arg.val || arg.v;
var name = arg.name || arg.n;
var behavior = arg.behavior || arg.behave || arg.b;
var style = arg.style || arg.s;
var props = arg.prop;
var e = doc.createElement( type );
if ( className ) { e.className = className }
if ( id ) { e.id = id }
if ( text ) { e.innerHTML = text }
if ( name ) { e.name = name }
if ( style ) { console.info( style ); e.setAttribute( 'style', style ) }
if ( props ) {
for ( var prop in props ) {
e[prop] = props[prop];
}
}
if ( behavior ) {
setBehavior( e, behavior );
}
if ( childs ) {
for ( var i = 0; i < childs.length; i++ ) {
try {
e.appendChild( childs[ i ] )
} catch ( err ) {
console.warn( err );
}
}
}
if ( parent ) { parent.appendChild( e ) }
if ( value ) { e.value = value }
return e;
};
var byId = function (arg, el ) {
if (el && arg) {
return el.getElementById( arg )
} else {
return document.getElementById( arg )
}
};
var byClass = function (arg, el ) {
if (el && arg) {
return el.getElementsByClassName( arg )
} else {
return document.getElementsByClassName( arg )
}
};
var addEvent = function ( ) { arguments[0].addEventListener( arguments[1], arguments[2] ); return arguments[0] };
var constructClass = function ( name, definition, properties ) {
name = definition;
for ( var d in properties ) {
name[d] = properties[d];
}
name.prototype = name;
return name;
};
var setBehavior = function ( element, behavior ) {
for ( var b_type in behavior ) {
var b_type_s = behavior[b_type];
if ( behavior_types[b_type_s] ) {
for ( var b in behavior_types[ b_type_s ]['evt'] ) {
addEvent( element, b, Processor[ behavior_types[ b_type_s ]['evt'][b] ] );
}
for ( var s in behavior_types[ b_type_s ]['state'] ) {
element[s] = behavior_types[ b_type_s ]['state'][s];
}
} else {
console.warn( 'No such (' + ( b_type_s || 'empty' ) + ') behaviour for ' + ( id || name || text || className || 0 ) + '!' );
}
}
};
var isDOMNode = function ( arg ) {
return (
typeof Node === "object" ?
arg instanceof Node :
arg && typeof arg === "object" && typeof arg.nodeType === "number" && typeof arg.nodeName==="string"
);
};
var isDOMElement = function ( arg ) {
return (
typeof HTMLElement === "object" ?
arg instanceof HTMLElement :
arg && typeof arg === "object" && arg !== null && arg.nodeType === 1 && typeof arg.nodeName==="string"
);
};
var isDOM = function ( arg ) {
return isDOMNode( arg ) || isDOMElement( arg );
}
/*
Object.prototype is slightly extended, following keys used:
'props'
'key'
'extend'
*/
Object.defineProperty( Object.prototype, 'props', {
enumerable: false,
configurable: false,
writeable: false,
value: function ( args ) { Object.defineProperties( this, args ) },
});
Object.prototype.props({
'keys': {
enumerable: false,
configurable: false,
writeable: false,
value: function () { return Object.keys( this ) },
},
'extend': {
enumerable: false,
configurable: false,
writeable: false,
value: function ( arg ) {
if ( typeof arg == 'object' ) {
for ( item in arg ) {
if ( arg.hasOwnProperty( item ) ) { this[ item ] = arg[ item ]; }
}
}
}
}
});