/[drupal]/contributions/modules/community_tags/community_tags.js
ViewVC logotype

Contents of /contributions/modules/community_tags/community_tags.js

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.4 - (show annotations) (download) (as text)
Wed Aug 1 22:37:14 2007 UTC (2 years, 3 months ago) by unconed
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
Changes since 1.3: +2 -2 lines
File MIME type: text/javascript
Split off theme function form display call and tweak code/comments.
1 Drupal.checkPlain = function (text) {
2 return text.replace('&', '&')
3 .replace('<', '&lt;')
4 .replace('"', '&quot;')
5 .replace(/^\s+/g, '')
6 .replace(/\s+$/g, '')
7 .replace('\n', '<br />');
8 }
9
10 Drupal.serialize = function (data, prefix) {
11 prefix = prefix || '';
12 var out = '';
13 for (i in data) {
14 var name = prefix.length ? (prefix +'[' + i +']') : i;
15 if (out.length) out += '&';
16 if (typeof data[i] == 'object') {
17 out += Drupal.serialize(data[i], name);
18 }
19 else {
20 out += name +'=';
21 out += Drupal.encodeURIComponent(data[i]);
22 }
23 }
24 return out;
25 }
26
27 if (Drupal.jsEnabled) {
28 $(document).ready(function () {
29 // Note: all tag fields are autocompleted, and have already been initialized at this point.
30 $('input.form-tags').each(function () {
31 // Hide submit buttons.
32 $('input[@type=submit]', this.form).hide();
33
34 // Fetch settings.
35 var o = Drupal.settings.communityTags;
36 var sequence = 0;
37
38 // Patch for drupal_to_js() bug in Drupal 5.1
39 if (typeof o.tags.push == 'undefined') {
40 o.tags = [];
41 }
42
43 // Show the textfield and empty its value.
44 var textfield = $(this).val('').css('display', 'inline');
45
46 // Prepare the add Ajax handler and add the button.
47 var addHandler = function () {
48 // Send existing tags and new tag string.
49 $.post(o.url, Drupal.serialize({ sequence: ++sequence, tags: o.tags, add: textfield[0].value }), function (data) {
50 data = Drupal.parseJson(data);
51 if (data.status && sequence == data.sequence) {
52 o.tags = data.tags;
53 updateList();
54 }
55 });
56
57 // Add tag to local list
58 o.tags.push(textfield[0].value);
59 o.tags.sort(function (a,b) { a = a.toLowerCase(); b = b.toLowerCase(); return (a>b) ? 1 : (a<b) ? -1 : 0; });
60 updateList();
61
62 // Clear field and focus it.
63 textfield.val('').focus();
64 };
65 var button = $('<input type="button" class="form-button" value="'+ Drupal.checkPlain(o.add) +'" />').click(addHandler);
66 $(this.form).submit(function () { addHandler(); return false; });
67
68 // Prepare the delete Ajax handler.
69 var deleteHandler = function () {
70 // Remove tag from local list.
71 var i = $(this).attr('key');
72 o.tags.splice(i, 1);
73 updateList();
74
75 // Send new tag list.
76 $.post(o.url, Drupal.serialize({ sequence: ++sequence, tags: o.tags, add: '' }), function (data) {
77 data = Drupal.parseJson(data);
78 if (data.status && sequence == data.sequence) {
79 o.tags = data.tags;
80 updateList();
81 }
82 });
83
84 // Clear textfield and focus it.
85 textfield.val('').focus();
86 };
87
88 // Callback to update the tag list.
89 function updateList() {
90 list.empty();
91 for (i in o.tags) {
92 list.append('<li key="'+ Drupal.checkPlain(i) +'">'+ Drupal.checkPlain(o.tags[i]) +'</li>');
93 }
94 $('li', list).click(deleteHandler);
95 }
96
97 // Create widget markup.
98 var widget = $('<div class="tag-widget"><ul class="inline-tags clear-block"></ul></div>');
99 textfield.before(widget);
100 widget.append(textfield).append(button);
101 var list = $('ul', widget);
102
103 updateList();
104 });
105 });
106 }

  ViewVC Help
Powered by ViewVC 1.1.2