rep.jquery 1.3.2

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.
Files changed (41) hide show
  1. data/.gitignore +25 -0
  2. data/LICENSE +20 -0
  3. data/README +1 -0
  4. data/Rakefile +19 -0
  5. data/TODO +0 -0
  6. data/VERSION +1 -0
  7. data/lib/jquery.rb +1 -0
  8. data/public/images/jquery.tablesorter.blue/asc.gif +0 -0
  9. data/public/images/jquery.tablesorter.blue/bg.gif +0 -0
  10. data/public/images/jquery.tablesorter.blue/desc.gif +0 -0
  11. data/public/images/jquery.tablesorter.blue/style.css +39 -0
  12. data/public/images/ui-bg_flat_0_aaaaaa_40x100.png +0 -0
  13. data/public/images/ui-bg_flat_75_ffffff_40x100.png +0 -0
  14. data/public/images/ui-bg_glass_55_fbf9ee_1x400.png +0 -0
  15. data/public/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
  16. data/public/images/ui-bg_glass_75_dadada_1x400.png +0 -0
  17. data/public/images/ui-bg_glass_75_e6e6e6_1x400.png +0 -0
  18. data/public/images/ui-bg_glass_95_fef1ec_1x400.png +0 -0
  19. data/public/images/ui-bg_highlight-soft_75_cccccc_1x100.png +0 -0
  20. data/public/images/ui-icons_222222_256x240.png +0 -0
  21. data/public/images/ui-icons_2e83ff_256x240.png +0 -0
  22. data/public/images/ui-icons_454545_256x240.png +0 -0
  23. data/public/images/ui-icons_888888_256x240.png +0 -0
  24. data/public/images/ui-icons_cd0a0a_256x240.png +0 -0
  25. data/public/javascripts/jquery/autogrow.js +134 -0
  26. data/public/javascripts/jquery/easing.js +207 -0
  27. data/public/javascripts/jquery/form.js +639 -0
  28. data/public/javascripts/jquery/jeditable.autogrow.js +41 -0
  29. data/public/javascripts/jquery/jeditable.js +545 -0
  30. data/public/javascripts/jquery/metadata.js +150 -0
  31. data/public/javascripts/jquery/scrollTo.js +217 -0
  32. data/public/javascripts/jquery/suggest.js +314 -0
  33. data/public/javascripts/jquery/tablesorter.patched.js +866 -0
  34. data/public/javascripts/jquery/template.js +93 -0
  35. data/public/javascripts/jquery/tooltip.js +296 -0
  36. data/public/javascripts/jquery/ui.all.js +300 -0
  37. data/public/javascripts/jquery.js +4376 -0
  38. data/public/stylesheets/jquery-ui.css +404 -0
  39. data/public/stylesheets/jquery.suggest.css +29 -0
  40. data/rep.jquery.gemspec +81 -0
  41. metadata +119 -0
