uki 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,612 @@
1
+ /**
2
+ * based on http://github.com/uki/uki/blob/1.4.2/src/ajax.js
3
+ *
4
+ * Copyright 2010, John Resig
5
+ * Dual licensed under the MIT or GPL Version 2 licenses.
6
+ * http://uki.org/license
7
+ */
8
+ (function() {
9
+
10
+ var jsc = +new Date,
11
+ jsre = /=\?(&|$)/,
12
+ rquery = /\?/,
13
+ rts = /(\?|&)_=.*?(&|$)/,
14
+ rurl = /^(\w+:)?\/\/([^\/?#]+)/,
15
+ r20 = /%20/g,
16
+ rnotwhite = /\S/,
17
+ noop = function() {};
18
+
19
+
20
+ uki.extend(uki, {
21
+ error: function( msg ) {
22
+ throw msg;
23
+ },
24
+
25
+ parseJSON: function( data ) {
26
+ if ( typeof data !== "string" || !data ) {
27
+ return null;
28
+ }
29
+
30
+ // Make sure leading/trailing whitespace is removed (IE can't handle it)
31
+ data = uki.trim( data );
32
+
33
+ // Make sure the incoming data is actual JSON
34
+ // Logic borrowed from http://json.org/json2.js
35
+ if ( /^[\],:{}\s]*$/.test(data.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
36
+ .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]")
37
+ .replace(/(?:^|:|,)(?:\s*\[)+/g, "")) ) {
38
+
39
+ // Try to use the native JSON parser first
40
+ return window.JSON && window.JSON.parse ?
41
+ window.JSON.parse( data ) :
42
+ (new Function("return " + data))();
43
+
44
+ } else {
45
+ uki.error( "Invalid JSON: " + data );
46
+ }
47
+ },
48
+
49
+ // Evalulates a script in a global context
50
+ globalEval: function( data ) {
51
+ if ( data && rnotwhite.test(data) ) {
52
+ // Inspired by code by Andrea Giammarchi
53
+ // http://webreflection.blogspot.com/2007/08/global-scope-evaluation-and-dom.html
54
+ var head = document.getElementsByTagName("head")[0] || document.documentElement,
55
+ script = document.createElement("script");
56
+
57
+ script.type = "text/javascript";
58
+
59
+ if ( uki.scriptEval ) {
60
+ script.appendChild( document.createTextNode( data ) );
61
+ } else {
62
+ script.text = data;
63
+ }
64
+
65
+ // Use insertBefore instead of appendChild to circumvent an IE6 bug.
66
+ // This arises when a base node is used (#2709).
67
+ head.insertBefore( script, head.firstChild );
68
+ head.removeChild( script );
69
+ }
70
+ },
71
+ scriptEval: false
72
+ });
73
+
74
+ var script = document.createElement("script"),
75
+ id = "script" + jsc;
76
+ script.type = "text/javascript";
77
+ try {
78
+ script.appendChild( document.createTextNode( "window." + id + "=1;" ) );
79
+ } catch(e) {}
80
+
81
+ document.documentElement.insertBefore( script, document.documentElement.firstChild );
82
+
83
+ // Make sure that the execution of code works by injecting a script
84
+ // tag with appendChild/createTextNode
85
+ // (IE doesn't support this, fails, and uses .text instead)
86
+ if ( window[ id ] ) {
87
+ uki.scriptEval = true;
88
+ delete window[ id ];
89
+ }
90
+
91
+
92
+ uki.extend(uki, {
93
+
94
+ get: function( url, data, callback, type ) {
95
+ // shift arguments if data argument was omited
96
+ if ( uki.isFunction( data ) ) {
97
+ type = type || callback;
98
+ callback = data;
99
+ data = null;
100
+ }
101
+
102
+ return uki.ajax({
103
+ type: "GET",
104
+ url: url,
105
+ data: data,
106
+ success: callback,
107
+ dataType: type
108
+ });
109
+ },
110
+
111
+ getScript: function( url, callback ) {
112
+ return uki.get(url, null, callback, "script");
113
+ },
114
+
115
+ getJSON: function( url, data, callback ) {
116
+ return uki.get(url, data, callback, "json");
117
+ },
118
+
119
+ post: function( url, data, callback, type ) {
120
+ // shift arguments if data argument was omited
121
+ if ( uki.isFunction( data ) ) {
122
+ type = type || callback;
123
+ callback = data;
124
+ data = {};
125
+ }
126
+
127
+ return uki.ajax({
128
+ type: "POST",
129
+ url: url,
130
+ data: data,
131
+ success: callback,
132
+ dataType: type
133
+ });
134
+ },
135
+
136
+ ajaxSetup: function( settings ) {
137
+ uki.extend( uki.ajaxSettings, settings );
138
+ },
139
+
140
+ ajaxSettings: {
141
+ url: location.href,
142
+ global: true,
143
+ type: "GET",
144
+ contentType: "application/x-www-form-urlencoded",
145
+ processData: true,
146
+ async: true,
147
+ /*
148
+ timeout: 0,
149
+ data: null,
150
+ username: null,
151
+ password: null,
152
+ traditional: false,
153
+ */
154
+ // Create the request object; Microsoft failed to properly
155
+ // implement the XMLHttpRequest in IE7 (can't request local files),
156
+ // so we use the ActiveXObject when it is available
157
+ // This function can be overriden by calling uki.ajaxSetup
158
+ xhr: window.XMLHttpRequest && (window.location.protocol !== "file:" || !window.ActiveXObject) ?
159
+ function() {
160
+ return new window.XMLHttpRequest();
161
+ } :
162
+ function() {
163
+ try {
164
+ return new window.ActiveXObject("Microsoft.XMLHTTP");
165
+ } catch(e) {}
166
+ },
167
+ accepts: {
168
+ xml: "application/xml, text/xml",
169
+ html: "text/html",
170
+ script: "text/javascript, application/javascript",
171
+ json: "application/json, text/javascript",
172
+ text: "text/plain",
173
+ _default: "*/*"
174
+ }
175
+ },
176
+
177
+ // Last-Modified header cache for next request
178
+ lastModified: {},
179
+ etag: {},
180
+
181
+ ajax: function( origSettings ) {
182
+ var s = uki.extend({}, uki.ajaxSettings, origSettings);
183
+
184
+ var jsonp, status, data,
185
+ callbackContext = origSettings && origSettings.context || s,
186
+ type = s.type.toUpperCase();
187
+
188
+ // convert data if not already a string
189
+ if ( s.data && s.processData && typeof s.data !== "string" ) {
190
+ s.data = uki.param( s.data, s.traditional );
191
+ }
192
+
193
+ // Handle JSONP Parameter Callbacks
194
+ if ( s.dataType === "jsonp" ) {
195
+ if ( type === "GET" ) {
196
+ if ( !jsre.test( s.url ) ) {
197
+ s.url += (rquery.test( s.url ) ? "&" : "?") + (s.jsonp || "callback") + "=?";
198
+ }
199
+ } else if ( !s.data || !jsre.test(s.data) ) {
200
+ s.data = (s.data ? s.data + "&" : "") + (s.jsonp || "callback") + "=?";
201
+ }
202
+ s.dataType = "json";
203
+ }
204
+
205
+ // Build temporary JSONP function
206
+ if ( s.dataType === "json" && (s.data && jsre.test(s.data) || jsre.test(s.url)) ) {
207
+ jsonp = s.jsonpCallback || ("jsonp" + jsc++);
208
+
209
+ // Replace the =? sequence both in the query string and the data
210
+ if ( s.data ) {
211
+ s.data = (s.data + "").replace(jsre, "=" + jsonp + "$1");
212
+ }
213
+
214
+ s.url = s.url.replace(jsre, "=" + jsonp + "$1");
215
+
216
+ // We need to make sure
217
+ // that a JSONP style response is executed properly
218
+ s.dataType = "script";
219
+
220
+ // Handle JSONP-style loading
221
+ window[ jsonp ] = window[ jsonp ] || function( tmp ) {
222
+ data = tmp;
223
+ success();
224
+ complete();
225
+ // Garbage collect
226
+ window[ jsonp ] = undefined;
227
+
228
+ try {
229
+ delete window[ jsonp ];
230
+ } catch(e) {}
231
+
232
+ if ( head ) {
233
+ head.removeChild( script );
234
+ }
235
+ };
236
+ }
237
+
238
+ if ( s.dataType === "script" && s.cache === null ) {
239
+ s.cache = false;
240
+ }
241
+
242
+ if ( s.cache === false && type === "GET" ) {
243
+ var ts = now();
244
+
245
+ // try replacing _= if it is there
246
+ var ret = s.url.replace(rts, "$1_=" + ts + "$2");
247
+
248
+ // if nothing was replaced, add timestamp to the end
249
+ s.url = ret + ((ret === s.url) ? (rquery.test(s.url) ? "&" : "?") + "_=" + ts : "");
250
+ }
251
+
252
+ // If data is available, append data to url for get requests
253
+ if ( s.data && type === "GET" ) {
254
+ s.url += (rquery.test(s.url) ? "&" : "?") + s.data;
255
+ }
256
+
257
+ // Matches an absolute URL, and saves the domain
258
+ var parts = rurl.exec( s.url ),
259
+ remote = parts && (parts[1] && parts[1] !== location.protocol || parts[2] !== location.host);
260
+
261
+ // If we're requesting a remote document
262
+ // and trying to load JSON or Script with a GET
263
+ if ( s.dataType === "script" && type === "GET" && remote ) {
264
+ var head = document.getElementsByTagName("head")[0] || document.documentElement;
265
+ var script = document.createElement("script");
266
+ script.src = s.url;
267
+ if ( s.scriptCharset ) {
268
+ script.charset = s.scriptCharset;
269
+ }
270
+
271
+ // Handle Script loading
272
+ if ( !jsonp ) {
273
+ var done = false;
274
+
275
+ // Attach handlers for all browsers
276
+ script.onload = script.onreadystatechange = function() {
277
+ if ( !done && (!this.readyState ||
278
+ this.readyState === "loaded" || this.readyState === "complete") ) {
279
+ done = true;
280
+ success();
281
+ complete();
282
+
283
+ // Handle memory leak in IE
284
+ script.onload = script.onreadystatechange = null;
285
+ if ( head && script.parentNode ) {
286
+ head.removeChild( script );
287
+ }
288
+ }
289
+ };
290
+ }
291
+
292
+ // Use insertBefore instead of appendChild to circumvent an IE6 bug.
293
+ // This arises when a base node is used (#2709 and #4378).
294
+ head.insertBefore( script, head.firstChild );
295
+
296
+ // We handle everything using the script element injection
297
+ return undefined;
298
+ }
299
+
300
+ var requestDone = false;
301
+
302
+ // Create the request object
303
+ var xhr = s.xhr();
304
+
305
+ if ( !xhr ) {
306
+ return;
307
+ }
308
+
309
+ // Open the socket
310
+ // Passing null username, generates a login popup on Opera (#2865)
311
+ if ( s.username ) {
312
+ xhr.open(type, s.url, s.async, s.username, s.password);
313
+ } else {
314
+ xhr.open(type, s.url, s.async);
315
+ }
316
+
317
+ // Need an extra try/catch for cross domain requests in Firefox 3
318
+ try {
319
+ // Set the correct header, if data is being sent
320
+ if ( s.data || origSettings && origSettings.contentType ) {
321
+ xhr.setRequestHeader("Content-Type", s.contentType);
322
+ }
323
+
324
+ // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode.
325
+ if ( s.ifModified ) {
326
+ if ( uki.lastModified[s.url] ) {
327
+ xhr.setRequestHeader("If-Modified-Since", uki.lastModified[s.url]);
328
+ }
329
+
330
+ if ( uki.etag[s.url] ) {
331
+ xhr.setRequestHeader("If-None-Match", uki.etag[s.url]);
332
+ }
333
+ }
334
+
335
+ // Set header so the called script knows that it's an XMLHttpRequest
336
+ // Only send the header if it's not a remote XHR
337
+ if ( !remote ) {
338
+ xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
339
+ }
340
+
341
+ // Set the Accepts header for the server, depending on the dataType
342
+ xhr.setRequestHeader("Accept", s.dataType && s.accepts[ s.dataType ] ?
343
+ s.accepts[ s.dataType ] + ", */*" :
344
+ s.accepts._default );
345
+ } catch(e) {}
346
+
347
+ // Allow custom headers/mimetypes and early abort
348
+ if ( s.beforeSend && s.beforeSend.call(callbackContext, xhr, s) === false ) {
349
+ // close opended socket
350
+ xhr.abort();
351
+ return false;
352
+ }
353
+
354
+ // Wait for a response to come back
355
+ var onreadystatechange = xhr.onreadystatechange = function( isTimeout ) {
356
+ // The request was aborted
357
+ if ( !xhr || xhr.readyState === 0 || isTimeout === "abort" ) {
358
+ // Opera doesn't call onreadystatechange before this point
359
+ // so we simulate the call
360
+ if ( !requestDone ) {
361
+ complete();
362
+ }
363
+
364
+ requestDone = true;
365
+ if ( xhr ) {
366
+ xhr.onreadystatechange = noop;
367
+ }
368
+
369
+ // The transfer is complete and the data is available, or the request timed out
370
+ } else if ( !requestDone && xhr && (xhr.readyState === 4 || isTimeout === "timeout") ) {
371
+ requestDone = true;
372
+ xhr.onreadystatechange = noop;
373
+
374
+ status = isTimeout === "timeout" ?
375
+ "timeout" :
376
+ !uki.httpSuccess( xhr ) ?
377
+ "error" :
378
+ s.ifModified && uki.httpNotModified( xhr, s.url ) ?
379
+ "notmodified" :
380
+ "success";
381
+
382
+ var errMsg;
383
+
384
+ if ( status === "success" ) {
385
+ // Watch for, and catch, XML document parse errors
386
+ try {
387
+ // process the data (runs the xml through httpData regardless of callback)
388
+ data = uki.httpData( xhr, s.dataType, s );
389
+ } catch(err) {
390
+ status = "parsererror";
391
+ errMsg = err;
392
+ }
393
+ }
394
+
395
+ // Make sure that the request was successful or notmodified
396
+ if ( status === "success" || status === "notmodified" ) {
397
+ // JSONP handles its own success callback
398
+ if ( !jsonp ) {
399
+ success();
400
+ }
401
+ } else {
402
+ uki.handleError(s, xhr, status, errMsg);
403
+ }
404
+
405
+ // Fire the complete handlers
406
+ complete();
407
+
408
+ if ( isTimeout === "timeout" ) {
409
+ xhr.abort();
410
+ }
411
+
412
+ // Stop memory leaks
413
+ if ( s.async ) {
414
+ xhr = null;
415
+ }
416
+ }
417
+ };
418
+
419
+ // Override the abort handler, if we can (IE doesn't allow it, but that's OK)
420
+ // Opera doesn't fire onreadystatechange at all on abort
421
+ try {
422
+ var oldAbort = xhr.abort;
423
+ xhr.abort = function() {
424
+ if ( xhr ) {
425
+ oldAbort.call( xhr );
426
+ }
427
+
428
+ onreadystatechange( "abort" );
429
+ };
430
+ } catch(e) { }
431
+
432
+ // Timeout checker
433
+ if ( s.async && s.timeout > 0 ) {
434
+ setTimeout(function() {
435
+ // Check to see if the request is still happening
436
+ if ( xhr && !requestDone ) {
437
+ onreadystatechange( "timeout" );
438
+ }
439
+ }, s.timeout);
440
+ }
441
+
442
+ // Send the data
443
+ try {
444
+ xhr.send( type === "POST" || type === "PUT" || type === "DELETE" ? s.data : null );
445
+ } catch(e) {
446
+ uki.handleError(s, xhr, null, e);
447
+ // Fire the complete handlers
448
+ complete();
449
+ }
450
+
451
+ // firefox 1.5 doesn't fire statechange for sync requests
452
+ if ( !s.async ) {
453
+ onreadystatechange();
454
+ }
455
+
456
+ function success() {
457
+ // If a local callback was specified, fire it and pass it the data
458
+ if ( s.success ) {
459
+ s.success.call( callbackContext, data, status, xhr );
460
+ }
461
+ }
462
+
463
+ function complete() {
464
+ // Process result
465
+ if ( s.complete ) {
466
+ s.complete.call( callbackContext, xhr, status);
467
+ }
468
+ }
469
+
470
+ // return XMLHttpRequest to allow aborting the request etc.
471
+ return xhr;
472
+ },
473
+
474
+ handleError: function( s, xhr, status, e ) {
475
+ // If a local callback was specified, fire it
476
+ if ( s.error ) {
477
+ s.error.call( s.context || s, xhr, status, e );
478
+ }
479
+ },
480
+
481
+ // Counter for holding the number of active queries
482
+ active: 0,
483
+
484
+ // Determines if an XMLHttpRequest was successful or not
485
+ httpSuccess: function( xhr ) {
486
+ try {
487
+ // IE error sometimes returns 1223 when it should be 204 so treat it as success, see #1450
488
+ return !xhr.status && location.protocol === "file:" ||
489
+ // Opera returns 0 when status is 304
490
+ ( xhr.status >= 200 && xhr.status < 300 ) ||
491
+ xhr.status === 304 || xhr.status === 1223 || xhr.status === 0;
492
+ } catch(e) {}
493
+
494
+ return false;
495
+ },
496
+
497
+ // Determines if an XMLHttpRequest returns NotModified
498
+ httpNotModified: function( xhr, url ) {
499
+ var lastModified = xhr.getResponseHeader("Last-Modified"),
500
+ etag = xhr.getResponseHeader("Etag");
501
+
502
+ if ( lastModified ) {
503
+ uki.lastModified[url] = lastModified;
504
+ }
505
+
506
+ if ( etag ) {
507
+ uki.etag[url] = etag;
508
+ }
509
+
510
+ // Opera returns 0 when status is 304
511
+ return xhr.status === 304 || xhr.status === 0;
512
+ },
513
+
514
+ httpData: function( xhr, type, s ) {
515
+ var ct = xhr.getResponseHeader("content-type") || "",
516
+ xml = type === "xml" || !type && ct.indexOf("xml") >= 0,
517
+ data = xml ? xhr.responseXML : xhr.responseText;
518
+
519
+ if ( xml && data.documentElement.nodeName === "parsererror" ) {
520
+ uki.error( "parsererror" );
521
+ }
522
+
523
+ // Allow a pre-filtering function to sanitize the response
524
+ // s is checked to keep backwards compatibility
525
+ if ( s && s.dataFilter ) {
526
+ data = s.dataFilter( data, type );
527
+ }
528
+
529
+ // The filter can actually parse the response
530
+ if ( typeof data === "string" ) {
531
+ // Get the JavaScript object, if JSON is used.
532
+ if ( type === "json" || !type && ct.indexOf("json") >= 0 ) {
533
+ data = uki.parseJSON( data );
534
+
535
+ // If the type is "script", eval it in global context
536
+ } else if ( type === "script" || !type && ct.indexOf("javascript") >= 0 ) {
537
+ uki.globalEval( data );
538
+ }
539
+ }
540
+
541
+ return data;
542
+ },
543
+
544
+
545
+ // Serialize an array of form elements or a set of
546
+ // key/values into a query string
547
+ param: function( a, traditional ) {
548
+ var s = [];
549
+
550
+ // Set traditional to true for uki <= 1.3.2 behavior.
551
+ if ( traditional === undefined ) {
552
+ traditional = uki.ajaxSettings.traditional;
553
+ }
554
+
555
+ // If an array was passed in, assume that it is an array of form elements.
556
+ if ( uki.isArray(a) || a.uki ) {
557
+ // Serialize the form elements
558
+ uki.each( a, function() {
559
+ add( this.name, this.value );
560
+ });
561
+
562
+ } else {
563
+ // If traditional, encode the "old" way (the way 1.3.2 or older
564
+ // did it), otherwise encode params recursively.
565
+ for ( var prefix in a ) {
566
+ buildParams( prefix, a[prefix] );
567
+ }
568
+ }
569
+
570
+ // Return the resulting serialization
571
+ return s.join("&").replace(r20, "+");
572
+
573
+ function buildParams( prefix, obj ) {
574
+ if ( uki.isArray(obj) ) {
575
+ // Serialize array item.
576
+ uki.each( obj, function( i, v ) {
577
+ if ( traditional || /\[\]$/.test( prefix ) ) {
578
+ // Treat each array item as a scalar.
579
+ add( prefix, v );
580
+ } else {
581
+ // If array item is non-scalar (array or object), encode its
582
+ // numeric index to resolve deserialization ambiguity issues.
583
+ // Note that rack (as of 1.0.0) can't currently deserialize
584
+ // nested arrays properly, and attempting to do so may cause
585
+ // a server error. Possible fixes are to modify rack's
586
+ // deserialization algorithm or to provide an option or flag
587
+ // to force array serialization to be shallow.
588
+ buildParams( prefix + "[" + ( typeof v === "object" || uki.isArray(v) ? i : "" ) + "]", v );
589
+ }
590
+ });
591
+
592
+ } else if ( !traditional && obj != null && typeof obj === "object" ) {
593
+ // Serialize object item.
594
+ uki.each( obj, function( k, v ) {
595
+ buildParams( prefix + "[" + k + "]", v );
596
+ });
597
+
598
+ } else {
599
+ // Serialize scalar item.
600
+ add( prefix, obj );
601
+ }
602
+ }
603
+
604
+ function add( key, value ) {
605
+ // If value is a function, invoke it and return its value
606
+ value = uki.isFunction(value) ? value() : value;
607
+ s[ s.length ] = encodeURIComponent(key) + "=" + encodeURIComponent(value);
608
+ }
609
+ }
610
+ });
611
+
612
+ })();
@@ -0,0 +1,9 @@
1
+ uki.more.view.splitTable.Render = uki.newClass(uki.view.table.Render, new function() {
2
+
3
+ this.setSelected = function(container, data, state, focus) {
4
+ focus = true;
5
+ container.style.backgroundColor = state && focus ? '#3875D7' : state ? '#CCC' : '';
6
+ container.style.color = state && focus ? '#FFF' : '#000';
7
+ }
8
+
9
+ });