| 1 |
// $Id: asset_wizard.js,v 1.2 2008/01/20 23:28:03 rz Exp $
|
| 2 |
|
| 3 |
/**
|
| 4 |
* the following are jslint (http://www.jslint.com) options
|
| 5 |
* the syntax is very specific, especially there is no space
|
| 6 |
* after the first asterisk
|
| 7 |
*
|
| 8 |
* the first allows common browser globals like window and document
|
| 9 |
* the second says that Drupal and $ are valid global variables
|
| 10 |
*
|
| 11 |
* Last successful jslint verification: 2007-12-20
|
| 12 |
*/
|
| 13 |
/*jslint browser: true */
|
| 14 |
/*extern Drupal, $, tb_init, tb_position, tb_remove */
|
| 15 |
|
| 16 |
/**
|
| 17 |
* Namespace everything within the Drupal.assetWizard
|
| 18 |
*/
|
| 19 |
Drupal.assetWizard = {
|
| 20 |
// reference to the input/textarea field being used
|
| 21 |
input: null,
|
| 22 |
// reference to the parent document object
|
| 23 |
parent: null,
|
| 24 |
// object representation of querystring
|
| 25 |
args: null,
|
| 26 |
// optional callback on insertion of asset wizard value
|
| 27 |
onInsert: null,
|
| 28 |
// optional callback on close of wizard
|
| 29 |
onClose: null,
|
| 30 |
// history array
|
| 31 |
history: []
|
| 32 |
};
|
| 33 |
|
| 34 |
/**
|
| 35 |
* Initialize the asset wizard. hook up some event callbacks and initialize
|
| 36 |
* some variables to be used later.
|
| 37 |
*/
|
| 38 |
Drupal.assetWizard.initialize = function(){
|
| 39 |
$('.asset-wizard-start').click(
|
| 40 |
function(){
|
| 41 |
var id = this.id.replace('asset-wizard-', '#');
|
| 42 |
Drupal.assetWizard.input = $(id)[0];
|
| 43 |
|
| 44 |
$preview = $(id).parent().find('.asset-textfield-preview a');
|
| 45 |
|
| 46 |
if($preview.size()){
|
| 47 |
Drupal.assetWizard.preview = $preview;
|
| 48 |
Drupal.assetWizard.afterInsert = Drupal.assetWizard.textfieldPreview;
|
| 49 |
}
|
| 50 |
}
|
| 51 |
);
|
| 52 |
|
| 53 |
$('.asset-wizard-start').each(
|
| 54 |
function(){
|
| 55 |
var id = this.id.replace('asset-wizard-', '#');
|
| 56 |
$(id).focus(
|
| 57 |
function(){
|
| 58 |
Drupal.assetWizard.textareaInterval = window.setInterval(Drupal.assetWizard.scanTextarea, 1000, this);
|
| 59 |
}
|
| 60 |
).blur(
|
| 61 |
function(){
|
| 62 |
clearInterval(Drupal.assetWizard.textareaInterval);
|
| 63 |
}
|
| 64 |
);
|
| 65 |
// we could make this a hidden input in the form code, but just hiding it here
|
| 66 |
// allows non-javascript users at least a little functionality.
|
| 67 |
$(id).filter('.form-text').hide();
|
| 68 |
}
|
| 69 |
);
|
| 70 |
};
|
| 71 |
|
| 72 |
/**
|
| 73 |
* Scan a textarea to look for selected macros. If found update the associated
|
| 74 |
* asset wizard link's href to point to the options form.
|
| 75 |
*
|
| 76 |
* @param {Object} textarea
|
| 77 |
* the textarea to process
|
| 78 |
*/
|
| 79 |
Drupal.assetWizard.scanTextarea = function(textarea){
|
| 80 |
var text = '';
|
| 81 |
/* IE */
|
| 82 |
if (window.selection) {
|
| 83 |
var cursor = window.selection.createRange();
|
| 84 |
text = cursor.text;
|
| 85 |
}
|
| 86 |
/* Gecko-based engines: Mozilla, Camino, Firefox, Netscape */
|
| 87 |
else if (textarea.selectionStart || textarea.selectionStart == "0") {
|
| 88 |
text = textarea.value.substring(textarea.selectionStart, textarea.selectionEnd);
|
| 89 |
}
|
| 90 |
/* Worst case scenario: browsers that don't know about cursor position, Safari, OmniWeb, Konqueror */
|
| 91 |
else {
|
| 92 |
text = '';
|
| 93 |
}
|
| 94 |
|
| 95 |
// reference to the link
|
| 96 |
var link = $('#asset-wizard-' + textarea.id);
|
| 97 |
// grab a a copy of link href and reset it to asset/wizard
|
| 98 |
var href = link.attr('href').replace(/asset\/wizard[^?]*/, 'asset/wizard');
|
| 99 |
// look for a macro in the selected text
|
| 100 |
var macros = Drupal.assetWizard.getMacros(text);
|
| 101 |
if(macros[0]){
|
| 102 |
var macro = macros[0];
|
| 103 |
// parse the macro into attributes
|
| 104 |
var attr = Drupal.assetWizard.parseMacro(macro);
|
| 105 |
// if the macro has an ID and a format, update the href
|
| 106 |
if(attr.aid && attr.format){
|
| 107 |
href = href.replace(/asset\/wizard[^?]*/, 'asset/wizard/' + attr.aid + '/' + attr.format + '/' + macro);
|
| 108 |
}
|
| 109 |
}
|
| 110 |
// update the link
|
| 111 |
link.attr('href', href);
|
| 112 |
};
|
| 113 |
|
| 114 |
/**
|
| 115 |
* Parse a macro into an array of attributes
|
| 116 |
*
|
| 117 |
* @param {String} macro
|
| 118 |
* @return {Array}
|
| 119 |
* An array of key=>value attributes
|
| 120 |
*/
|
| 121 |
Drupal.assetWizard.parseMacro = function(macro){
|
| 122 |
if(!macro){
|
| 123 |
return {};
|
| 124 |
}
|
| 125 |
macro = macro.replace(/^\[asset\|/, '').replace(/\]$/, '');
|
| 126 |
var pairs = macro.split('|');
|
| 127 |
var attr = {};
|
| 128 |
for(var i = 0; i < pairs.length; i++) {
|
| 129 |
var parts = pairs[i].split('=', 2);
|
| 130 |
// only deal with key=value pairs
|
| 131 |
if (parts.length != 2){
|
| 132 |
continue;
|
| 133 |
}
|
| 134 |
attr[parts[0]] = unescape(parts[1]);
|
| 135 |
}
|
| 136 |
return attr; // Return the Object
|
| 137 |
};
|
| 138 |
|
| 139 |
/**
|
| 140 |
* Build a macro from an array of attributes
|
| 141 |
*
|
| 142 |
* @param {Array} attributes
|
| 143 |
* @return {String}
|
| 144 |
* A string representation of the macro
|
| 145 |
*/
|
| 146 |
Drupal.assetWizard.buildMacro = function(attributes){
|
| 147 |
var pairs = [];
|
| 148 |
for(var key in attributes){
|
| 149 |
if(attributes[key]){
|
| 150 |
pairs.push(key +'='+ attributes[key]);
|
| 151 |
}
|
| 152 |
}
|
| 153 |
return '[asset|' + pairs.join('|') + ']';
|
| 154 |
};
|
| 155 |
|
| 156 |
/**
|
| 157 |
* Search a string for asset macros
|
| 158 |
*
|
| 159 |
* @param {String} text
|
| 160 |
* @return {Array}
|
| 161 |
* An array of macros on success, false on failure
|
| 162 |
*/
|
| 163 |
Drupal.assetWizard.getMacros = function(text){
|
| 164 |
var regex = /\[asset[^\[\]]+\]/g;
|
| 165 |
var matches = text.match(regex);
|
| 166 |
if(!matches || matches.length < 1){
|
| 167 |
return false;
|
| 168 |
}
|
| 169 |
return matches;
|
| 170 |
};
|
| 171 |
|
| 172 |
/**
|
| 173 |
* This function gets called when the wizard is finished.
|
| 174 |
* Any alternative uses of the
|
| 175 |
*
|
| 176 |
* @param assetValue - the value returned from the wizard
|
| 177 |
*/
|
| 178 |
Drupal.assetWizard.insert = function(assetValue){
|
| 179 |
var wizard = Drupal.assetWizard;
|
| 180 |
var input = Drupal.assetWizard.input;
|
| 181 |
var onInsert = Drupal.assetWizard.onInsert;
|
| 182 |
|
| 183 |
if(onInsert && typeof(onInsert) == 'function'){
|
| 184 |
onInsert(assetValue);
|
| 185 |
Drupal.assetWizard.onInsert = null;
|
| 186 |
}
|
| 187 |
else{
|
| 188 |
// in a textarea we want to overwrite the current selection,
|
| 189 |
// insert at the cursor location or append
|
| 190 |
if($(input).is('textarea')){
|
| 191 |
wizard.textareaUpdate(input, assetValue);
|
| 192 |
}
|
| 193 |
// for other inputs, simply replace the value
|
| 194 |
else{
|
| 195 |
input.value = assetValue;
|
| 196 |
}
|
| 197 |
}
|
| 198 |
|
| 199 |
// allow for an afterInsert callback
|
| 200 |
if(wizard.afterInsert && typeof(wizard.afterInsert) == 'function'){
|
| 201 |
wizard.afterInsert(assetValue);
|
| 202 |
wizard.afterInsert = null;
|
| 203 |
}
|
| 204 |
|
| 205 |
// fire 'notify' event
|
| 206 |
$.event.trigger('assetWizardInsert', [this, assetValue]);
|
| 207 |
|
| 208 |
// Close the dialog
|
| 209 |
wizard.close();
|
| 210 |
return false;
|
| 211 |
};
|
| 212 |
|
| 213 |
/**
|
| 214 |
* Helper function to update a textarea by overwriting a selection,
|
| 215 |
* inserting at cursor, or simply appending to the current value
|
| 216 |
*/
|
| 217 |
Drupal.assetWizard.textareaUpdate = function(textarea, value){
|
| 218 |
/* IE */
|
| 219 |
if (window.selection) {
|
| 220 |
textarea.focus();
|
| 221 |
var cursor = window.selection.createRange();
|
| 222 |
cursor.text = value;
|
| 223 |
}
|
| 224 |
/* Gecko-based engines: Mozilla, Camino, Firefox, Netscape */
|
| 225 |
else if (textarea.selectionStart || textarea.selectionStart == "0") {
|
| 226 |
var startPos = textarea.selectionStart;
|
| 227 |
var endPos = textarea.selectionEnd;
|
| 228 |
var body = textarea.value;
|
| 229 |
textarea.value = body.substring(0, startPos) + value + body.substring(endPos, body.length);
|
| 230 |
}
|
| 231 |
/* Worst case scenario: browsers that don't know about cursor position, Safari, OmniWeb, Konqueror */
|
| 232 |
else {
|
| 233 |
textarea.value += value;
|
| 234 |
}
|
| 235 |
};
|
| 236 |
|
| 237 |
Drupal.assetWizard.textfieldPreview = function(aid){
|
| 238 |
// Drupal.assetWizard.preview.load(Drupal.settings.assetWizard.assetUrl + '/' + aid + '/icon/ajax');
|
| 239 |
Drupal.assetWizard.preview = null;
|
| 240 |
};
|
| 241 |
|
| 242 |
/**
|
| 243 |
* Close the wizard. Called when the cancel button is clicked and after insert
|
| 244 |
*/
|
| 245 |
Drupal.assetWizard.close = function(){
|
| 246 |
tb_remove();
|
| 247 |
};
|
| 248 |
|
| 249 |
function insertToEditor(value){
|
| 250 |
Drupal.assetWizard.insert(value);
|
| 251 |
}
|
| 252 |
|
| 253 |
$(document).ready(Drupal.assetWizard.initialize);
|
| 254 |
|
| 255 |
/**
|
| 256 |
* ajax form submit callbacks
|
| 257 |
*/
|
| 258 |
Drupal.assetWizard.beforeSubmit = function(formData, jqForm, options){
|
| 259 |
// console.log($.param(formData));
|
| 260 |
$('.wizard-content').addClass('wizard-loading');
|
| 261 |
};
|
| 262 |
|
| 263 |
// on form submit inside thickbox
|
| 264 |
Drupal.assetWizard.formSubmit = function(data){
|
| 265 |
$('#TB_ajaxContent').html(data);
|
| 266 |
// copied from line 226 of jquery.thickbox.js
|
| 267 |
tb_position();
|
| 268 |
$("#TB_load").remove();
|
| 269 |
tb_init("#TB_ajaxContent a.thickbox");
|
| 270 |
$("#TB_window").css({display:"block"});
|
| 271 |
$('.wizard-content').removeClass('wizard-loading');
|
| 272 |
};
|
| 273 |
|
| 274 |
/**
|
| 275 |
* I need to know when tb get's re-inited so I can also run some of my own init code.
|
| 276 |
* I am hijacking the tb_init function, running my own code and then running the
|
| 277 |
* original tb_init.
|
| 278 |
*/
|
| 279 |
// duplicate original tb_init
|
| 280 |
Drupal.assetWizard.tb_init_original = tb_init;
|
| 281 |
|
| 282 |
// new tb_init function - make sure to call reference to original function
|
| 283 |
Drupal.assetWizard.tb_init = function(dom_chunk){
|
| 284 |
var options = {
|
| 285 |
beforeSubmit: Drupal.assetWizard.beforeSubmit,
|
| 286 |
success: Drupal.assetWizard.formSubmit
|
| 287 |
};
|
| 288 |
// if($("#TB_window").size()){
|
| 289 |
// $("#TB_window form").ajaxForm(options);
|
| 290 |
// }
|
| 291 |
|
| 292 |
// move buttons out of the content pane
|
| 293 |
$('#asset-wizard').append($('.wizard-buttons'));
|
| 294 |
// hook the moved buttons to their form
|
| 295 |
$('.wizard-buttons input.form-submit').click(
|
| 296 |
function(){
|
| 297 |
$("#asset-wizard form").ajaxSubmit(options);
|
| 298 |
}
|
| 299 |
);
|
| 300 |
// also make sure to hook up original form submit enter key submits
|
| 301 |
$("#TB_window form").ajaxForm(options);
|
| 302 |
// add a back button
|
| 303 |
|
| 304 |
if (Drupal.assetWizard.history.length > 0) {
|
| 305 |
$('<a>Back</a>').attr(
|
| 306 |
{
|
| 307 |
'src': Drupal.assetWizard.history.pop,
|
| 308 |
'class': 'wizard-back'
|
| 309 |
}
|
| 310 |
).prependTo($('.wizard-buttons')[0]);
|
| 311 |
}
|
| 312 |
|
| 313 |
// create the close button
|
| 314 |
$('#asset-wizard').append(
|
| 315 |
$('<a>x</a>').attr(
|
| 316 |
{
|
| 317 |
'href': '#',
|
| 318 |
'class': 'wizard-close'
|
| 319 |
}
|
| 320 |
).click(
|
| 321 |
function(){
|
| 322 |
Drupal.assetWizard.close();
|
| 323 |
return false;
|
| 324 |
}
|
| 325 |
)
|
| 326 |
);
|
| 327 |
|
| 328 |
$('#asset-wizard .help').each(
|
| 329 |
function(){
|
| 330 |
$(this).wrap('<div class="help-wrapper"></div>');
|
| 331 |
|
| 332 |
}
|
| 333 |
);
|
| 334 |
|
| 335 |
$(dom_chunk).click(
|
| 336 |
function(){
|
| 337 |
Drupal.assetWizard.history.push(this.href);
|
| 338 |
}
|
| 339 |
);
|
| 340 |
Drupal.assetWizard.tb_init_original(dom_chunk);
|
| 341 |
};
|
| 342 |
|
| 343 |
// overwrite tb_init with new function
|
| 344 |
tb_init = Drupal.assetWizard.tb_init;
|