@@ -0,0 +1,545 @@
1
+ //= require "../jquery"
2
+
3
+ /*
4
+ * Jeditable - jQuery in place edit plugin
5
+ *
6
+ * Copyright (c) 2006-2009 Mika Tuupola, Dylan Verheul
7
+ *
8
+ * Licensed under the MIT license:
9
+ * http://www.opensource.org/licenses/mit-license.php
10
+ *
11
+ * Project home:
12
+ * http://www.appelsiini.net/projects/jeditable
13
+ *
14
+ * Based on editable by Dylan Verheul <dylan_at_dyve.net>:
15
+ * http://www.dyve.net/jquery/?editable
16
+ *
17
+ */
18
+
19
+ /**
20
+ * Version 1.7.1
21
+ *
22
+ * ** means there is basic unit tests for this parameter.
23
+ *
24
+ * @name Jeditable
25
+ * @type jQuery
26
+ * @param String target (POST) URL or function to send edited content to **
27
+ * @param Hash options additional options
28
+ * @param String options[method] method to use to send edited content (POST or PUT) **
29
+ * @param Function options[callback] Function to run after submitting edited content **
30
+ * @param String options[name] POST parameter name of edited content
31
+ * @param String options[id] POST parameter name of edited div id
32
+ * @param Hash options[submitdata] Extra parameters to send when submitting edited content.
33
+ * @param String options[type] text, textarea or select (or any 3rd party input type) **
34
+ * @param Integer options[rows] number of rows if using textarea **
35
+ * @param Integer options[cols] number of columns if using textarea **
36
+ * @param Mixed options[height] 'auto', 'none' or height in pixels **
37
+ * @param Mixed options[width] 'auto', 'none' or width in pixels **
38
+ * @param String options[loadurl] URL to fetch input content before editing **
39
+ * @param String options[loadtype] Request type for load url. Should be GET or POST.
40
+ * @param String options[loadtext] Text to display while loading external content.
41
+ * @param Mixed options[loaddata] Extra parameters to pass when fetching content before editing.
42
+ * @param Mixed options[data] Or content given as paramameter. String or function.**
43
+ * @param String options[indicator] indicator html to show when saving
44
+ * @param String options[tooltip] optional tooltip text via title attribute **
45
+ * @param String options[event] jQuery event such as 'click' of 'dblclick' **
46
+ * @param String options[submit] submit button value, empty means no button **
47
+ * @param String options[cancel] cancel button value, empty means no button **
48
+ * @param String options[cssclass] CSS class to apply to input form. 'inherit' to copy from parent. **
49
+ * @param String options[style] Style to apply to input form 'inherit' to copy from parent. **
50
+ * @param String options[select] true or false, when true text is highlighted ??
51
+ * @param String options[placeholder] Placeholder text or html to insert when element is empty. **
52
+ * @param String options[onblur] 'cancel', 'submit', 'ignore' or function ??
53
+ *
54
+ * @param Function options[onsubmit] function(settings, original) { ... } called before submit
55
+ * @param Function options[onreset] function(settings, original) { ... } called before reset
56
+ * @param Function options[onerror] function(settings, original, xhr) { ... } called on error
57
+ *
58
+ * @param Hash options[ajaxoptions] jQuery Ajax options. See docs.jquery.com.
59
+ *
60
+ */
61
+
62
+ (function($) {
63
+
64
+ $.fn.editable = function(target, options) {
65
+
66
+ if ('disable' == target) {
67
+ $(this).data('disabled.editable', true);
68
+ return;
69
+ }
70
+ if ('enable' == target) {
71
+ $(this).data('disabled.editable', false);
72
+ return;
73
+ }
74
+ if ('destroy' == target) {
75
+ $(this)
76
+ .unbind($(this).data('event.editable'))
77
+ .removeData('disabled.editable')
78
+ .removeData('event.editable');
79
+ return;
80
+ }
81
+
82
+ var settings = $.extend({}, $.fn.editable.defaults, {target:target}, options);
83
+
84
+ /* setup some functions */
85
+ var plugin = $.editable.types[settings.type].plugin || function() { };
86
+ var submit = $.editable.types[settings.type].submit || function() { };
87
+ var buttons = $.editable.types[settings.type].buttons
88
+ || $.editable.types['defaults'].buttons;
89
+ var content = $.editable.types[settings.type].content
90
+ || $.editable.types['defaults'].content;
91
+ var element = $.editable.types[settings.type].element
92
+ || $.editable.types['defaults'].element;
93
+ var reset = $.editable.types[settings.type].reset
94
+ || $.editable.types['defaults'].reset;
95
+ var callback = settings.callback || function() { };
96
+ var onedit = settings.onedit || function() { };
97
+ var onsubmit = settings.onsubmit || function() { };
98
+ var onreset = settings.onreset || function() { };
99
+ var onerror = settings.onerror || reset;
100
+
101
+ /* show tooltip */
102
+ if (settings.tooltip) {
103
+ $(this).attr('title', settings.tooltip);
104
+ }
105
+
106
+ settings.autowidth = 'auto' == settings.width;
107
+ settings.autoheight = 'auto' == settings.height;
108
+
109
+ return this.each(function() {
110
+
111
+ /* save this to self because this changes when scope changes */
112
+ var self = this;
113
+
114
+ /* inlined block elements lose their width and height after first edit */
115
+ /* save them for later use as workaround */
116
+ var savedwidth = $(self).width();
117
+ var savedheight = $(self).height();
118
+
119
+ /* save so it can be later used by $.editable('destroy') */
120
+ $(this).data('event.editable', settings.event);
121
+
122
+ /* if element is empty add something clickable (if requested) */
123
+ if (!$.trim($(this).html())) {
124
+ $(this).html(settings.placeholder);
125
+ }
126
+
127
+ $(this).bind(settings.event, function(e) {
128
+
129
+ /* abort if disabled for this element */
130
+ if (true === $(this).data('disabled.editable')) {
131
+ return;
132
+ }
133
+
134
+ /* prevent throwing an exeption if edit field is clicked again */
135
+ if (self.editing) {
136
+ return;
137
+ }
138
+
139
+ /* abort if onedit hook returns false */
140
+ if (false === onedit.apply(this, [settings, self])) {
141
+ return;
142
+ }
143
+
144
+ /* prevent default action and bubbling */
145
+ e.preventDefault();
146
+ e.stopPropagation();
147
+
148
+ /* remove tooltip */
149
+ if (settings.tooltip) {
150
+ $(self).removeAttr('title');
151
+ }
152
+
153
+ /* figure out how wide and tall we are, saved width and height */
154
+ /* are workaround for http://dev.jquery.com/ticket/2190 */
155
+ if (0 == $(self).width()) {
156
+ //$(self).css('visibility', 'hidden');
157
+ settings.width = savedwidth;
158
+ settings.height = savedheight;
159
+ } else {
160
+ if (settings.width != 'none') {
161
+ settings.width =
162
+ settings.autowidth ? $(self).width() : settings.width;
163
+ }
164
+ if (settings.height != 'none') {
165
+ settings.height =
166
+ settings.autoheight ? $(self).height() : settings.height;
167
+ }
168
+ }
169
+ //$(this).css('visibility', '');
170
+
171
+ /* remove placeholder text, replace is here because of IE */
172
+ if ($(this).html().toLowerCase().replace(/(;|")/g, '') ==
173
+ settings.placeholder.toLowerCase().replace(/(;|")/g, '')) {
174
+ $(this).html('');
175
+ }
176
+
177
+ self.editing = true;
178
+ self.revert = $(self).html();
179
+ $(self).html('');
180
+
181
+ /* create the form object */
182
+ var form = $('<form />');
183
+
184
+ /* apply css or style or both */
185
+ if (settings.cssclass) {
186
+ if ('inherit' == settings.cssclass) {
187
+ form.attr('class', $(self).attr('class'));
188
+ } else {
189
+ form.attr('class', settings.cssclass);
190
+ }
191
+ }
192
+
193
+ if (settings.style) {
194
+ if ('inherit' == settings.style) {
195
+ form.attr('style', $(self).attr('style'));
196
+ /* IE needs the second line or display wont be inherited */
197
+ form.css('display', $(self).css('display'));
198
+ } else {
199
+ form.attr('style', settings.style);
200
+ }
201
+ }
202
+
203
+ /* add main input element to form and store it in input */
204
+ var input = element.apply(form, [settings, self]);
205
+
206
+ /* set input content via POST, GET, given data or existing value */
207
+ var input_content;
208
+
209
+ if (settings.loadurl) {
210
+ var t = setTimeout(function() {
211
+ input.disabled = true;
212
+ content.apply(form, [settings.loadtext, settings, self]);
213
+ }, 100);
214
+
215
+ var loaddata = {};
216
+ loaddata[settings.id] = self.id;
217
+ if ($.isFunction(settings.loaddata)) {
218
+ $.extend(loaddata, settings.loaddata.apply(self, [self.revert, settings]));
219
+ } else {
220
+ $.extend(loaddata, settings.loaddata);
221
+ }
222
+ $.ajax({
223
+ type : settings.loadtype,
224
+ url : settings.loadurl,
225
+ data : loaddata,
226
+ async : false,
227
+ success: function(result) {
228
+ window.clearTimeout(t);
229
+ input_content = result;
230
+ input.disabled = false;
231
+ }
232
+ });
233
+ } else if (settings.data) {
234
+ input_content = settings.data;
235
+ if ($.isFunction(settings.data)) {
236
+ input_content = settings.data.apply(self, [self.revert, settings]);
237
+ }
238
+ } else {
239
+ input_content = self.revert;
240
+ }
241
+ content.apply(form, [input_content, settings, self]);
242
+
243
+ input.attr('name', settings.name);
244
+
245
+ /* add buttons to the form */
246
+ buttons.apply(form, [settings, self]);
247
+
248
+ /* add created form to self */
249
+ $(self).append(form);
250
+
251
+ /* attach 3rd party plugin if requested */
252
+ plugin.apply(form, [settings, self]);
253
+
254
+ /* focus to first visible form element */
255
+ $(':input:visible:enabled:first', form).focus();
256
+
257
+ /* highlight input contents when requested */
258
+ if (settings.select) {
259
+ input.select();
260
+ }
261
+
262
+ /* discard changes if pressing esc */
263
+ input.keydown(function(e) {
264
+ if (e.keyCode == 27) {
265
+ e.preventDefault();
266
+ //self.reset();
267
+ reset.apply(form, [settings, self]);
268
+ }
269
+ });
270
+
271
+ /* discard, submit or nothing with changes when clicking outside */
272
+ /* do nothing is usable when navigating with tab */
273
+ var t;
274
+ if ('cancel' == settings.onblur) {
275
+ input.blur(function(e) {
276
+ /* prevent canceling if submit was clicked */
277
+ t = setTimeout(function() {
278
+ reset.apply(form, [settings, self]);
279
+ }, 500);
280
+ });
281
+ } else if ('submit' == settings.onblur) {
282
+ input.blur(function(e) {
283
+ /* prevent double submit if submit was clicked */
284
+ t = setTimeout(function() {
285
+ form.submit();
286
+ }, 200);
287
+ });
288
+ } else if ($.isFunction(settings.onblur)) {
289
+ input.blur(function(e) {
290
+ settings.onblur.apply(self, [input.val(), settings]);
291
+ });
292
+ } else {
293
+ input.blur(function(e) {
294
+ /* TODO: maybe something here */
295
+ });
296
+ }
297
+
298
+ form.submit(function(e) {
299
+
300
+ if (t) {
301
+ clearTimeout(t);
302
+ }
303
+
304
+ /* do no submit */
305
+ e.preventDefault();
306
+
307
+ /* call before submit hook. */
308
+ /* if it returns false abort submitting */
309
+ if (false !== onsubmit.apply(form, [settings, self])) {
310
+ /* custom inputs call before submit hook. */
311
+ /* if it returns false abort submitting */
312
+ if (false !== submit.apply(form, [settings, self])) {
313
+
314
+ /* check if given target is function */
315
+ if ($.isFunction(settings.target)) {
316
+ var str = settings.target.apply(self, [input.val(), settings]);
317
+ $(self).html(str);
318
+ self.editing = false;
319
+ callback.apply(self, [self.innerHTML, settings]);
320
+ /* TODO: this is not dry */
321
+ if (!$.trim($(self).html())) {
322
+ $(self).html(settings.placeholder);
323
+ }
324
+ } else {
325
+ /* add edited content and id of edited element to POST */
326
+ var submitdata = {};
327
+ submitdata[settings.name] = input.val();
328
+ submitdata[settings.id] = self.id;
329
+ /* add extra data to be POST:ed */
330
+ if ($.isFunction(settings.submitdata)) {
331
+ $.extend(submitdata, settings.submitdata.apply(self, [self.revert, settings]));
332
+ } else {
333
+ $.extend(submitdata, settings.submitdata);
334
+ }
335
+
336
+ /* quick and dirty PUT support */
337
+ if ('PUT' == settings.method) {
338
+ submitdata['_method'] = 'put';
339
+ }
340
+
341
+ /* show the saving indicator */
342
+ $(self).html(settings.indicator);
343
+
344
+ /* defaults for ajaxoptions */
345
+ var ajaxoptions = {
346
+ type : 'POST',
347
+ data : submitdata,
348
+ dataType: 'html',
349
+ url : settings.target,
350
+ success : function(result, status) {
351
+ if (ajaxoptions.dataType == 'html') {
352
+ $(self).html(result);
353
+ }
354
+ self.editing = false;
355
+ callback.apply(self, [result, settings]);
356
+ if (!$.trim($(self).html())) {
357
+ $(self).html(settings.placeholder);
358
+ }
359
+ },
360
+ error : function(xhr, status, error) {
361
+ onerror.apply(form, [settings, self, xhr]);
362
+ }
363
+ };
364
+
365
+ /* override with what is given in settings.ajaxoptions */
366
+ $.extend(ajaxoptions, settings.ajaxoptions);
367
+ $.ajax(ajaxoptions);
368
+
369
+ }
370
+ }
371
+ }
372
+
373
+ /* show tooltip again */
374
+ $(self).attr('title', settings.tooltip);
375
+
376
+ return false;
377
+ });
378
+ });
379
+
380
+ /* privileged methods */
381
+ this.reset = function(form) {
382
+ /* prevent calling reset twice when blurring */
383
+ if (this.editing) {
384
+ /* before reset hook, if it returns false abort reseting */
385
+ if (false !== onreset.apply(form, [settings, self])) {
386
+ $(self).html(self.revert);
387
+ self.editing = false;
388
+ if (!$.trim($(self).html())) {
389
+ $(self).html(settings.placeholder);
390
+ }
391
+ /* show tooltip again */
392
+ if (settings.tooltip) {
393
+ $(self).attr('title', settings.tooltip);
394
+ }
395
+ }
396
+ }
397
+ };
398
+ });
399
+
400
+ };
401
+
402
+
403
+ $.editable = {
404
+ types: {
405
+ defaults: {
406
+ element : function(settings, original) {
407
+ var input = $('<input type="hidden"></input>');
408
+ $(this).append(input);
409
+ return(input);
410
+ },
411
+ content : function(string, settings, original) {
412
+ $(':input:first', this).val(string);
413
+ },
414
+ reset : function(settings, original) {
415
+ original.reset(this);
416
+ },
417
+ buttons : function(settings, original) {
418
+ var form = this;
419
+ if (settings.submit) {
420
+ /* if given html string use that */
421
+ if (settings.submit.match(/>$/)) {
422
+ var submit = $(settings.submit).click(function() {
423
+ if (submit.attr("type") != "submit") {
424
+ form.submit();
425
+ }
426
+ });
427
+ /* otherwise use button with given string as text */
428
+ } else {
429
+ var submit = $('<button type="submit" />');
430
+ submit.html(settings.submit);
431
+ }
432
+ $(this).append(submit);
433
+ }
434
+ if (settings.cancel) {
435
+ /* if given html string use that */
436
+ if (settings.cancel.match(/>$/)) {
437
+ var cancel = $(settings.cancel);
438
+ /* otherwise use button with given string as text */
439
+ } else {
440
+ var cancel = $('<button type="cancel" />');
441
+ cancel.html(settings.cancel);
442
+ }
443
+ $(this).append(cancel);
444
+
445
+ $(cancel).click(function(event) {
446
+ //original.reset();
447
+ if ($.isFunction($.editable.types[settings.type].reset)) {
448
+ var reset = $.editable.types[settings.type].reset;
449
+ } else {
450
+ var reset = $.editable.types['defaults'].reset;
451
+ }
452
+ reset.apply(form, [settings, original]);
453
+ return false;
454
+ });
455
+ }
456
+ }
457
+ },
458
+ text: {
459
+ element : function(settings, original) {
460
+ var input = $('<input />');
461
+ if (settings.width != 'none') { input.width(settings.width); }
462
+ if (settings.height != 'none') { input.height(settings.height); }
463
+ /* https://bugzilla.mozilla.org/show_bug.cgi?id=236791 */
464
+ //input[0].setAttribute('autocomplete','off');
465
+ input.attr('autocomplete','off');
466
+ $(this).append(input);
467
+ return(input);
468
+ }
469
+ },
470
+ textarea: {
471
+ element : function(settings, original) {
472
+ var textarea = $('<textarea />');
473
+ if (settings.rows) {
474
+ textarea.attr('rows', settings.rows);
475
+ } else if (settings.height != "none") {
476
+ textarea.height(settings.height);
477
+ }
478
+ if (settings.cols) {
479
+ textarea.attr('cols', settings.cols);
480
+ } else if (settings.width != "none") {
481
+ textarea.width(settings.width);
482
+ }
483
+ $(this).append(textarea);
484
+ return(textarea);
485
+ }
486
+ },
487
+ select: {
488
+ element : function(settings, original) {
489
+ var select = $('<select />');
490
+ $(this).append(select);
491
+ return(select);
492
+ },
493
+ content : function(data, settings, original) {
494
+ /* If it is string assume it is json. */
495
+ if (String == data.constructor) {
496
+ eval ('var json = ' + data);
497
+ } else {
498
+ /* Otherwise assume it is a hash already. */
499
+ var json = data;
500
+ }
501
+ for (var key in json) {
502
+ if (!json.hasOwnProperty(key)) {
503
+ continue;
504
+ }
505
+ if ('selected' == key) {
506
+ continue;
507
+ }
508
+ var option = $('<option />').val(key).append(json[key]);
509
+ $('select', this).append(option);
510
+ }
511
+ /* Loop option again to set selected. IE needed this... */
512
+ $('select', this).children().each(function() {
513
+ if ($(this).val() == json['selected'] ||
514
+ $(this).text() == $.trim(original.revert)) {
515
+ $(this).attr('selected', 'selected');
516
+ }
517
+ });
518
+ }
519
+ }
520
+ },
521
+
522
+ /* Add new input type */
523
+ addInputType: function(name, input) {
524
+ $.editable.types[name] = input;
525
+ }
526
+ };
527
+
528
+ // publicly accessible defaults
529
+ $.fn.editable.defaults = {
530
+ name : 'value',
531
+ id : 'id',
532
+ type : 'text',
533
+ width : 'auto',
534
+ height : 'auto',
535
+ event : 'click.editable',
536
+ onblur : 'cancel',
537
+ loadtype : 'GET',
538
+ loadtext : 'Loading...',
539
+ placeholder: 'Click to edit',
540
+ loaddata : {},
541
+ submitdata : {},
542
+ ajaxoptions: {}
543
+ };
544
+
545
+ })(jQuery);
@@ -0,0 +1,150 @@
1
+ //= require "../jquery"
2
+
3
+ /*
4
+ * Metadata - jQuery plugin for parsing metadata from elements
5
+ *
6
+ * Copyright (c) 2006 John Resig, Yehuda Katz, J�örn Zaefferer, Paul McLanahan
7
+ *
8
+ * Dual licensed under the MIT and GPL licenses:
9
+ * http://www.opensource.org/licenses/mit-license.php
10
+ * http://www.gnu.org/licenses/gpl.html
11
+ *
12
+ * Revision: $Id: jquery.metadata.js 3640 2007-10-11 18:34:38Z pmclanahan $
13
+ *
14
+ */
15
+
16
+ /**
17
+ * Sets the type of metadata to use. Metadata is encoded in JSON, and each property
18
+ * in the JSON will become a property of the element itself.
19
+ *
20
+ * There are four supported types of metadata storage:
21
+ *
22
+ * attr: Inside an attribute. The name parameter indicates *which* attribute.
23
+ *
24
+ * class: Inside the class attribute, wrapped in curly braces: { }
25
+ *
26
+ * elem: Inside a child element (e.g. a script tag). The
27
+ * name parameter indicates *which* element.
28
+ * html5: Values are stored in data-* attributes.
29
+ *
30
+ * The metadata for an element is loaded the first time the element is accessed via jQuery.
31
+ *
32
+ * As a result, you can define the metadata type, use $(expr) to load the metadata into the elements
33
+ * matched by expr, then redefine the metadata type and run another $(expr) for other elements.
34
+ *
35
+ * @name $.metadata.setType
36
+ *
37
+ * @example <p id="one" class="some_class {item_id: 1, item_label: 'Label'}">This is a p</p>
38
+ * @before $.metadata.setType("class")
39
+ * @after $("#one").metadata().item_id == 1; $("#one").metadata().item_label == "Label"
40
+ * @desc Reads metadata from the class attribute
41
+ *
42
+ * @example <p id="one" class="some_class" data="{item_id: 1, item_label: 'Label'}">This is a p</p>
43
+ * @before $.metadata.setType("attr", "data")
44
+ * @after $("#one").metadata().item_id == 1; $("#one").metadata().item_label == "Label"
45
+ * @desc Reads metadata from a "data" attribute
46
+ *
47
+ * @example <p id="one" class="some_class"><script>{item_id: 1, item_label: 'Label'}</script>This is a p</p>
48
+ * @before $.metadata.setType("elem", "script")
49
+ * @after $("#one").metadata().item_id == 1; $("#one").metadata().item_label == "Label"
50
+ * @desc Reads metadata from a nested script element
51
+ *
52
+ * @example <p id="one" class="some_class" data-item_id="1" data-item_label="Label">This is a p</p>
53
+ * @before $.metadata.setType("html5")
54
+ * @after $("#one").metadata().item_id == 1; $("#one").metadata().item_label == "Label"
55
+ * @desc Reads metadata from a series of data-* attributes
56
+ *
57
+ * @param String type The encoding type
58
+ * @param String name The name of the attribute to be used to get metadata (optional)
59
+ * @cat Plugins/Metadata
60
+ * @descr Sets the type of encoding to be used when loading metadata for the first time
61
+ * @type undefined
62
+ * @see metadata()
63
+ */
64
+
65
+ (function($) {
66
+
67
+ $.extend({
68
+ metadata : {
69
+ defaults : {
70
+ type: 'class',
71
+ name: 'metadata',
72
+ cre: /({.*})/,
73
+ single: 'metadata'
74
+ },
75
+ setType: function( type, name ){
76
+ this.defaults.type = type;
77
+ this.defaults.name = name;
78
+ },
79
+ get: function( elem, opts ){
80
+ var settings = $.extend({},this.defaults,opts);
81
+ // check for empty string in single property
82
+ if ( !settings.single.length ) settings.single = 'metadata';
83
+
84
+ var data = $.data(elem, settings.single);
85
+ // returned cached data if it already exists
86
+ if ( data ) return data;
87
+
88
+ data = "{}";
89
+
90
+ var getData = function(data) {
91
+ if(typeof data != "string") return data;
92
+
93
+ if( data.indexOf('{') < 0 ) {
94
+ data = eval("(" + data + ")");
95
+ }
96
+ }
97
+
98
+ var getObject = function(data) {
99
+ if(typeof data != "string") return data;
100
+
101
+ data = eval("(" + data + ")");
102
+ return data;
103
+ }
104
+
105
+ if ( settings.type == "html5" ) {
106
+ var object = {};
107
+ $( elem.attributes ).each(function() {
108
+ var name = this.nodeName;
109
+ if(name.match(/^data-/)) name = name.replace(/^data-/, '');
110
+ else return true;
111
+ object[name] = getObject(this.nodeValue);
112
+ });
113
+ } else {
114
+ if ( settings.type == "class" ) {
115
+ var m = settings.cre.exec( elem.className );
116
+ if ( m )
117
+ data = m[1];
118
+ } else if ( settings.type == "elem" ) {
119
+ if( !elem.getElementsByTagName ) return;
120
+ var e = elem.getElementsByTagName(settings.name);
121
+ if ( e.length )
122
+ data = $.trim(e[0].innerHTML);
123
+ } else if ( elem.getAttribute != undefined ) {
124
+ var attr = elem.getAttribute( settings.name );
125
+ if ( attr )
126
+ data = attr;
127
+ }
128
+ object = getObject(data.indexOf("{") < 0 ? "{" + data + "}" : data);
129
+ }
130
+
131
+ $.data( elem, settings.single, object );
132
+ return object;
133
+ }
134
+ }
135
+ });
136
+
137
+ /**
138
+ * Returns the metadata object for the first member of the jQuery object.
139
+ *
140
+ * @name metadata
141
+ * @descr Returns element's metadata object
142
+ * @param Object opts An object contianing settings to override the defaults
143
+ * @type jQuery
144
+ * @cat Plugins/Metadata
145
+ */
146
+ $.fn.metadata = function( opts ){
147
+ return $.metadata.get( this[0], opts );
148
+ };
149
+
150
+ })(jQuery);