-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy path140medley.js
More file actions
243 lines (224 loc) · 6.41 KB
/
Copy path140medley.js
File metadata and controls
243 lines (224 loc) · 6.41 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
* 140 medley
* (c) 2011 - Honza Pokorny
* Licensed under the terms of the BSD license
*
* This is a micro-framework or a collection of small, helpful utilities for
* common javascript tasks.
*
* Size:
* Source: 8.6 kb
* Minified: 821 bytes
* gzipped: 504 bytes
*
* Features:
* - templating - t();
* - local storage - s();
* - bind/unbind events - b();
* - create DOM elements - m();
* - DOM selector - $();
* - Get cross-browser xhr - j();
*
*/
/*
* Templating
*
* Usage:
* var hello = t("Hello, #{this.name || 'world'}!")
*
* console.log( // => "Hello, Jed!"
* hello({name: "Jed"})
* )
*
* Copyright (C) 2011 Jed Schmidt <http://jed.is> - WTFPL
* More: https://gist.github.com/964762
*/
var t = function(
a, // the string source from which the template is compiled
b // the default `with` context of the template (optional)
){
return function(
c, // the object called as `this` in the template
d // the `with` context of this template call (optional)
){
return a.replace(
/#{([^}]*)}/g, // a regexp that finds the interpolated code: "#{<code>}"
function(
a, // not used, only positional
e // the code matched by the interpolation
){
return Function(
"x",
"with(x)return " + e // the result of the interpolated code
).call(
c, // pass the data object as `this`, with
d // the most
|| b // specific
|| {} // context.
)
}
)
}
};
/*
* LocalStorage
*
* Copyright (C) 2011 Jed Schmidt <http://jed.is> - WTFPL
* More: https://gist.github.com/966030
*
*/
var s = function(
a, // placeholder for storage object
b // placeholder for JSON
){
return b
? { // if JSON is supported
get: function( // provide a getter function
c // that takes a key
){
return a[c] && // and if the key exists
b.parse(a[c]) // parses and returns it,
},
set: function( // and a setter function
c, // that takes a key
d // and a value
){
a[c] = // and sets
b.stringify(d) // its serialization.
}
}
: {} // if JSON isn't supported, provide a shim.
}(
this.localStorage // use native localStorage if available
|| {}, // or an object otherwise
JSON // use native JSON (required)
)
/*
* Bind/Unbind events
*
* Usage:
* var el = document.getElementyById('#container');
* b(el, 'click', function() {
* console.log('clicked');
* });
*
* Copyright (C) 2011 Jed Schmidt <http://jed.is> - WTFPL
* More: https://gist.github.com/968186
*
*/
var p = function(
a, // a DOM element
b, // an event name such as "click"
c, // (placeholder)
d // (placeholder)
){
c = c || document; // use the document by default
d = c[ // save the current onevent handler
b = "on" + b // prepent the event name with "on"
];
a = c[b] = // cache and replace the current handler
function(e) { // with a function that
d = d && d( // executes/caches the previous handler
e = e || c.event // with a cross-browser object,
);
return (a = a && b(e)) // and calls the passed function,
? b // returning the current handler if it rebinds
: d // and the previous handler otherwise.
};
c = this // cache the window to fetch IE events
};
/*
* Create DOM element
*
* Usage:
* var el = m('<h1>Hello</h1>');
* document.body.appendChild(el);
*
*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* Version 2, December 2004
*
* Copyright (C) 2011 Jed Schmidt <http://jed.is> - WTFPL
* More: https://gist.github.com/966233
*
*/
var m = function(
a, // an HTML string
b, // placeholder
c // placeholder
){
b = document; // get the document,
c = b.createElement("p"); // create a container element,
c.innerHTML = a; // write the HTML to it, and
a = b.createDocumentFragment(); // create a fragment.
while ( // while
b = c.firstChild // the container element has a first child
) a.appendChild(b); // append the child to the fragment,
return a // and then return the fragment.
}
/*
* DOM selector
*
* Usage:
* $('div');
* $('#name');
* $('.name');
*
*
* Copyright (C) 2011 Jed Schmidt <http://jed.is> - WTFPL
* More: https://gist.github.com/991057
*
*/
var $ = function(
a, // take a simple selector like "name", "#name", or ".name", and
b // an optional context, and
){
a = a.match(/^(\W)?(.*)/); // split the selector into name and symbol.
return( // return an element or list, from within the scope of
b // the passed context
|| document // or document,
)[
"getElement" + ( // obtained by the appropriate method calculated by
a[1]
? a[1] == "#"
? "ById" // the node by ID,
: "sByClassName" // the nodes by class name, or
: "sByTagName" // the nodes by tag name,
)
](
a[2] // called with the name.
)
}
/*
* Get cross browser xhr object
*
*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* Version 2, December 2004
*
* Copyright (C) 2011 Jed Schmidt <http://jed.is>
* More: https://gist.github.com/993585
*
*/
var j = function(
a // cursor placeholder
){
for( // for all a
a=0; // from 0
a<4; // to 4,
a++ // incrementing
) try { // try
return a // returning
? new ActiveXObject( // a new ActiveXObject
[ // reflecting
, // (elided)
"Msxml2", // the various
"Msxml3", // working
"Microsoft" // options
][a] + // for Microsoft implementations, and
".XMLHTTP" // the appropriate suffix,
) // but make sure to
: new XMLHttpRequest // try the w3c standard first, and
}
catch(e){} // ignore when it fails.
}