| 1 |
// $Id: ac.js,v 1.9 2009/06/17 22:48:15 ac Exp $
|
| 2 |
|
| 3 |
var AC = AC || { locks: {} };
|
| 4 |
|
| 5 |
/**
|
| 6 |
* Send an ajax request.
|
| 7 |
*
|
| 8 |
* @param string module
|
| 9 |
* Module name
|
| 10 |
*
|
| 11 |
* @param string op
|
| 12 |
* Operation
|
| 13 |
*
|
| 14 |
* @param object data
|
| 15 |
* (optional) Key:value pairs used to generate query string.
|
| 16 |
* When omitted this will be considered the callback function.
|
| 17 |
*
|
| 18 |
* @param function callback
|
| 19 |
* Callback function once request is complete.
|
| 20 |
*/
|
| 21 |
AC.request = function(module, op, data, callback) {
|
| 22 |
var d = typeof data == 'function' ? {} : data;
|
| 23 |
var c = typeof data == 'function' ? data : callback;
|
| 24 |
var u = Drupal.settings.basePath + 'js/' + module + '/' + op;
|
| 25 |
|
| 26 |
if (!AC.isLocked(module, op)){
|
| 27 |
d.token = Drupal.settings.ac.token;
|
| 28 |
$.post(u, d, function(response){
|
| 29 |
c(response);
|
| 30 |
}, 'json');
|
| 31 |
}
|
| 32 |
};
|
| 33 |
|
| 34 |
/**
|
| 35 |
* Validate response.
|
| 36 |
*
|
| 37 |
* @param object response
|
| 38 |
*
|
| 39 |
* @return bool
|
| 40 |
*/
|
| 41 |
AC.checkResponse = function(response) {
|
| 42 |
var s = false;
|
| 43 |
|
| 44 |
// Check for lock
|
| 45 |
if (response.lock){
|
| 46 |
AC.setLock(response._module, response._op, response.lock, response.lock_message, response.message_wrapper, response.message_class, response.message_position);
|
| 47 |
}
|
| 48 |
|
| 49 |
// Check debugging js
|
| 50 |
if (response.debug) {
|
| 51 |
eval(response.debug);
|
| 52 |
}
|
| 53 |
// Error
|
| 54 |
else if (!response.status){
|
| 55 |
AC.setMessage(response.message_wrapper, response.message, 'error', response.message_duration, response.message_class, response.message_position);
|
| 56 |
}
|
| 57 |
// Warning
|
| 58 |
else if (response.status == 2){
|
| 59 |
if (response.message.length){
|
| 60 |
AC.setMessage(response.message_wrapper, response.message, 'warning', response.message_duration, response.message_class, response.message_position);
|
| 61 |
}
|
| 62 |
s = true;
|
| 63 |
}
|
| 64 |
// Success
|
| 65 |
else {
|
| 66 |
if (response.message.length){
|
| 67 |
AC.setMessage(response.message_wrapper, response.message, 'status', response.message_duration, response.message_class, response.message_position);
|
| 68 |
}
|
| 69 |
s = true;
|
| 70 |
}
|
| 71 |
|
| 72 |
return s;
|
| 73 |
};
|
| 74 |
|
| 75 |
/**
|
| 76 |
* Set message.
|
| 77 |
*
|
| 78 |
* @param string wrapper
|
| 79 |
*
|
| 80 |
* @param string message
|
| 81 |
*
|
| 82 |
* @param string type
|
| 83 |
* (optional) Defaults to 'status'.
|
| 84 |
*
|
| 85 |
* @param int duration
|
| 86 |
* (optional) Message duration. Defaults to 3000;
|
| 87 |
*
|
| 88 |
* @param string classes
|
| 89 |
* (optional) Additional classes for this message.
|
| 90 |
*
|
| 91 |
* @param string position
|
| 92 |
* (optional) This position string is evaluted as JavaScript allowing
|
| 93 |
* you to optionally traverse and alter where the messages should be
|
| 94 |
* displayed relative to the wrapper. Ex: '.parent().after(message)'
|
| 95 |
*/
|
| 96 |
AC.setMessage = function(wrapper, message, type, duration, classes, position) {
|
| 97 |
var d = duration == null ? 3000 : duration;
|
| 98 |
var t = type || 'status';
|
| 99 |
var c = classes || '';
|
| 100 |
var p = position || null;
|
| 101 |
message = '<div class="messages ac-message ac-message-' + t + ' ' + t + ' ' + c + '">' + message + '</div>';
|
| 102 |
|
| 103 |
if (p){
|
| 104 |
eval('$(\'' + wrapper + '\')' + p + ';');
|
| 105 |
}
|
| 106 |
else {
|
| 107 |
$(wrapper).html(message);
|
| 108 |
}
|
| 109 |
|
| 110 |
if (d){
|
| 111 |
setTimeout('AC.clearMessages()', d);
|
| 112 |
}
|
| 113 |
};
|
| 114 |
|
| 115 |
/**
|
| 116 |
* Lock a request.
|
| 117 |
*
|
| 118 |
* @param string module
|
| 119 |
*
|
| 120 |
* @param string op
|
| 121 |
*
|
| 122 |
* @param int duration
|
| 123 |
* Duration in seconds to lock this request.
|
| 124 |
*
|
| 125 |
* @param string message
|
| 126 |
* (optional) Lock message.
|
| 127 |
*
|
| 128 |
* @param string classes
|
| 129 |
* (optional) Additional classes for this message.
|
| 130 |
*
|
| 131 |
* @param string position
|
| 132 |
* (optional) This position string is evaluted as JavaScript allowing
|
| 133 |
* you to optionally traverse and alter where the messages should be
|
| 134 |
* displayed relative to the wrapper. Ex: '.parent().after(message)'
|
| 135 |
*/
|
| 136 |
AC.setLock = function(module, op, duration, message, wrapper, classes, position) {
|
| 137 |
// @todo: accept wrapper and duration
|
| 138 |
var d = duration * 1000;
|
| 139 |
var m = message || '';
|
| 140 |
var w = wrapper || '#ac-messages';
|
| 141 |
var date = new Date();
|
| 142 |
AC.locks[module] = {};
|
| 143 |
AC.locks[module][op] = {
|
| 144 |
duration: date.getTime() + d,
|
| 145 |
message: m,
|
| 146 |
wrapper: w,
|
| 147 |
classes: classes,
|
| 148 |
position: position
|
| 149 |
};
|
| 150 |
};
|
| 151 |
|
| 152 |
/**
|
| 153 |
* Check a request lock.
|
| 154 |
*
|
| 155 |
* @param string module
|
| 156 |
*
|
| 157 |
* @param string op
|
| 158 |
*
|
| 159 |
* @return bool
|
| 160 |
* Weither or not the request is locked.
|
| 161 |
*/
|
| 162 |
AC.isLocked = function(module, op) {
|
| 163 |
if (AC.locks[module] && AC.locks[module][op]){
|
| 164 |
var date = new Date();
|
| 165 |
if (AC.locks[module][op].duration <= date.getTime()){
|
| 166 |
delete AC.locks[module][op];
|
| 167 |
}
|
| 168 |
else {
|
| 169 |
// @todo: Display lock_message using message_wrapper and message_duration, display as notification with own class
|
| 170 |
AC.setMessage(AC.locks[module][op].wrapper, AC.locks[module][op].message, 'warning', 3000, 'ac-lock ' + AC.locks[module][op].classes, AC.locks[module][op].position);
|
| 171 |
return true;
|
| 172 |
}
|
| 173 |
}
|
| 174 |
|
| 175 |
return false;
|
| 176 |
};
|
| 177 |
|
| 178 |
/**
|
| 179 |
* Clear messages set by AC.
|
| 180 |
*/
|
| 181 |
AC.clearMessages = function() {
|
| 182 |
$('.ac-message').fadeOut(600);
|
| 183 |
};
|