2011-07-26 16:25:04 +01:00
|
|
|
var phpbb = {};
|
2012-06-21 19:16:05 +01:00
|
|
|
phpbb.alertTime = 100;
|
2011-07-26 16:25:04 +01:00
|
|
|
|
2011-08-24 11:44:28 +01:00
|
|
|
(function($) { // Avoid conflicts with other libraries
|
2011-07-26 11:46:49 +01:00
|
|
|
|
2012-02-08 19:03:30 +01:00
|
|
|
"use strict";
|
|
|
|
|
2011-09-28 18:34:25 +01:00
|
|
|
// define a couple constants for keydown functions.
|
2012-02-08 18:40:08 +01:00
|
|
|
var keymap = {
|
|
|
|
ENTER: 13,
|
|
|
|
ESC: 27
|
|
|
|
};
|
2011-09-24 17:41:58 +01:00
|
|
|
|
2011-11-18 17:31:10 +00:00
|
|
|
var dark = $('#darkenwrapper');
|
2012-06-21 19:16:05 +01:00
|
|
|
var loadingAlert = $('#loadingalert');
|
2013-01-02 16:44:05 +01:00
|
|
|
var phpbbAlertTimer = null;
|
2011-07-25 20:42:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2012-06-21 19:16:05 +01:00
|
|
|
* Display a loading screen
|
2011-08-24 11:44:28 +01:00
|
|
|
*
|
2012-06-21 19:16:05 +01:00
|
|
|
* @returns object Returns loadingAlert.
|
2011-07-25 20:42:29 +01:00
|
|
|
*/
|
2012-06-21 19:16:05 +01:00
|
|
|
phpbb.loadingAlert = function() {
|
|
|
|
if (dark.is(':visible')) {
|
|
|
|
loadingAlert.fadeIn(phpbb.alertTime);
|
|
|
|
} else {
|
|
|
|
loadingAlert.show();
|
|
|
|
dark.fadeIn(phpbb.alertTime, function() {
|
2011-10-15 18:07:06 +01:00
|
|
|
// Wait five seconds and display an error if nothing has been returned by then.
|
2012-12-19 16:41:25 +01:00
|
|
|
phpbbAlertTimer = setTimeout(function() {
|
2012-06-21 19:16:05 +01:00
|
|
|
if (loadingAlert.is(':visible')) {
|
2012-02-08 18:42:21 +01:00
|
|
|
phpbb.alert($('#phpbb_alert').attr('data-l-err'), $('#phpbb_alert').attr('data-l-timeout-processing-req'));
|
2011-08-19 17:39:35 +01:00
|
|
|
}
|
|
|
|
}, 5000);
|
|
|
|
});
|
2011-07-25 20:42:29 +01:00
|
|
|
}
|
2011-08-19 17:39:35 +01:00
|
|
|
|
2012-06-21 19:16:05 +01:00
|
|
|
return loadingAlert;
|
|
|
|
};
|
2011-07-25 20:42:29 +01:00
|
|
|
|
2012-12-19 16:41:25 +01:00
|
|
|
/**
|
|
|
|
* Clear loading alert timeout
|
|
|
|
*/
|
|
|
|
phpbb.clearLoadingTimeout = function() {
|
2013-01-04 22:46:12 +01:00
|
|
|
if (phpbbAlertTimer !== null) {
|
2012-12-19 16:41:25 +01:00
|
|
|
clearTimeout(phpbbAlertTimer);
|
2013-01-02 16:44:05 +01:00
|
|
|
phpbbAlertTimer = null;
|
2012-12-19 16:41:25 +01:00
|
|
|
}
|
2013-01-04 22:46:12 +01:00
|
|
|
};
|
2012-12-19 16:41:25 +01:00
|
|
|
|
2011-07-14 13:33:42 +01:00
|
|
|
/**
|
2011-07-14 17:49:17 +01:00
|
|
|
* Display a simple alert similar to JSs native alert().
|
2011-07-14 13:33:42 +01:00
|
|
|
*
|
2011-10-22 16:09:38 +01:00
|
|
|
* You can only call one alert or confirm box at any one time.
|
|
|
|
*
|
|
|
|
* @param string title Title of the message, eg "Information" (HTML).
|
|
|
|
* @param string msg Message to display (HTML).
|
2011-07-26 12:13:09 +01:00
|
|
|
* @param bool fadedark Remove the dark background when done? Defaults
|
2013-03-26 13:13:33 +01:00
|
|
|
* to yes.
|
2011-07-14 14:41:24 +01:00
|
|
|
*
|
2011-08-24 11:44:28 +01:00
|
|
|
* @returns object Returns the div created.
|
2011-07-14 13:33:42 +01:00
|
|
|
*/
|
2011-07-25 20:42:29 +01:00
|
|
|
phpbb.alert = function(title, msg, fadedark) {
|
2011-09-25 18:12:34 +01:00
|
|
|
var div = $('#phpbb_alert');
|
2011-10-15 16:21:40 +01:00
|
|
|
div.find('.alert_title').html(title);
|
|
|
|
div.find('.alert_text').html(msg);
|
2011-07-14 13:33:42 +01:00
|
|
|
|
2011-09-24 16:42:22 +01:00
|
|
|
div.bind('click', function(e) {
|
2011-07-25 20:42:29 +01:00
|
|
|
e.stopPropagation();
|
|
|
|
});
|
2011-09-24 16:42:22 +01:00
|
|
|
dark.one('click', function(e) {
|
2012-02-08 19:02:16 +01:00
|
|
|
var fade;
|
|
|
|
|
2011-10-22 17:07:00 +01:00
|
|
|
div.find('.alert_close').unbind('click');
|
2012-02-08 19:02:16 +01:00
|
|
|
fade = (typeof fadedark !== 'undefined' && !fadedark) ? div : dark;
|
2012-06-21 19:16:05 +01:00
|
|
|
fade.fadeOut(phpbb.alertTime, function() {
|
2011-09-25 16:02:26 +01:00
|
|
|
div.hide();
|
2011-07-26 12:13:09 +01:00
|
|
|
});
|
2012-02-08 18:58:23 +01:00
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2011-07-14 13:33:42 +01:00
|
|
|
});
|
2011-09-27 19:32:06 +01:00
|
|
|
|
2011-08-18 19:02:18 +01:00
|
|
|
$(document).bind('keydown', function(e) {
|
2012-02-08 18:40:08 +01:00
|
|
|
if (e.keyCode === keymap.ENTER || e.keyCode === keymap.ESC) {
|
2011-09-24 16:42:22 +01:00
|
|
|
dark.trigger('click');
|
2012-02-08 18:58:23 +01:00
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2011-08-18 19:02:18 +01:00
|
|
|
}
|
|
|
|
});
|
2011-09-27 19:32:06 +01:00
|
|
|
|
2012-02-08 19:38:21 +01:00
|
|
|
div.find('.alert_close').one('click', function(e) {
|
2011-09-25 16:49:43 +01:00
|
|
|
dark.trigger('click');
|
2012-02-08 19:38:21 +01:00
|
|
|
|
|
|
|
e.preventDefault();
|
2011-09-25 16:49:43 +01:00
|
|
|
});
|
2011-07-14 13:33:42 +01:00
|
|
|
|
2012-06-21 19:16:05 +01:00
|
|
|
if (loadingAlert.is(':visible')) {
|
|
|
|
loadingAlert.fadeOut(phpbb.alertTime, function() {
|
2011-09-24 16:42:22 +01:00
|
|
|
dark.append(div);
|
2012-06-21 19:16:05 +01:00
|
|
|
div.fadeIn(phpbb.alertTime);
|
2011-07-25 20:42:29 +01:00
|
|
|
});
|
2012-06-21 19:16:05 +01:00
|
|
|
} else if (dark.is(':visible')) {
|
2011-09-24 16:42:22 +01:00
|
|
|
dark.append(div);
|
2012-06-21 19:16:05 +01:00
|
|
|
div.fadeIn(phpbb.alertTime);
|
|
|
|
} else {
|
2011-09-24 16:42:22 +01:00
|
|
|
dark.append(div);
|
2011-07-25 20:42:29 +01:00
|
|
|
div.show();
|
2012-06-21 19:16:05 +01:00
|
|
|
dark.fadeIn(phpbb.alertTime);
|
2011-07-25 20:42:29 +01:00
|
|
|
}
|
2011-09-27 19:32:06 +01:00
|
|
|
|
2011-07-14 14:41:24 +01:00
|
|
|
return div;
|
2012-06-21 19:16:05 +01:00
|
|
|
};
|
2011-07-14 13:33:42 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a simple yes / no box to the user.
|
|
|
|
*
|
2011-10-22 16:09:38 +01:00
|
|
|
* You can only call one alert or confirm box at any one time.
|
|
|
|
*
|
2011-10-15 18:05:58 +01:00
|
|
|
* @param string msg Message to display (HTML).
|
2011-07-26 12:13:09 +01:00
|
|
|
* @param function callback Callback. Bool param, whether the user pressed
|
2013-01-12 19:08:34 +01:00
|
|
|
* yes or no (or whatever their language is).
|
2011-07-26 12:13:09 +01:00
|
|
|
* @param bool fadedark Remove the dark background when done? Defaults
|
2013-01-12 19:08:34 +01:00
|
|
|
* to yes.
|
2011-07-14 14:41:24 +01:00
|
|
|
*
|
2011-08-24 11:44:28 +01:00
|
|
|
* @returns object Returns the div created.
|
2011-07-14 13:33:42 +01:00
|
|
|
*/
|
2011-07-25 20:42:29 +01:00
|
|
|
phpbb.confirm = function(msg, callback, fadedark) {
|
2011-09-25 18:12:34 +01:00
|
|
|
var div = $('#phpbb_confirm');
|
2011-10-15 16:21:40 +01:00
|
|
|
div.find('.alert_text').html(msg);
|
2011-09-25 18:12:34 +01:00
|
|
|
|
|
|
|
div.bind('click', function(e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
2012-02-08 18:42:21 +01:00
|
|
|
|
2013-01-04 22:46:12 +01:00
|
|
|
var clickHandler = function(e) {
|
2013-05-08 00:08:35 +02:00
|
|
|
var res = this.name === 'confirm';
|
2011-07-26 12:13:09 +01:00
|
|
|
var fade = (typeof fadedark !== 'undefined' && !fadedark && res) ? div : dark;
|
2012-06-21 19:16:05 +01:00
|
|
|
fade.fadeOut(phpbb.alertTime, function() {
|
2011-09-25 16:02:26 +01:00
|
|
|
div.hide();
|
2011-07-26 12:13:09 +01:00
|
|
|
});
|
2013-01-04 22:46:12 +01:00
|
|
|
div.find('input[type="button"]').unbind('click', clickHandler);
|
2011-07-26 12:13:09 +01:00
|
|
|
callback(res);
|
2012-02-08 18:58:23 +01:00
|
|
|
|
|
|
|
if (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
}
|
2011-11-17 17:09:09 +00:00
|
|
|
};
|
2013-01-04 22:46:12 +01:00
|
|
|
div.find('input[type="button"]').one('click', clickHandler);
|
2011-09-25 16:49:43 +01:00
|
|
|
|
|
|
|
dark.one('click', function(e) {
|
2011-10-22 17:07:00 +01:00
|
|
|
div.find('.alert_close').unbind('click');
|
2012-06-21 19:16:05 +01:00
|
|
|
dark.fadeOut(phpbb.alertTime, function() {
|
2011-09-25 16:49:43 +01:00
|
|
|
div.hide();
|
|
|
|
});
|
|
|
|
callback(false);
|
2012-02-08 18:58:23 +01:00
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2011-09-25 16:49:43 +01:00
|
|
|
});
|
2011-09-27 19:32:06 +01:00
|
|
|
|
2011-08-18 19:02:18 +01:00
|
|
|
$(document).bind('keydown', function(e) {
|
2012-02-08 18:40:08 +01:00
|
|
|
if (e.keyCode === keymap.ENTER) {
|
2013-05-08 00:08:35 +02:00
|
|
|
$('input[name="confirm"]').trigger('click');
|
2012-02-08 18:58:23 +01:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2012-02-08 18:40:08 +01:00
|
|
|
} else if (e.keyCode === keymap.ESC) {
|
2013-05-08 00:08:35 +02:00
|
|
|
$('input[name="cancel"]').trigger('click');
|
2012-02-08 18:58:23 +01:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2011-08-18 19:02:18 +01:00
|
|
|
}
|
|
|
|
});
|
2011-09-27 19:32:06 +01:00
|
|
|
|
2012-02-08 19:38:21 +01:00
|
|
|
div.find('.alert_close').one('click', function(e) {
|
2011-10-22 17:07:00 +01:00
|
|
|
var fade = (typeof fadedark !== 'undefined' && fadedark) ? div : dark;
|
2012-06-21 19:16:05 +01:00
|
|
|
fade.fadeOut(phpbb.alertTime, function() {
|
2011-10-22 17:07:00 +01:00
|
|
|
div.hide();
|
|
|
|
});
|
|
|
|
callback(false);
|
2012-02-08 19:38:21 +01:00
|
|
|
|
|
|
|
e.preventDefault();
|
2011-09-25 16:49:43 +01:00
|
|
|
});
|
2011-09-27 19:32:06 +01:00
|
|
|
|
2012-06-21 19:16:05 +01:00
|
|
|
if (loadingAlert.is(':visible')) {
|
|
|
|
loadingAlert.fadeOut(phpbb.alertTime, function() {
|
2011-09-24 16:42:22 +01:00
|
|
|
dark.append(div);
|
2012-06-21 19:16:05 +01:00
|
|
|
div.fadeIn(phpbb.alertTime);
|
2011-07-25 20:42:29 +01:00
|
|
|
});
|
2012-06-21 19:16:05 +01:00
|
|
|
} else if (dark.is(':visible')) {
|
2011-09-24 16:42:22 +01:00
|
|
|
dark.append(div);
|
2012-06-21 19:16:05 +01:00
|
|
|
div.fadeIn(phpbb.alertTime);
|
|
|
|
} else {
|
2011-09-24 16:42:22 +01:00
|
|
|
dark.append(div);
|
2011-07-25 20:42:29 +01:00
|
|
|
div.show();
|
2012-06-21 19:16:05 +01:00
|
|
|
dark.fadeIn(phpbb.alertTime);
|
2011-07-25 20:42:29 +01:00
|
|
|
}
|
2011-09-27 19:32:06 +01:00
|
|
|
|
2011-07-14 14:41:24 +01:00
|
|
|
return div;
|
2012-06-21 19:16:05 +01:00
|
|
|
};
|
2011-07-14 13:33:42 +01:00
|
|
|
|
2011-08-24 11:31:17 +01:00
|
|
|
/**
|
|
|
|
* Turn a querystring into an array.
|
|
|
|
*
|
|
|
|
* @argument string string The querystring to parse.
|
2011-10-22 16:50:33 +01:00
|
|
|
* @returns object The object created.
|
2011-08-24 11:31:17 +01:00
|
|
|
*/
|
2013-01-04 22:46:12 +01:00
|
|
|
phpbb.parseQuerystring = function(string) {
|
2011-10-22 16:13:27 +01:00
|
|
|
var params = {}, i, split;
|
2011-09-27 19:32:06 +01:00
|
|
|
|
2011-08-24 11:31:17 +01:00
|
|
|
string = string.split('&');
|
2012-06-21 19:16:05 +01:00
|
|
|
for (i = 0; i < string.length; i++) {
|
2011-10-22 16:12:08 +01:00
|
|
|
split = string[i].split('=');
|
2011-10-22 16:13:27 +01:00
|
|
|
params[split[0]] = decodeURIComponent(split[1]);
|
2011-08-24 11:31:17 +01:00
|
|
|
}
|
2011-10-22 16:13:27 +01:00
|
|
|
return params;
|
2012-06-21 19:16:05 +01:00
|
|
|
};
|
2011-08-24 11:31:17 +01:00
|
|
|
|
2011-07-14 14:41:24 +01:00
|
|
|
|
2011-07-14 14:57:45 +01:00
|
|
|
/**
|
|
|
|
* Makes a link use AJAX instead of loading an entire page.
|
2011-07-14 17:49:17 +01:00
|
|
|
*
|
2011-10-15 17:29:34 +01:00
|
|
|
* This function will work for links (both standard links and links which
|
|
|
|
* invoke confirm_box) and forms. It will be called automatically for links
|
|
|
|
* and forms with the data-ajax attribute set, and will call the necessary
|
|
|
|
* callback.
|
|
|
|
*
|
|
|
|
* For more info, view the following page on the phpBB wiki:
|
|
|
|
* http://wiki.phpbb.com/JavaScript_Function.phpbb.ajaxify
|
|
|
|
*
|
2012-02-08 19:46:36 +01:00
|
|
|
* @param object options Options.
|
2011-07-14 17:49:17 +01:00
|
|
|
* @param bool/function refresh If we are sent back a refresh, should it be
|
2013-01-12 19:08:34 +01:00
|
|
|
* acted upon? This can either be true / false / a function.
|
2011-10-15 17:29:34 +01:00
|
|
|
* @param function callback Callback to call on completion of event. Has
|
2013-01-12 19:08:34 +01:00
|
|
|
* three parameters: the element that the event was evoked from, the JSON
|
|
|
|
* that was returned and (if it is a form) the form action.
|
2011-07-14 14:57:45 +01:00
|
|
|
*/
|
2012-02-08 19:56:25 +01:00
|
|
|
phpbb.ajaxify = function(options) {
|
|
|
|
var elements = $(options.selector),
|
|
|
|
refresh = options.refresh,
|
|
|
|
callback = options.callback,
|
2012-04-11 23:29:47 +02:00
|
|
|
overlay = (typeof options.overlay !== 'undefined') ? options.overlay : true,
|
2012-06-21 19:16:05 +01:00
|
|
|
isForm = elements.is('form'),
|
|
|
|
eventName = isForm ? 'submit' : 'click';
|
2012-02-08 19:56:25 +01:00
|
|
|
|
2012-06-21 19:16:05 +01:00
|
|
|
elements.bind(eventName, function(event) {
|
2012-02-19 16:35:21 +00:00
|
|
|
var action, method, data, submit, that = this, $this = $(this);
|
2011-09-27 19:32:06 +01:00
|
|
|
|
2012-06-21 19:16:05 +01:00
|
|
|
if ($this.find('input[type="submit"][data-clicked]').attr('data-ajax') === 'false') {
|
2012-02-08 18:58:23 +01:00
|
|
|
return;
|
2011-07-25 10:59:19 +01:00
|
|
|
}
|
2011-09-27 19:32:06 +01:00
|
|
|
|
2013-01-10 10:59:20 +01:00
|
|
|
function errorHandler() {
|
|
|
|
phpbb.clearLoadingTimeout();
|
|
|
|
phpbb.alert(dark.attr('data-ajax-error-title'), dark.attr('data-ajax-error-text'));
|
|
|
|
}
|
|
|
|
|
2011-10-15 17:29:34 +01:00
|
|
|
/**
|
|
|
|
* This is a private function used to handle the callbacks, refreshes
|
2011-10-22 16:40:37 +01:00
|
|
|
* and alert. It calls the callback, refreshes the page if necessary, and
|
|
|
|
* displays an alert to the user and removes it after an amount of time.
|
|
|
|
*
|
|
|
|
* It cannot be called from outside this function, and is purely here to
|
|
|
|
* avoid repetition of code.
|
|
|
|
*
|
|
|
|
* @param object res The object sent back by the server.
|
2011-10-15 17:29:34 +01:00
|
|
|
*/
|
2012-06-21 19:16:05 +01:00
|
|
|
function returnHandler(res) {
|
2012-02-08 19:02:16 +01:00
|
|
|
var alert;
|
|
|
|
|
2012-12-19 16:41:25 +01:00
|
|
|
phpbb.clearLoadingTimeout();
|
|
|
|
|
2011-11-30 16:58:17 +00:00
|
|
|
// Is a confirmation required?
|
2012-06-21 19:16:05 +01:00
|
|
|
if (typeof res.S_CONFIRM_ACTION === 'undefined') {
|
2011-11-30 16:58:17 +00:00
|
|
|
// If a confirmation is not required, display an alert and call the
|
|
|
|
// callbacks.
|
2012-06-21 19:16:05 +01:00
|
|
|
if (typeof res.MESSAGE_TITLE !== 'undefined') {
|
2012-02-08 19:02:16 +01:00
|
|
|
alert = phpbb.alert(res.MESSAGE_TITLE, res.MESSAGE_TEXT);
|
2012-06-21 19:16:05 +01:00
|
|
|
} else {
|
|
|
|
dark.fadeOut(phpbb.alertTime);
|
2011-08-24 16:39:25 +01:00
|
|
|
}
|
2011-09-27 19:32:06 +01:00
|
|
|
|
2012-06-21 19:16:05 +01:00
|
|
|
if (typeof phpbb.ajaxCallbacks[callback] === 'function') {
|
|
|
|
phpbb.ajaxCallbacks[callback].call(that, res);
|
2011-07-17 14:57:13 +01:00
|
|
|
}
|
2011-08-24 22:29:52 +01:00
|
|
|
|
2011-11-30 16:58:17 +00:00
|
|
|
// If the server says to refresh the page, check whether the page should
|
|
|
|
// be refreshed and refresh page after specified time if required.
|
2012-06-21 19:16:05 +01:00
|
|
|
if (res.REFRESH_DATA) {
|
|
|
|
if (typeof refresh === 'function') {
|
2011-08-24 22:29:52 +01:00
|
|
|
refresh = refresh(res.REFRESH_DATA.url);
|
2012-06-21 19:16:05 +01:00
|
|
|
} else if (typeof refresh !== 'boolean') {
|
2011-08-24 22:29:52 +01:00
|
|
|
refresh = false;
|
|
|
|
}
|
2011-09-27 19:32:06 +01:00
|
|
|
|
2011-08-24 22:29:52 +01:00
|
|
|
setTimeout(function() {
|
2012-06-21 19:16:05 +01:00
|
|
|
if (refresh) {
|
2011-08-24 22:29:52 +01:00
|
|
|
window.location = res.REFRESH_DATA.url;
|
|
|
|
}
|
2011-09-27 19:32:06 +01:00
|
|
|
|
2011-11-30 16:58:17 +00:00
|
|
|
// Hide the alert even if we refresh the page, in case the user
|
|
|
|
// presses the back button.
|
2012-06-21 19:16:05 +01:00
|
|
|
dark.fadeOut(phpbb.alertTime, function() {
|
2011-09-25 16:02:26 +01:00
|
|
|
alert.hide();
|
2011-08-24 22:29:52 +01:00
|
|
|
});
|
2011-11-30 16:58:17 +00:00
|
|
|
}, res.REFRESH_DATA.time * 1000); // Server specifies time in seconds
|
2011-08-24 22:29:52 +01:00
|
|
|
}
|
2012-06-21 19:16:05 +01:00
|
|
|
} else {
|
2013-01-15 13:10:25 +01:00
|
|
|
// If confirmation is required, display a dialog to the user.
|
2012-12-18 13:31:38 +01:00
|
|
|
phpbb.confirm(res.MESSAGE_BODY, function(del) {
|
2012-06-21 19:16:05 +01:00
|
|
|
if (del) {
|
|
|
|
phpbb.loadingAlert();
|
2011-08-24 22:29:52 +01:00
|
|
|
data = $('<form>' + res.S_HIDDEN_FIELDS + '</form>').serialize();
|
2012-02-15 20:58:00 +01:00
|
|
|
$.ajax({
|
|
|
|
url: res.S_CONFIRM_ACTION,
|
|
|
|
type: 'POST',
|
|
|
|
data: data + '&confirm=' + res.YES_VALUE,
|
2012-06-21 19:16:05 +01:00
|
|
|
success: returnHandler,
|
|
|
|
error: errorHandler
|
2012-02-15 20:58:00 +01:00
|
|
|
});
|
2011-07-17 15:27:58 +01:00
|
|
|
}
|
2011-07-25 20:42:29 +01:00
|
|
|
}, false);
|
2011-07-17 15:27:58 +01:00
|
|
|
}
|
|
|
|
}
|
2011-09-27 19:32:06 +01:00
|
|
|
|
2011-11-30 16:58:17 +00:00
|
|
|
// If the element is a form, POST must be used and some extra data must
|
|
|
|
// be taken from the form.
|
2012-06-21 19:16:05 +01:00
|
|
|
var runFilter = (typeof options.filter === 'function');
|
2012-02-15 19:32:53 +01:00
|
|
|
|
2012-06-21 19:16:05 +01:00
|
|
|
if (isForm) {
|
2012-02-15 19:32:53 +01:00
|
|
|
action = $this.attr('action').replace('&', '&');
|
|
|
|
data = $this.serializeArray();
|
2012-02-15 19:38:55 +01:00
|
|
|
method = $this.attr('method') || 'GET';
|
2012-02-19 16:30:22 +00:00
|
|
|
|
2012-06-21 19:16:05 +01:00
|
|
|
if ($this.find('input[type="submit"][data-clicked]')) {
|
2012-02-19 16:35:21 +00:00
|
|
|
submit = $this.find('input[type="submit"][data-clicked]');
|
2012-02-19 16:30:22 +00:00
|
|
|
data.push({
|
|
|
|
name: submit.attr('name'),
|
|
|
|
value: submit.val()
|
|
|
|
});
|
|
|
|
}
|
2012-06-21 19:16:05 +01:00
|
|
|
} else {
|
2012-02-15 19:32:53 +01:00
|
|
|
action = this.href;
|
|
|
|
data = null;
|
|
|
|
method = 'GET';
|
|
|
|
}
|
|
|
|
|
2012-02-15 19:38:55 +01:00
|
|
|
// If filter function returns false, cancel the AJAX functionality,
|
2012-02-15 19:32:53 +01:00
|
|
|
// and return true (meaning that the HTTP request will be sent normally).
|
2012-06-21 19:16:05 +01:00
|
|
|
if (runFilter && !options.filter.call(this, data)) {
|
2012-02-19 16:35:21 +00:00
|
|
|
return;
|
2011-07-17 15:27:58 +01:00
|
|
|
}
|
2011-09-27 19:32:06 +01:00
|
|
|
|
2013-03-26 13:13:33 +01:00
|
|
|
if (overlay && (typeof $this.attr('data-overlay') === 'undefined' || $this.attr('data-overlay') === 'true')) {
|
2012-06-21 19:16:05 +01:00
|
|
|
phpbb.loadingAlert();
|
2012-04-11 23:29:47 +02:00
|
|
|
}
|
2012-02-15 19:32:53 +01:00
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
url: action,
|
|
|
|
type: method,
|
|
|
|
data: data,
|
2012-06-21 19:16:05 +01:00
|
|
|
success: returnHandler,
|
|
|
|
error: errorHandler
|
2012-02-15 19:32:53 +01:00
|
|
|
});
|
|
|
|
|
2012-02-19 16:35:21 +00:00
|
|
|
event.preventDefault();
|
2011-07-17 14:57:13 +01:00
|
|
|
});
|
2012-02-15 19:32:53 +01:00
|
|
|
|
2012-06-21 19:16:05 +01:00
|
|
|
if (isForm) {
|
2012-02-19 16:30:22 +00:00
|
|
|
elements.find('input:submit').click(function () {
|
|
|
|
var $this = $(this);
|
|
|
|
|
|
|
|
$this.siblings('[data-clicked]').removeAttr('data-clicked');
|
|
|
|
$this.attr('data-clicked', 'true');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:58:37 +01:00
|
|
|
return this;
|
2012-06-21 19:16:05 +01:00
|
|
|
};
|
2011-07-17 14:57:13 +01:00
|
|
|
|
2012-06-19 18:57:20 +02:00
|
|
|
/**
|
|
|
|
* Hide the optgroups that are not the selected timezone
|
|
|
|
*
|
2013-01-04 22:46:12 +01:00
|
|
|
* @param bool keepSelection Shall we keep the value selected, or shall the user be forced to repick one.
|
2012-06-19 18:57:20 +02:00
|
|
|
*/
|
2013-01-04 22:46:12 +01:00
|
|
|
phpbb.timezoneSwitchDate = function(keepSelection) {
|
2013-02-27 23:12:34 +01:00
|
|
|
if ($('#timezone_copy').length === 0) {
|
2012-07-23 18:13:22 +02:00
|
|
|
// We make a backup of the original dropdown, so we can remove optgroups
|
|
|
|
// instead of setting display to none, because IE and chrome will not
|
|
|
|
// hide options inside of optgroups and selects via css
|
|
|
|
$('#timezone').clone().attr('id', 'timezone_copy').css('display', 'none').attr('name', 'tz_copy').insertAfter('#timezone');
|
|
|
|
} else {
|
|
|
|
// Copy the content of our backup, so we can remove all unneeded options
|
|
|
|
$('#timezone').replaceWith($('#timezone_copy').clone().attr('id', 'timezone').css('display', 'block').attr('name', 'tz'));
|
|
|
|
}
|
|
|
|
|
2013-02-27 23:12:34 +01:00
|
|
|
if ($('#tz_date').val() !== '') {
|
2012-07-23 18:13:22 +02:00
|
|
|
$('#timezone > optgroup').remove(":not([label='" + $('#tz_date').val() + "'])");
|
|
|
|
}
|
2012-06-19 18:57:20 +02:00
|
|
|
|
2013-02-27 23:12:34 +01:00
|
|
|
if ($('#tz_date').val() === $('#tz_select_date_suggest').attr('data-suggested-tz')) {
|
2012-07-19 10:34:21 +02:00
|
|
|
$('#tz_select_date_suggest').css('display', 'none');
|
|
|
|
} else {
|
|
|
|
$('#tz_select_date_suggest').css('display', 'inline');
|
|
|
|
}
|
|
|
|
|
2013-02-27 23:12:34 +01:00
|
|
|
if ($("#timezone > optgroup[label='" + $('#tz_date').val() + "'] > option").size() === 1) {
|
2012-06-19 18:57:20 +02:00
|
|
|
// If there is only one timezone for the selected date, we just select that automatically.
|
|
|
|
$("#timezone > optgroup[label='" + $('#tz_date').val() + "'] > option:first").attr('selected', true);
|
2013-01-04 22:46:12 +01:00
|
|
|
keepSelection = true;
|
2012-06-19 18:57:20 +02:00
|
|
|
}
|
|
|
|
|
2013-01-04 22:46:12 +01:00
|
|
|
if (typeof keepSelection !== 'undefined' && !keepSelection) {
|
2013-01-02 22:05:14 +01:00
|
|
|
var timezoneOptions = $('#timezone > optgroup option');
|
|
|
|
if (timezoneOptions.filter(':selected').length <= 0) {
|
|
|
|
timezoneOptions.filter(':first').attr('selected', true);
|
|
|
|
}
|
2012-06-19 18:57:20 +02:00
|
|
|
}
|
2013-01-04 22:46:12 +01:00
|
|
|
};
|
2012-06-19 18:57:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the date/time select
|
|
|
|
*/
|
2013-01-04 22:46:12 +01:00
|
|
|
phpbb.timezoneEnableDateSelection = function() {
|
2012-06-19 18:57:20 +02:00
|
|
|
$('#tz_select_date').css('display', 'block');
|
2013-01-04 22:46:12 +01:00
|
|
|
};
|
2012-06-19 18:57:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Preselect a date/time or suggest one, if it is not picked.
|
|
|
|
*
|
2013-01-04 22:46:12 +01:00
|
|
|
* @param bool forceSelector Shall we select the suggestion?
|
2012-06-19 18:57:20 +02:00
|
|
|
*/
|
2013-01-04 22:46:12 +01:00
|
|
|
phpbb.timezonePreselectSelect = function(forceSelector) {
|
2012-06-19 18:57:20 +02:00
|
|
|
|
|
|
|
// The offset returned here is in minutes and negated.
|
|
|
|
// http://www.w3schools.com/jsref/jsref_getTimezoneOffset.asp
|
|
|
|
var offset = (new Date()).getTimezoneOffset();
|
2013-01-10 10:59:20 +01:00
|
|
|
var sign = '-';
|
2012-06-19 18:57:20 +02:00
|
|
|
|
|
|
|
if (offset < 0) {
|
2013-01-10 10:59:20 +01:00
|
|
|
sign = '+';
|
2012-06-19 18:57:20 +02:00
|
|
|
offset = -offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
var minutes = offset % 60;
|
|
|
|
var hours = (offset - minutes) / 60;
|
|
|
|
|
|
|
|
if (hours < 10) {
|
|
|
|
hours = '0' + hours.toString();
|
|
|
|
} else {
|
|
|
|
hours = hours.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (minutes < 10) {
|
|
|
|
minutes = '0' + minutes.toString();
|
|
|
|
} else {
|
|
|
|
minutes = minutes.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
var prefix = 'GMT' + sign + hours + ':' + minutes;
|
2013-01-04 22:46:12 +01:00
|
|
|
var prefixLength = prefix.length;
|
|
|
|
var selectorOptions = $('#tz_date > option');
|
2013-01-10 10:59:20 +01:00
|
|
|
var i;
|
2012-06-19 18:57:20 +02:00
|
|
|
|
2013-01-10 10:59:20 +01:00
|
|
|
for (i = 0; i < selectorOptions.length; ++i) {
|
2013-01-04 22:46:12 +01:00
|
|
|
var option = selectorOptions[i];
|
2012-06-19 18:57:20 +02:00
|
|
|
|
2013-02-27 23:12:34 +01:00
|
|
|
if (option.value.substring(0, prefixLength) === prefix) {
|
|
|
|
if ($('#tz_date').val() !== option.value && !forceSelector) {
|
2012-06-19 18:57:20 +02:00
|
|
|
// We do not select the option for the user, but notify him,
|
|
|
|
// that we would suggest a different setting.
|
2013-01-04 22:46:12 +01:00
|
|
|
phpbb.timezoneSwitchDate(true);
|
2012-07-24 17:09:26 +02:00
|
|
|
$('#tz_select_date_suggest').css('display', 'inline');
|
2012-06-19 18:57:20 +02:00
|
|
|
} else {
|
|
|
|
option.selected = true;
|
2013-01-04 22:46:12 +01:00
|
|
|
phpbb.timezoneSwitchDate(!forceSelector);
|
2012-06-19 18:57:20 +02:00
|
|
|
$('#tz_select_date_suggest').css('display', 'none');
|
|
|
|
}
|
2012-07-24 17:09:26 +02:00
|
|
|
|
|
|
|
$('#tz_select_date_suggest').attr('title', $('#tz_select_date_suggest').attr('data-l-suggestion').replace("%s", option.innerHTML));
|
|
|
|
$('#tz_select_date_suggest').attr('value', $('#tz_select_date_suggest').attr('data-l-suggestion').replace("%s", option.innerHTML.substring(0, 9)));
|
|
|
|
$('#tz_select_date_suggest').attr('data-suggested-tz', option.innerHTML);
|
|
|
|
|
|
|
|
// Found the suggestion, there cannot be more, so return from here.
|
|
|
|
return;
|
2012-06-19 18:57:20 +02:00
|
|
|
}
|
|
|
|
}
|
2013-01-04 22:46:12 +01:00
|
|
|
};
|
2012-06-19 18:57:20 +02:00
|
|
|
|
2013-01-15 11:31:39 -06:00
|
|
|
// Toggle notification list
|
|
|
|
$('#notification_list_button').click(function(e) {
|
|
|
|
$('#notification_list').toggle();
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
$('#phpbb').click(function(e) {
|
2013-01-15 12:30:26 -06:00
|
|
|
var target = $(e.target);
|
2013-01-15 11:31:39 -06:00
|
|
|
|
2013-01-15 12:30:26 -06:00
|
|
|
if (!target.is('#notification_list') && !target.is('#notification_list_button') && !target.parents().is('#notification_list')) {
|
2013-01-15 11:31:39 -06:00
|
|
|
$('#notification_list').hide();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-06-21 19:16:05 +01:00
|
|
|
phpbb.ajaxCallbacks = {};
|
2011-10-15 17:29:34 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an AJAX callback to be used by phpbb.ajaxify.
|
|
|
|
*
|
|
|
|
* See the phpbb.ajaxify comments for information on stuff like parameters.
|
|
|
|
*
|
|
|
|
* @param string id The name of the callback.
|
|
|
|
* @param function callback The callback to be called.
|
|
|
|
*/
|
2013-01-04 22:46:12 +01:00
|
|
|
phpbb.addAjaxCallback = function(id, callback) {
|
2012-06-21 19:16:05 +01:00
|
|
|
if (typeof callback === 'function') {
|
|
|
|
phpbb.ajaxCallbacks[id] = callback;
|
2011-07-23 18:58:37 +01:00
|
|
|
}
|
|
|
|
return this;
|
2012-06-21 19:16:05 +01:00
|
|
|
};
|
2011-07-14 14:57:45 +01:00
|
|
|
|
2011-07-14 17:49:17 +01:00
|
|
|
|
2011-10-22 16:16:15 +01:00
|
|
|
/**
|
|
|
|
* This callback alternates text - it replaces the current text with the text in
|
|
|
|
* the alt-text data attribute, and replaces the text in the attribute with the
|
|
|
|
* current text so that the process can be repeated.
|
|
|
|
*/
|
2013-01-04 22:46:12 +01:00
|
|
|
phpbb.addAjaxCallback('alt_text', function() {
|
2012-02-08 19:38:21 +01:00
|
|
|
var el = $(this),
|
2012-06-21 19:16:05 +01:00
|
|
|
altText;
|
2012-02-08 19:38:21 +01:00
|
|
|
|
2012-06-21 19:16:05 +01:00
|
|
|
altText = el.attr('data-alt-text');
|
2012-04-18 17:01:40 +02:00
|
|
|
el.attr('data-alt-text', el.text());
|
2012-06-21 19:16:05 +01:00
|
|
|
el.attr('title', altText);
|
2013-01-08 23:35:39 +01:00
|
|
|
el.text(altText);
|
2011-09-25 16:02:26 +01:00
|
|
|
});
|
|
|
|
|
2012-04-18 17:03:24 +02:00
|
|
|
/**
|
|
|
|
* This callback is based on the alt_text callback.
|
|
|
|
*
|
|
|
|
* It replaces the current text with the text in the alt-text data attribute,
|
|
|
|
* and replaces the text in the attribute with the current text so that the
|
|
|
|
* process can be repeated.
|
2012-07-05 00:30:02 +02:00
|
|
|
* Additionally it replaces the class of the link's parent
|
|
|
|
* and changes the link itself.
|
2012-04-18 17:03:24 +02:00
|
|
|
*/
|
2013-01-04 22:46:12 +01:00
|
|
|
phpbb.addAjaxCallback('toggle_link', function() {
|
2012-04-18 17:03:24 +02:00
|
|
|
var el = $(this),
|
2013-01-04 22:46:12 +01:00
|
|
|
toggleText,
|
|
|
|
toggleUrl,
|
|
|
|
toggleClass;
|
2012-07-05 00:30:02 +02:00
|
|
|
|
|
|
|
// Toggle link text
|
|
|
|
|
2013-01-04 22:46:12 +01:00
|
|
|
toggleText = el.attr('data-toggle-text');
|
2012-07-05 00:30:02 +02:00
|
|
|
el.attr('data-toggle-text', el.text());
|
2013-01-04 22:46:12 +01:00
|
|
|
el.attr('title', toggleText);
|
|
|
|
el.text(toggleText);
|
2012-07-05 00:30:02 +02:00
|
|
|
|
|
|
|
// Toggle link url
|
2013-01-04 22:46:12 +01:00
|
|
|
toggleUrl = el.attr('data-toggle-url');
|
2012-07-05 00:30:02 +02:00
|
|
|
el.attr('data-toggle-url', el.attr('href'));
|
2013-01-04 22:46:12 +01:00
|
|
|
el.attr('href', toggleUrl);
|
2012-07-05 00:30:02 +02:00
|
|
|
|
|
|
|
// Toggle class of link parent
|
2013-01-04 22:46:12 +01:00
|
|
|
toggleClass = el.attr('data-toggle-class');
|
2012-07-05 00:30:02 +02:00
|
|
|
el.attr('data-toggle-class', el.parent().attr('class'));
|
2013-01-04 22:46:12 +01:00
|
|
|
el.parent().attr('class', toggleClass);
|
2012-04-18 17:03:24 +02:00
|
|
|
});
|
2011-07-26 11:46:49 +01:00
|
|
|
|
2013-04-09 18:38:59 +03:00
|
|
|
/**
|
|
|
|
* Automatically resize textarea
|
|
|
|
*
|
2013-04-30 11:27:43 +03:00
|
|
|
* This function automatically resizes textarea elements when user
|
2013-04-09 18:38:59 +03:00
|
|
|
* types text.
|
|
|
|
*
|
2013-04-30 11:27:43 +03:00
|
|
|
* @param {jQuery} items jQuery object(s) to resize
|
|
|
|
* @param {object} options Optional parameter that adjusts default
|
2013-04-09 18:38:59 +03:00
|
|
|
* configuration. See configuration variable
|
2013-04-30 11:27:43 +03:00
|
|
|
*
|
|
|
|
* Optional parameters:
|
|
|
|
* minWindowHeight {number} Minimum browser window height when textareas are resized. Default = 500
|
|
|
|
* minHeight {number} Minimum height of textarea. Default = 200
|
|
|
|
* maxHeight {number} Maximum height of textarea. Default = 500
|
|
|
|
* heightDiff {number} Minimum difference between window and textarea height. Default = 200
|
|
|
|
* resizeCallback {function} Function to call after resizing textarea
|
|
|
|
* resetCallback {function} Function to call when resize has been canceled
|
|
|
|
|
|
|
|
* Callback function format: function(item) {}
|
|
|
|
* this points to DOM object
|
|
|
|
* item is a jQuery object, same as this
|
2013-04-09 18:38:59 +03:00
|
|
|
*/
|
2013-04-30 11:27:43 +03:00
|
|
|
phpbb.resizeTextArea = function(items, options) {
|
2013-04-09 18:38:59 +03:00
|
|
|
// Configuration
|
|
|
|
var configuration = {
|
2013-04-30 11:27:43 +03:00
|
|
|
minWindowHeight: 500,
|
|
|
|
minHeight: 200,
|
|
|
|
maxHeight: 500,
|
|
|
|
heightDiff: 200,
|
|
|
|
resizeCallback: function(item) { },
|
|
|
|
resetCallback: function(item) { }
|
|
|
|
};
|
2013-04-09 18:38:59 +03:00
|
|
|
|
|
|
|
if (arguments.length > 1)
|
|
|
|
{
|
2013-04-30 11:27:43 +03:00
|
|
|
configuration = $.extend(configuration, options);
|
2013-04-09 18:38:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function resetAutoResize(item)
|
|
|
|
{
|
|
|
|
var $item = $(item);
|
|
|
|
if ($item.hasClass('auto-resized'))
|
|
|
|
{
|
2013-04-10 09:08:25 +03:00
|
|
|
$(item).css({height: '', resize: ''}).removeClass('auto-resized');
|
2013-04-09 18:38:59 +03:00
|
|
|
configuration.resetCallback.call(item, $item);
|
|
|
|
}
|
2013-04-30 11:27:43 +03:00
|
|
|
}
|
2013-04-09 18:38:59 +03:00
|
|
|
|
|
|
|
function autoResize(item)
|
|
|
|
{
|
|
|
|
function setHeight(height)
|
|
|
|
{
|
2013-04-10 09:08:25 +03:00
|
|
|
$item.css({height: height + 'px', resize: 'none'}).addClass('auto-resized');
|
2013-04-09 18:38:59 +03:00
|
|
|
configuration.resizeCallback.call(item, $item);
|
|
|
|
}
|
|
|
|
|
|
|
|
var windowHeight = $(window).height();
|
|
|
|
|
|
|
|
if (windowHeight < configuration.minWindowHeight)
|
|
|
|
{
|
|
|
|
resetAutoResize(item);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var maxHeight = Math.min(Math.max(windowHeight - configuration.heightDiff, configuration.minHeight), configuration.maxHeight),
|
|
|
|
$item = $(item),
|
|
|
|
height = parseInt($item.height()),
|
|
|
|
scrollHeight = (item.scrollHeight) ? item.scrollHeight : 0;
|
|
|
|
|
|
|
|
if (height > maxHeight)
|
|
|
|
{
|
|
|
|
setHeight(maxHeight);
|
|
|
|
}
|
|
|
|
else if (scrollHeight > (height + 5))
|
|
|
|
{
|
|
|
|
setHeight(Math.min(maxHeight, scrollHeight));
|
|
|
|
}
|
2013-04-30 11:27:43 +03:00
|
|
|
}
|
2013-04-09 18:38:59 +03:00
|
|
|
|
|
|
|
items.bind('focus change keyup', function() {
|
|
|
|
$(this).each(function() {
|
|
|
|
autoResize(this);
|
|
|
|
});
|
2013-04-10 09:08:25 +03:00
|
|
|
}).change();
|
2013-04-09 18:38:59 +03:00
|
|
|
|
|
|
|
$(window).resize(function() {
|
|
|
|
items.each(function() {
|
|
|
|
if ($(this).hasClass('auto-resized'))
|
|
|
|
{
|
|
|
|
autoResize(this);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-05-20 20:29:37 +03:00
|
|
|
/**
|
|
|
|
* Adjust textarea to manage code bbcode
|
|
|
|
*
|
|
|
|
* This function allows to use tab characters when typing code
|
|
|
|
* and keeps indentation of previous line of code when adding new
|
|
|
|
* line while typing code.
|
|
|
|
*
|
|
|
|
* Editor's functionality is changed only when cursor is between
|
|
|
|
* [code] and [/code] bbcode tags.
|
|
|
|
*
|
|
|
|
* @param {object} textarea Textarea DOM object to apply editor to
|
|
|
|
*/
|
|
|
|
phpbb.applyCodeEditor = function(textarea) {
|
|
|
|
// list of allowed start and end bbcode code tags, in lower case
|
|
|
|
var startTags = ['[code]', '[code='],
|
|
|
|
startTagsEnd = ']',
|
|
|
|
endTags = ['[/code]'];
|
|
|
|
|
|
|
|
if ($(textarea).data('code-editor') === true) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if cursor is currently inside code tag
|
|
|
|
*
|
|
|
|
* @return {boolean} True if cursor is in code tag
|
|
|
|
*/
|
|
|
|
function inTag() {
|
|
|
|
var start = textarea.selectionStart,
|
|
|
|
lastEnd = -1,
|
|
|
|
lastStart = -1,
|
|
|
|
i, index, value;
|
|
|
|
|
|
|
|
value = textarea.value.toLowerCase();
|
|
|
|
|
|
|
|
for (i = 0; i < startTags.length; i++) {
|
|
|
|
var tagLength = startTags[i].length;
|
|
|
|
if (start >= tagLength) {
|
|
|
|
index = value.lastIndexOf(startTags[i], start - tagLength);
|
|
|
|
lastStart = Math.max(lastStart, index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (lastStart == -1) return false;
|
|
|
|
|
|
|
|
if (start > 0) {
|
|
|
|
for (i = 0; i < endTags.length; i++) {
|
|
|
|
index = value.lastIndexOf(endTags[i], start - 1);
|
|
|
|
lastEnd = Math.max(lastEnd, index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (lastEnd < lastStart);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get line of text before cursor
|
|
|
|
*
|
|
|
|
* @param {boolean} stripCodeStart If true, only part of line
|
|
|
|
* after [code] tag will be returned.
|
|
|
|
*
|
|
|
|
* @return {string} Line of text
|
|
|
|
*/
|
|
|
|
function getLastLine(stripCodeStart) {
|
|
|
|
var start = textarea.selectionStart,
|
|
|
|
value = textarea.value,
|
|
|
|
index = value.lastIndexOf("\n", start - 1);
|
|
|
|
|
|
|
|
value = value.substring(index + 1, start);
|
|
|
|
|
|
|
|
if (stripCodeStart) {
|
|
|
|
for (var i = 0; i < startTags.length; i++) {
|
|
|
|
index = value.lastIndexOf(startTags[i]);
|
|
|
|
if (index >= 0) {
|
|
|
|
var tagLength = startTags[i].length;
|
|
|
|
|
|
|
|
value = value.substring(index + tagLength);
|
|
|
|
if (startTags[i].lastIndexOf(startTagsEnd) != tagLength) {
|
|
|
|
index = value.indexOf(startTagsEnd);
|
|
|
|
|
|
|
|
if (index >= 0) {
|
|
|
|
value = value.substr(index + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append text at cursor position
|
|
|
|
*
|
|
|
|
* @param {string} Text Text to append
|
|
|
|
*/
|
|
|
|
function appendText(text) {
|
|
|
|
var start = textarea.selectionStart,
|
|
|
|
end = textarea.selectionEnd,
|
|
|
|
value = textarea.value;
|
|
|
|
|
|
|
|
textarea.value = value.substr(0, start) + text + value.substr(end);
|
|
|
|
textarea.selectionStart = textarea.selectionEnd = start + text.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
$(textarea).data('code-editor', true).on('keydown', function(event) {
|
|
|
|
var key = event.keyCode || event.which;
|
|
|
|
|
|
|
|
// intercept tabs
|
|
|
|
if (key == 9) {
|
|
|
|
if (inTag()) {
|
|
|
|
appendText("\t");
|
|
|
|
event.preventDefault();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// intercept new line characters
|
|
|
|
if (key == 13) {
|
|
|
|
if (inTag()) {
|
|
|
|
var lastLine = getLastLine(true),
|
|
|
|
code = '' + /^\s*/g.exec(lastLine);
|
|
|
|
|
|
|
|
if (code.length > 0) {
|
|
|
|
appendText("\n" + code);
|
|
|
|
event.preventDefault();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply code editor to all textarea elements with data-bbcode attribute
|
|
|
|
*/
|
|
|
|
$(document).ready(function() {
|
|
|
|
$('textarea[data-bbcode]').each(function() {
|
|
|
|
phpbb.applyCodeEditor(this);
|
|
|
|
});
|
|
|
|
});
|
2013-04-09 18:38:59 +03:00
|
|
|
|
2011-09-27 19:32:06 +01:00
|
|
|
})(jQuery); // Avoid conflicts with other libraries
|