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

Contents of /contributions/modules/adlib/adlib.js

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


Revision 1.8 - (show annotations) (download) (as text)
Thu Apr 30 14:55:10 2009 UTC (6 months, 4 weeks ago) by nedjo
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +0 -0 lines
File MIME type: text/javascript
Introduce configuration setting to control whether adlibs can be set for all forms or only node forms (node forms only being the default).
1 // $Id: adlib.js,v 1.6 2009/04/29 23:45:39 nedjo Exp $
2
3 Drupal.adlib = {};
4
5 /**
6 * Attach the adlib behaviour.
7 */
8 Drupal.adlib.attach = function () {
9 var settings = Drupal.settings.adlib;
10
11 var target = $('<div />').addClass('adlib-message');
12 $('.adlib-form:not(.adlib-form-processed)')
13 .addClass('adlib-form-processed')
14 .before($('<div />').addClass('adlib-errors'))
15 .after($('<div />').addClass('adlib-preview'))
16 .find('.adlib-element')
17 .after(target)
18 .each(function () {
19 var adlibElement = this;
20 var id = Drupal.adlib.getId(this);
21 $(this)
22 .append($('#' + id + '-wrapper').hide())
23 .find('.adlib-message')
24 .click(function () {
25 var adlibMessage = this;
26 var submit = $('<button type="submit" class="adlib-button adlib-submit" />')
27 .html(settings.submit)
28 .click(function () {
29 $(this).addClass('throbbing');
30 // We need preview functionality to be able to use Ajax.
31 // Determine if there is a preview submit button.
32 if ($('input.form-submit[value=' + settings.preview + ']', this.form).size()) {
33 Drupal.adlib.dispatch(this, id, adlibMessage, adlibElement, target, settings);
34 }
35 // Otherwise we just insert the value entered in the form.
36 // However, we still need Ajax since the value may have
37 // been loaded from the server and is unfiltered.
38 else {
39 Drupal.adlib.checkPlain(this, id, adlibMessage, adlibElement, target, settings);
40 }
41 return false;
42 });
43 var cancel = $('<button type="cancel" class="adlib-button adlib-cancel" />')
44 .html(settings.cancel)
45 .click(function () {;
46 $('#' + id + '-wrapper').hide();
47 $(adlibElement)
48 .find('.adlib-buttons')
49 .remove();
50 $(adlibMessage).fadeIn();
51 return false;
52 });
53 var buttons = $('<span class="adlib-buttons" />')
54 .append(submit)
55 .append(cancel);
56 $('#' + id + '-wrapper').fadeIn();
57 $(this)
58 .hide()
59 .parent()
60 .append(buttons);
61 });
62 });
63 };
64
65 /**
66 * Get the ID of the form element to be used.
67 */
68 Drupal.adlib.getId = function (elt) {
69 var id = $(elt).attr('id');
70 // Remove the leading adlib-.
71 return id.substring(6);
72 };
73
74 /**
75 * Submit a form for preview and extract the result for a given form
76 * element.
77 */
78 Drupal.adlib.dispatch = function (submit, id, adlibMessage, adlibElement, target, settings) {
79 var form = submit.form;
80
81 // Set options.
82 var options = {
83 dataType: 'json',
84 data: {op: settings.preview, ajaxsubmit: 1},
85 beforeSubmit: function () {
86 // Remove any error messages.
87 $(':input', form).removeClass('error');
88 $('.adlib-errors', form).html('');
89 $(adlibElement)
90 .find('.adlib-submit')
91 .addClass('throbbing');
92 },
93 success: function (data) {
94 var name = $('#' + id).attr('name') || $(adlibElement).find(':input:first').attr('name');
95 if (name.indexOf('[') != -1) {
96 name = name.substring(0, name.indexOf('['));
97 }
98 if (data.errors) {
99 for (var eltName in data.errors) {
100 $('[@name=' + eltName + ']', form).addClass('error');
101 }
102 var offset = $('.adlib-errors', form).html(data.message).offset();
103 $('html,body').animate({scrollTop: (offset.top - 10)}, 500);
104 }
105 // Set preview.
106 else if (data.preview) {
107 $('#' + id + '-wrapper').hide();
108 var html = $(form)
109 .find('.adlib-preview')
110 .hide()
111 .html(data.preview)
112 .fadeIn()
113 .find('.adlib-property-' + name)
114 .html();
115 $(adlibMessage).html(html);
116 $(adlibElement)
117 .find('.adlib-buttons')
118 .remove();
119 $(adlibMessage).fadeIn();
120 }
121 },
122 error: function(data, status) {
123 alert(settins.error);
124 $(adlibElement)
125 .find('.adlib-buttons')
126 .remove();
127 $(adlibMessage).fadeIn();
128 },
129 };
130
131 $(form).ajaxSubmit(options);
132 }
133
134 /**
135 * Pass the edited value of an adlib element for filtering on the server.
136 */
137 Drupal.adlib.checkPlain = function (submit, id, adlibMessage, adlibElement, target, settings) {
138 // The element may have more than one input. We just concatenate
139 // the values of all. Not perfect.
140 var val = $('#' + id + '-wrapper :input').fieldValue().split(' ');
141
142 // Set options.
143 var options = {
144 url: settings.checkPlainPath,
145 dataType: 'json',
146 data: {val: val},
147 beforeSubmit: function () {
148 $(adlibElement)
149 .find('.adlib-submit')
150 .addClass('throbbing');
151 },
152 success: function (data) {
153 $('#' + id + '-wrapper').hide();
154 $(adlibMessage).html(data.val);
155 $(adlibElement)
156 .find('.adlib-buttons')
157 .remove();
158 $(adlibMessage).fadeIn();
159 },
160 error: function(data, status) {
161 alert(settings.error);
162 $(adlibElement)
163 .find('.adlib-buttons')
164 .remove();
165 $(adlibMessage).fadeIn();
166 },
167 };
168
169 $.ajax(options);
170 }
171
172 // Global Killswitch
173 if (Drupal.jsEnabled) {
174 $(document).ready(Drupal.adlib.attach);
175 }

  ViewVC Help
Powered by ViewVC 1.1.2