diff options
Diffstat (limited to 'phpBB/assets/javascript')
-rw-r--r-- | phpBB/assets/javascript/core.js | 647 | ||||
-rw-r--r-- | phpBB/assets/javascript/editor.js | 4 |
2 files changed, 328 insertions, 323 deletions
diff --git a/phpBB/assets/javascript/core.js b/phpBB/assets/javascript/core.js index 388f31698f..b5187991f9 100644 --- a/phpBB/assets/javascript/core.js +++ b/phpBB/assets/javascript/core.js @@ -3,7 +3,7 @@ phpbb.alertTime = 100; (function($) { // Avoid conflicts with other libraries -"use strict"; +'use strict'; // define a couple constants for keydown functions. var keymap = { @@ -12,8 +12,8 @@ var keymap = { ESC: 27 }; -var dark = $('#darkenwrapper'); -var loadingIndicator = $('#loading_indicator'); +var $dark = $('#darkenwrapper'); +var $loadingIndicator = $('#loading_indicator'); var phpbbAlertTimer = null; phpbb.isTouch = (window && typeof window.ontouchstart !== 'undefined'); @@ -24,18 +24,20 @@ phpbb.isTouch = (window && typeof window.ontouchstart !== 'undefined'); * @returns object Returns loadingIndicator. */ phpbb.loadingIndicator = function() { - if (!loadingIndicator.is(':visible')) { - loadingIndicator.fadeIn(phpbb.alertTime); + if (!$loadingIndicator.is(':visible')) { + $loadingIndicator.fadeIn(phpbb.alertTime); // Wait fifteen seconds and display an error if nothing has been returned by then. phpbb.clearLoadingTimeout(); phpbbAlertTimer = setTimeout(function() { - if (loadingIndicator.is(':visible')) { - phpbb.alert($('#phpbb_alert').attr('data-l-err'), $('#phpbb_alert').attr('data-l-timeout-processing-req')); + var $alert = $('#phpbb_alert'); + + if ($loadingIndicator.is(':visible')) { + phpbb.alert($alert.attr('data-l-err'), $alert.attr('data-l-timeout-processing-req')); } }, 15000); } - return loadingIndicator; + return $loadingIndicator; }; /** @@ -73,60 +75,77 @@ phpbb.closeDarkenWrapper = function(delay) { * @returns object Returns the div created. */ phpbb.alert = function(title, msg, fadedark) { - var div = $('#phpbb_alert'); - div.find('.alert_title').html(title); - div.find('.alert_text').html(msg); + var $alert = $('#phpbb_alert'); + $alert.find('.alert_title').html(title); + $alert.find('.alert_text').html(msg); + + $(document).on('keydown.phpbb.alert', function(e) { + if (e.keyCode === keymap.ENTER || e.keyCode === keymap.ESC) { + phpbb.alert.close($alert, true); + e.preventDefault(); + e.stopPropagation(); + } + }); + phpbb.alert.open($alert); + + return $alert; +}; + +/** +* Handler for opening an alert box. +* +* @param jQuery $alert jQuery object. +*/ +phpbb.alert.open = function($alert) { + if (!$dark.is(':visible')) { + $dark.fadeIn(phpbb.alertTime); + } - if (!dark.is(':visible')) { - dark.fadeIn(phpbb.alertTime); + if ($loadingIndicator.is(':visible')) { + $loadingIndicator.fadeOut(phpbb.alertTime, function() { + $dark.append($alert); + $alert.fadeIn(phpbb.alertTime); + }); + } else if ($dark.is(':visible')) { + $dark.append($alert); + $alert.fadeIn(phpbb.alertTime); + } else { + $dark.append($alert); + $alert.show(); + $dark.fadeIn(phpbb.alertTime); } - div.bind('click', function(e) { + $alert.on('click', function(e) { e.stopPropagation(); }); - dark.one('click', function(e) { - var fade; - - div.find('.alert_close').unbind('click'); - fade = (typeof fadedark !== 'undefined' && !fadedark) ? div : dark; - fade.fadeOut(phpbb.alertTime, function() { - div.hide(); - }); + $dark.one('click', function(e) { + phpbb.alert.close($alert, true); e.preventDefault(); e.stopPropagation(); }); - $(document).keydown(function(e) { - if ((e.keyCode === keymap.ENTER || e.keyCode === keymap.ESC) && dark.is(':visible')) { - dark.trigger('click'); - - e.preventDefault(); - e.stopPropagation(); - } + $alert.find('.alert_close').one('click', function(e) { + phpbb.alert.close($alert, true); + e.preventDefault(); }); +}; - div.find('.alert_close').one('click', function(e) { - dark.trigger('click'); +/** +* Handler for closing an alert box. +* +* @param jQuery $alert jQuery object. +* @param bool fadedark Whether to remove dark background. +*/ +phpbb.alert.close = function($alert, fadedark) { + var $fade = (fadedark) ? $dark : $alert; - e.preventDefault(); + $fade.fadeOut(phpbb.alertTime, function() { + $alert.hide(); }); - if (loadingIndicator.is(':visible')) { - loadingIndicator.fadeOut(phpbb.alertTime, function() { - dark.append(div); - div.fadeIn(phpbb.alertTime); - }); - } else if (dark.is(':visible')) { - dark.append(div); - div.fadeIn(phpbb.alertTime); - } else { - dark.append(div); - div.show(); - dark.fadeIn(phpbb.alertTime); - } - - return div; + $alert.find('.alert_close').off('click'); + $(document).off('keydown.phpbb.alert'); }; /** @@ -143,81 +162,36 @@ phpbb.alert = function(title, msg, fadedark) { * @returns object Returns the div created. */ phpbb.confirm = function(msg, callback, fadedark) { - var div = $('#phpbb_confirm'); - div.find('.alert_text').html(msg); - - if (!dark.is(':visible')) { - dark.fadeIn(phpbb.alertTime); - } - - div.bind('click', function(e) { - e.stopPropagation(); - }); + var $confirmDiv = $('#phpbb_confirm'); + $confirmDiv.find('.alert_text').html(msg); + fadedark = fadedark || true; - var clickHandler = function(e) { - var res = this.name === 'confirm'; - var fade = (typeof fadedark !== 'undefined' && !fadedark && res) ? div : dark; - fade.fadeOut(phpbb.alertTime, function() { - div.hide(); - }); - div.find('input[type="button"]').unbind('click', clickHandler); - callback(res); + $(document).on('keydown.phpbb.alert', function(e) { + if (e.keyCode === keymap.ENTER || e.keyCode === keymap.ESC) { + var name = (e.keyCode === keymap.ENTER) ? 'confirm' : 'cancel'; - if (e) { + $('input[name="' + name + '"]').trigger('click'); e.preventDefault(); e.stopPropagation(); } - }; - div.find('input[type="button"]').one('click', clickHandler); - - dark.one('click', function(e) { - div.find('.alert_close').unbind('click'); - dark.fadeOut(phpbb.alertTime, function() { - div.hide(); - }); - callback(false); - - e.preventDefault(); - e.stopPropagation(); }); - $(document).bind('keydown', function(e) { - if (e.keyCode === keymap.ENTER) { - $('input[name="confirm"]').trigger('click'); - e.preventDefault(); - e.stopPropagation(); - } else if (e.keyCode === keymap.ESC) { - $('input[name="cancel"]').trigger('click'); - e.preventDefault(); - e.stopPropagation(); - } - }); + $confirmDiv.find('input[type="button"]').one('click.phpbb.confirmbox', function(e) { + var confirmed = this.name === 'confirm'; - div.find('.alert_close').one('click', function(e) { - var fade = (typeof fadedark !== 'undefined' && fadedark) ? div : dark; - fade.fadeOut(phpbb.alertTime, function() { - div.hide(); - }); - callback(false); + if (confirmed) { + callback(true); + } + $confirmDiv.find('input[type="button"]').off('click.phpbb.confirmbox'); + phpbb.alert.close($confirmDiv, fadedark || !confirmed); e.preventDefault(); + e.stopPropagation(); }); - if (loadingIndicator.is(':visible')) { - loadingIndicator.fadeOut(phpbb.alertTime, function() { - dark.append(div); - div.fadeIn(phpbb.alertTime); - }); - } else if (dark.is(':visible')) { - dark.append(div); - div.fadeIn(phpbb.alertTime); - } else { - dark.append(div); - div.show(); - dark.fadeIn(phpbb.alertTime); - } + phpbb.alert.open($confirmDiv); - return div; + return $confirmDiv; }; /** @@ -257,12 +231,12 @@ phpbb.parseQuerystring = function(string) { * that was returned and (if it is a form) the form action. */ phpbb.ajaxify = function(options) { - var elements = $(options.selector), + var $elements = $(options.selector), refresh = options.refresh, callback = options.callback, overlay = (typeof options.overlay !== 'undefined') ? options.overlay : true, - isForm = elements.is('form'), - isText = elements.is('input[type="text"], textarea'), + isForm = $elements.is('form'), + isText = $elements.is('input[type="text"], textarea'), eventName; if (isForm) { @@ -273,7 +247,7 @@ phpbb.ajaxify = function(options) { eventName = 'click'; } - elements.bind(eventName, function(event) { + $elements.on(eventName, function(event) { var action, method, data, submit, that = this, $this = $(this); if ($this.find('input[type="submit"][data-clicked]').attr('data-ajax') === 'false') { @@ -293,11 +267,12 @@ phpbb.ajaxify = function(options) { errorText = errorThrown; } else { - errorText = dark.attr('data-ajax-error-text-' + textStatus); - if (typeof errorText !== 'string' || !errorText.length) - errorText = dark.attr('data-ajax-error-text'); + errorText = $dark.attr('data-ajax-error-text-' + textStatus); + if (typeof errorText !== 'string' || !errorText.length) { + errorText = $dark.attr('data-ajax-error-text'); + } } - phpbb.alert(dark.attr('data-ajax-error-title'), errorText); + phpbb.alert($dark.attr('data-ajax-error-title'), errorText); } /** @@ -322,7 +297,7 @@ phpbb.ajaxify = function(options) { if (typeof res.MESSAGE_TITLE !== 'undefined') { alert = phpbb.alert(res.MESSAGE_TITLE, res.MESSAGE_TEXT); } else { - dark.fadeOut(phpbb.alertTime); + $dark.fadeOut(phpbb.alertTime); } if (typeof phpbb.ajaxCallbacks[callback] === 'function') { @@ -345,7 +320,7 @@ phpbb.ajaxify = function(options) { // Hide the alert even if we refresh the page, in case the user // presses the back button. - dark.fadeOut(phpbb.alertTime, function() { + $dark.fadeOut(phpbb.alertTime, function() { if (typeof alert !== 'undefined') { alert.hide(); } @@ -355,17 +330,19 @@ phpbb.ajaxify = function(options) { } else { // If confirmation is required, display a dialog to the user. phpbb.confirm(res.MESSAGE_BODY, function(del) { - if (del) { - phpbb.loadingIndicator(); - data = $('<form>' + res.S_HIDDEN_FIELDS + '</form>').serialize(); - $.ajax({ - url: res.S_CONFIRM_ACTION, - type: 'POST', - data: data + '&confirm=' + res.YES_VALUE + '&' + $('#phpbb_confirm form').serialize(), - success: returnHandler, - error: errorHandler - }); + if (!del) { + return; } + + phpbb.loadingIndicator(); + data = $('<form>' + res.S_HIDDEN_FIELDS + '</form>').serialize(); + $.ajax({ + url: res.S_CONFIRM_ACTION, + type: 'POST', + data: data + '&confirm=' + res.YES_VALUE + '&' + $('form', '#phpbb_confirm').serialize(), + success: returnHandler, + error: errorHandler + }); }, false); } } @@ -373,7 +350,7 @@ phpbb.ajaxify = function(options) { // If the element is a form, POST must be used and some extra data must // be taken from the form. var runFilter = (typeof options.filter === 'function'); - var data = {}; + data = {}; if (isForm) { action = $this.attr('action').replace('&', '&'); @@ -388,7 +365,7 @@ phpbb.ajaxify = function(options) { }); } } else if (isText) { - var name = ($this.attr('data-name') !== undefined) ? $this.attr('data-name') : this['name']; + var name = $this.attr('data-name') || this.name; action = $this.attr('data-url').replace('&', '&'); data[name] = this.value; method = 'POST'; @@ -399,7 +376,8 @@ phpbb.ajaxify = function(options) { } var sendRequest = function() { - if (overlay && (typeof $this.attr('data-overlay') === 'undefined' || $this.attr('data-overlay') === 'true')) { + var dataOverlay = $this.attr('data-overlay'); + if (overlay && (typeof dataOverlay === 'undefined' || dataOverlay === 'true')) { phpbb.loadingIndicator(); } @@ -411,7 +389,7 @@ phpbb.ajaxify = function(options) { error: errorHandler }); request.always(function() { - loadingIndicator.fadeOut(phpbb.alertTime); + $loadingIndicator.fadeOut(phpbb.alertTime); }); }; @@ -426,7 +404,7 @@ phpbb.ajaxify = function(options) { }); if (isForm) { - elements.find('input:submit').click(function () { + $elements.find('input:submit').click(function () { var $this = $(this); $this.siblings('[data-clicked]').removeAttr('data-clicked'); @@ -437,7 +415,13 @@ phpbb.ajaxify = function(options) { return this; }; -phpbb.search = {cache: {data: []}, tpl: [], container: []}; +phpbb.search = { + cache: { + data: [] + }, + tpl: [], + container: [] +}; /** * Get cached search data. @@ -478,7 +462,7 @@ phpbb.search.cache.set = function(id, key, value) { * @return undefined */ phpbb.search.cache.setResults = function(id, keyword, value) { - this.data[id]['results'][keyword] = value; + this.data[id].results[keyword] = value; }; /** @@ -495,16 +479,16 @@ phpbb.search.cleanKeyword = function(keyword) { * Get clean version of search keyword. If textarea supports several keywords * (one per line), it fetches the current keyword based on the caret position. * - * @param jQuery el Search input|textarea. + * @param jQuery $input Search input|textarea. * @param string keyword Input|textarea value. * @param bool multiline Whether textarea supports multiple search keywords. * * @return string Clean string. */ -phpbb.search.getKeyword = function(el, keyword, multiline) { +phpbb.search.getKeyword = function($input, keyword, multiline) { if (multiline) { - var line = phpbb.search.getKeywordLine(el); - keyword = keyword.split("\n").splice(line, 1); + var line = phpbb.search.getKeywordLine($input); + keyword = keyword.split('\n').splice(line, 1); } return phpbb.search.cleanKeyword(keyword); }; @@ -513,46 +497,48 @@ phpbb.search.getKeyword = function(el, keyword, multiline) { * Get the textarea line number on which the keyword resides - for textareas * that support multiple keywords (one per line). * - * @param jQuery el Search textarea. + * @param jQuery $textarea Search textarea. * @return int */ -phpbb.search.getKeywordLine = function (el) { - return el.val().substr(0, el.get(0).selectionStart).split("\n").length - 1; +phpbb.search.getKeywordLine = function ($textarea) { + var selectionStart = $textarea.get(0).selectionStart; + return $textarea.val().substr(0, selectionStart).split('\n').length - 1; }; /** * Set the value on the input|textarea. If textarea supports multiple * keywords, only the active keyword is replaced. * - * @param jQuery el Search input|textarea. + * @param jQuery $input Search input|textarea. * @param string value Value to set. * @param bool multiline Whether textarea supports multiple search keywords. * * @return undefined */ -phpbb.search.setValue = function(el, value, multiline) { +phpbb.search.setValue = function($input, value, multiline) { if (multiline) { - var line = phpbb.search.getKeywordLine(el), - lines = el.val().split("\n"); + var line = phpbb.search.getKeywordLine($input), + lines = $input.val().split('\n'); lines[line] = value; - value = lines.join("\n"); + value = lines.join('\n'); } - el.val(value); + $input.val(value); }; /** * Sets the onclick event to set the value on the input|textarea to the selected search result. * - * @param jQuery el Search input|textarea. + * @param jQuery $input Search input|textarea. * @param object value Result object. - * @param object container jQuery object for the search container. + * @param jQuery $row Result element. + * @param jQuery $container jQuery object for the search container. * * @return undefined */ -phpbb.search.setValueOnClick = function(el, value, row, container) { - row.click(function() { - phpbb.search.setValue(el, value.result, el.attr('data-multiline')); - container.hide(); +phpbb.search.setValueOnClick = function($input, value, $row, $container) { + $row.click(function() { + phpbb.search.setValue($input, value.result, $input.attr('data-multiline')); + $container.hide(); }); }; @@ -569,39 +555,39 @@ phpbb.search.setValueOnClick = function(el, value, row, container) { * @return bool Returns false. */ phpbb.search.filter = function(data, event, sendRequest) { - var el = $(this), - dataName = (el.attr('data-name') !== undefined) ? el.attr('data-name') : el.attr('name'), - minLength = parseInt(el.attr('data-min-length')), - searchID = el.attr('data-results'), - keyword = phpbb.search.getKeyword(el, data[dataName], el.attr('data-multiline')), + var $this = $(this), + dataName = ($this.attr('data-name') !== undefined) ? $this.attr('data-name') : $this.attr('name'), + minLength = parseInt($this.attr('data-min-length')), + searchID = $this.attr('data-results'), + keyword = phpbb.search.getKeyword($this, data[dataName], $this.attr('data-multiline')), cache = phpbb.search.cache.get(searchID), proceed = true; data[dataName] = keyword; - if (cache['timeout']) { - clearTimeout(cache['timeout']); + if (cache.timeout) { + clearTimeout(cache.timeout); } var timeout = setTimeout(function() { // Check min length and existence of cache. if (minLength > keyword.length) { proceed = false; - } else if (cache['last_search']) { + } else if (cache.lastSearch) { // Has the keyword actually changed? - if (cache['last_search'] === keyword) { + if (cache.lastSearch === keyword) { proceed = false; } else { // Do we already have results for this? - if (cache['results'][keyword]) { - var response = {keyword: keyword, results: cache['results'][keyword]}; - phpbb.search.handleResponse(response, el, true); + if (cache.results[keyword]) { + var response = {keyword: keyword, results: cache.results[keyword]}; + phpbb.search.handleResponse(response, $this, true); proceed = false; } // If the previous search didn't yield results and the string only had characters added to it, // then we won't bother sending a request. - if (keyword.indexOf(cache['last_search']) === 0 && cache['results'][cache['last_search']].length === 0) { - phpbb.search.cache.set(searchID, 'last_search', keyword); + if (keyword.indexOf(cache.lastSearch) === 0 && cache.results[cache.lastSearch].length === 0) { + phpbb.search.cache.set(searchID, 'lastSearch', keyword); phpbb.search.cache.setResults(searchID, keyword, []); proceed = false; } @@ -621,22 +607,22 @@ phpbb.search.filter = function(data, event, sendRequest) { * Handle search result response. * * @param object res Data received from server. - * @param jQuery el Search input|textarea. + * @param jQuery $input Search input|textarea. * @param bool fromCache Whether the results are from the cache. * @param function callback Optional callback to run when assigning each search result. * * @return undefined */ -phpbb.search.handleResponse = function(res, el, fromCache, callback) { +phpbb.search.handleResponse = function(res, $input, fromCache, callback) { if (typeof res !== 'object') { return; } - var searchID = el.attr('data-results'), - container = $(searchID); + var searchID = $input.attr('data-results'), + $container = $(searchID); - if (this.cache.get(searchID)['callback']) { - callback = this.cache.get(searchID)['callback']; + if (this.cache.get(searchID).callback) { + callback = this.cache.get(searchID).callback; } else if (typeof callback === 'function') { this.cache.set(searchID, 'callback', callback); } @@ -645,35 +631,35 @@ phpbb.search.handleResponse = function(res, el, fromCache, callback) { this.cache.setResults(searchID, res.keyword, res.results); } - this.cache.set(searchID, 'last_search', res.keyword); - this.showResults(res.results, el, container, callback); + this.cache.set(searchID, 'lastSearch', res.keyword); + this.showResults(res.results, $input, $container, callback); }; /** * Show search results. * * @param array results Search results. - * @param jQuery el Search input|textarea. - * @param jQuery container Search results container element. + * @param jQuery $input Search input|textarea. + * @param jQuery $container Search results container element. * @param function callback Optional callback to run when assigning each search result. * * @return undefined */ -phpbb.search.showResults = function(results, el, container, callback) { - var resultContainer = $('.search-results', container); - this.clearResults(resultContainer); +phpbb.search.showResults = function(results, $input, $container, callback) { + var $resultContainer = $('.search-results', $container); + this.clearResults($resultContainer); if (!results.length) { - container.hide(); + $container.hide(); return; } - var searchID = container.attr('id'), + var searchID = $container.attr('id'), tpl, row; if (!this.tpl[searchID]) { - tpl = $('.search-result-tpl', container); + tpl = $('.search-result-tpl', $container); this.tpl[searchID] = tpl.clone().removeClass('search-result-tpl'); tpl.remove(); } @@ -684,27 +670,27 @@ phpbb.search.showResults = function(results, el, container, callback) { row.find('.search-result').html(item.display); if (typeof callback === 'function') { - callback.call(this, el, item, row, container); + callback.call(this, $input, item, row, $container); } - row.appendTo(resultContainer).show(); + row.appendTo($resultContainer).show(); }); - container.show(); + $container.show(); }; /** * Clear search results. * - * @param jQuery container Search results container. + * @param jQuery $container Search results container. * @return undefined */ -phpbb.search.clearResults = function(container) { - container.children(':not(.search-result-tpl)').remove(); +phpbb.search.clearResults = function($container) { + $container.children(':not(.search-result-tpl)').remove(); }; -$('#phpbb').click(function(e) { - var target = $(e.target); +$('#phpbb').click(function() { + var $this = $(this); - if (!target.is('.live-search') && !target.parents().is('.live-search')) { + if (!$this.is('.live-search') && !$this.parents().is('.live-search')) { $('.live-search').hide(); } }); @@ -718,10 +704,7 @@ phpbb.history = {}; * @return bool Returns true if the method is supported. */ phpbb.history.isSupported = function(fn) { - if (typeof history === 'undefined' || typeof history[fn] === 'undefined') { - return false; - } - return true; + return !(typeof history === 'undefined' || typeof history[fn] === 'undefined'); }; /** @@ -783,36 +766,47 @@ phpbb.history.pushUrl = function(url, title, obj) { * @param bool keepSelection Shall we keep the value selected, or shall the user be forced to repick one. */ phpbb.timezoneSwitchDate = function(keepSelection) { - if ($('#timezone_copy').length === 0) { + var $timezoneCopy = $('#timezone_copy'); + var $timezone = $('#timezone'); + var $tzDate = $('#tz_date'); + var $tzSelectDateSuggest = $('#tz_select_date_suggest'); + + if ($timezoneCopy.length === 0) { // 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'); + $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')); + $timezone.html($timezoneCopy.html()); } - if ($('#tz_date').val() !== '') { - $('#timezone > optgroup').remove(":not([label='" + $('#tz_date').val() + "'])"); + if ($tzDate.val() !== '') { + $timezone.children('optgroup').remove(':not([label="' + $('#tz_date').val() + '"])'); } - if ($('#tz_date').val() === $('#tz_select_date_suggest').attr('data-suggested-tz')) { - $('#tz_select_date_suggest').css('display', 'none'); + if ($tzDate.val() === $tzSelectDateSuggest.attr('data-suggested-tz')) { + $tzSelectDateSuggest.css('display', 'none'); } else { - $('#tz_select_date_suggest').css('display', 'inline'); + $tzSelectDateSuggest.css('display', 'inline'); } + + var $tzOptions = $timezone.children('optgroup[label="' + $tzDate.val() + '"]').children('option'); - if ($("#timezone > optgroup[label='" + $('#tz_date').val() + "'] > option").size() === 1) { + if ($tzOptions.length === 1) { // If there is only one timezone for the selected date, we just select that automatically. - $("#timezone > optgroup[label='" + $('#tz_date').val() + "'] > option:first").prop('selected', true); + $tzOptions.prop('selected', true); keepSelection = true; } if (typeof keepSelection !== 'undefined' && !keepSelection) { - var timezoneOptions = $('#timezone > optgroup option'); - if (timezoneOptions.filter(':selected').length <= 0) { - timezoneOptions.filter(':first').prop('selected', true); + var $timezoneOptions = $timezone.find('optgroup option'); + if ($timezoneOptions.filter(':selected').length <= 0) { + $timezoneOptions.filter(':first').prop('selected', true); } } }; @@ -832,7 +826,6 @@ phpbb.timezoneEnableDateSelection = function() { phpbb.timezonePreselectSelect = function(forceSelector) { // The offset returned here is in minutes and negated. - // http://www.w3schools.com/jsref/jsref_getTimezoneOffset.asp var offset = (new Date()).getTimezoneOffset(); var sign = '-'; @@ -858,9 +851,11 @@ phpbb.timezonePreselectSelect = function(forceSelector) { var prefix = 'GMT' + sign + hours + ':' + minutes; var prefixLength = prefix.length; - var selectorOptions = $('#tz_date > option'); + var selectorOptions = $('option', '#tz_date'); var i; + var $tzSelectDateSuggest = $('#tz_select_date_suggest'); + for (i = 0; i < selectorOptions.length; ++i) { var option = selectorOptions[i]; @@ -869,16 +864,18 @@ phpbb.timezonePreselectSelect = function(forceSelector) { // We do not select the option for the user, but notify him, // that we would suggest a different setting. phpbb.timezoneSwitchDate(true); - $('#tz_select_date_suggest').css('display', 'inline'); + $tzSelectDateSuggest.css('display', 'inline'); } else { option.selected = true; phpbb.timezoneSwitchDate(!forceSelector); - $('#tz_select_date_suggest').css('display', 'none'); + $tzSelectDateSuggest.css('display', 'none'); } - $('#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); + var suggestion = $tzSelectDateSuggest.attr('data-l-suggestion'); + + $tzSelectDateSuggest.attr('title', suggestion.replace('%s', option.innerHTML)); + $tzSelectDateSuggest.attr('value', suggestion.replace('%s', option.innerHTML.substring(0, 9))); + $tzSelectDateSuggest.attr('data-suggested-tz', option.innerHTML); // Found the suggestion, there cannot be more, so return from here. return; @@ -916,22 +913,22 @@ phpbb.addAjaxCallback('member_search', function(res) { * current text so that the process can be repeated. */ phpbb.addAjaxCallback('alt_text', function() { - var el, + var $anchor, updateAll = $(this).data('update-all'), altText; if (updateAll !== undefined && updateAll.length) { - el = $(updateAll); + $anchor = $(updateAll); } else { - el = $(this); + $anchor = $(this); } - el.each(function() { - var el = $(this); - altText = el.attr('data-alt-text'); - el.attr('data-alt-text', el.text()); - el.attr('title', $.trim(altText)); - el.text(altText); + $anchor.each(function() { + var $this = $(this); + altText = $this.attr('data-alt-text'); + $this.attr('data-alt-text', $this.text()); + $this.attr('title', $.trim(altText)); + $this.text(altText); }); }); @@ -945,36 +942,36 @@ phpbb.addAjaxCallback('alt_text', function() { * and changes the link itself. */ phpbb.addAjaxCallback('toggle_link', function() { - var el, + var $anchor, updateAll = $(this).data('update-all') , toggleText, toggleUrl, toggleClass; if (updateAll !== undefined && updateAll.length) { - el = $(updateAll); + $anchor = $(updateAll); } else { - el = $(this); + $anchor = $(this); } - el.each(function() { - var el = $(this); + $anchor.each(function() { + var $this = $(this); // Toggle link text - toggleText = el.attr('data-toggle-text'); - el.attr('data-toggle-text', el.text()); - el.attr('title', $.trim(toggleText)); - el.text(toggleText); + toggleText = $this.attr('data-toggle-text'); + $this.attr('data-toggle-text', $this.text()); + $this.attr('title', $.trim(toggleText)); + $this.text(toggleText); // Toggle link url - toggleUrl = el.attr('data-toggle-url'); - el.attr('data-toggle-url', el.attr('href')); - el.attr('href', toggleUrl); + toggleUrl = $this.attr('data-toggle-url'); + $this.attr('data-toggle-url', $this.attr('href')); + $this.attr('href', toggleUrl); // Toggle class of link parent - toggleClass = el.attr('data-toggle-class'); - el.attr('data-toggle-class', el.parent().attr('class')); - el.parent().attr('class', toggleClass); + toggleClass = $this.attr('data-toggle-class'); + $this.attr('data-toggle-class', $this.parent().attr('class')); + $this.parent().attr('class', toggleClass); }); }); @@ -984,7 +981,7 @@ phpbb.addAjaxCallback('toggle_link', function() { * This function automatically resizes textarea elements when user * types text. * -* @param {jQuery} items jQuery object(s) to resize +* @param {jQuery} $items jQuery object(s) to resize * @param {object} options Optional parameter that adjusts default * configuration. See configuration variable * @@ -1000,25 +997,26 @@ phpbb.addAjaxCallback('toggle_link', function() { * this points to DOM object * item is a jQuery object, same as this */ -phpbb.resizeTextArea = function(items, options) { +phpbb.resizeTextArea = function($items, options) { // Configuration var configuration = { minWindowHeight: 500, minHeight: 200, maxHeight: 500, heightDiff: 200, - resizeCallback: function(item) { }, - resetCallback: function(item) { } + resizeCallback: function() {}, + resetCallback: function() {} }; - if (phpbb.isTouch) return; + if (phpbb.isTouch) { + return; + } if (arguments.length > 1) { configuration = $.extend(configuration, options); } - function resetAutoResize(item) - { + function resetAutoResize(item) { var $item = $(item); if ($item.hasClass('auto-resized')) { $(item).css({height: '', resize: ''}).removeClass('auto-resized'); @@ -1026,10 +1024,8 @@ phpbb.resizeTextArea = function(items, options) { } } - function autoResize(item) - { - function setHeight(height) - { + function autoResize(item) { + function setHeight(height) { height += parseInt($item.css('height')) - $item.height(); $item.css({height: height + 'px', resize: 'none'}).addClass('auto-resized'); configuration.resizeCallback.call(item, $item); @@ -1042,7 +1038,10 @@ phpbb.resizeTextArea = function(items, options) { return; } - var maxHeight = Math.min(Math.max(windowHeight - configuration.heightDiff, configuration.minHeight), configuration.maxHeight), + 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; @@ -1059,14 +1058,14 @@ phpbb.resizeTextArea = function(items, options) { } } - items.bind('focus change keyup', function() { + $items.on('focus change keyup', function() { $(this).each(function() { autoResize(this); }); }).change(); $(window).resize(function() { - items.each(function() { + $items.each(function() { if ($(this).hasClass('auto-resized')) { autoResize(this); } @@ -1104,7 +1103,9 @@ phpbb.inBBCodeTag = function(textarea, startTags, endTags) { lastStart = Math.max(lastStart, index); } } - if (lastStart == -1) return false; + if (lastStart === -1) { + return false; + } if (start > 0) { for (i = 0; i < endTags.length; i++) { @@ -1114,7 +1115,7 @@ phpbb.inBBCodeTag = function(textarea, startTags, endTags) { } return (lastEnd < lastStart); -} +}; /** @@ -1158,7 +1159,7 @@ phpbb.applyCodeEditor = function(textarea) { function getLastLine(stripCodeStart) { var start = textarea.selectionStart, value = textarea.value, - index = value.lastIndexOf("\n", start - 1); + index = value.lastIndexOf('\n', start - 1); value = value.substring(index + 1, start); @@ -1201,28 +1202,27 @@ phpbb.applyCodeEditor = function(textarea) { var key = event.keyCode || event.which; // intercept tabs - if (key == keymap.TAB && + if (key === keymap.TAB && !event.ctrlKey && !event.shiftKey && !event.altKey && !event.metaKey) { if (inTag()) { - appendText("\t"); + appendText('\t'); event.preventDefault(); return; } } // intercept new line characters - if (key == keymap.ENTER) { + if (key === keymap.ENTER) { if (inTag()) { var lastLine = getLastLine(true), code = '' + /^\s*/g.exec(lastLine); if (code.length > 0) { - appendText("\n" + code); + appendText('\n' + code); event.preventDefault(); - return; } } } @@ -1247,43 +1247,41 @@ phpbb.toggleDropdown = function() { var $this = $(this), options = $this.data('dropdown-options'), parent = options.parent, - visible = parent.hasClass('dropdown-visible'); + visible = parent.hasClass('dropdown-visible'), + direction; if (!visible) { // Hide other dropdown menus $(phpbb.dropdownHandles).each(phpbb.toggleDropdown); // Figure out direction of dropdown - var direction = options.direction, - verticalDirection = options.verticalDirection, + direction = options.direction; + var verticalDirection = options.verticalDirection, offset = $this.offset(); - if (direction == 'auto') { + if (direction === 'auto') { if (($(window).width() - $this.outerWidth(true)) / 2 > offset.left) { direction = 'right'; - } - else { + } else { direction = 'left'; } } - parent.toggleClass(options.leftClass, direction == 'left').toggleClass(options.rightClass, direction == 'right'); + parent.toggleClass(options.leftClass, direction === 'left') + .toggleClass(options.rightClass, direction === 'right'); - if (verticalDirection == 'auto') { + if (verticalDirection === 'auto') { var height = $(window).height(), top = offset.top - $(window).scrollTop(); - if (top < height * 0.7) { - verticalDirection = 'down'; - } - else { - verticalDirection = 'up'; - } + verticalDirection = (top < height * 0.7) ? 'down' : 'up'; } - parent.toggleClass(options.upClass, verticalDirection == 'up').toggleClass(options.downClass, verticalDirection == 'down'); + parent.toggleClass(options.upClass, verticalDirection === 'up') + .toggleClass(options.downClass, verticalDirection === 'down'); } options.dropdown.toggle(); - parent.toggleClass(options.visibleClass, !visible).toggleClass('dropdown-visible', !visible); + parent.toggleClass(options.visibleClass, !visible) + .toggleClass('dropdown-visible', !visible); // Check dimensions when showing dropdown // !visible because variable shows state of dropdown before it was toggled @@ -1304,8 +1302,7 @@ phpbb.toggleDropdown = function() { if (offset < 2) { $this.css('left', (2 - offset) + 'px'); - } - else if ((offset + width + 2) > windowWidth) { + } else if ((offset + width + 2) > windowWidth) { $this.css('margin-left', (windowWidth - offset - width - 2) + 'px'); } @@ -1315,7 +1312,7 @@ phpbb.toggleDropdown = function() { }); var freeSpace = parent.offset().left - 4; - if (direction == 'left') { + if (direction === 'left') { options.dropdown.css('margin-left', '-' + freeSpace + 'px'); // Try to position the notification dropdown correctly in RTL-responsive mode @@ -1342,8 +1339,7 @@ phpbb.toggleDropdown = function() { var e = arguments[0]; e.preventDefault(); e.stopPropagation(); - } - catch (error) { } + } catch (error) { } } return false; }; @@ -1354,7 +1350,7 @@ phpbb.toggleDropdown = function() { phpbb.toggleSubmenu = function(e) { $(this).siblings('.dropdown-submenu').toggle(); e.preventDefault(); -} +}; /** * Register dropdown menu @@ -1364,8 +1360,7 @@ phpbb.toggleSubmenu = function(e) { * @param {jQuery} dropdown Dropdown menu. * @param {Object} options List of options. Optional. */ -phpbb.registerDropdown = function(toggle, dropdown, options) -{ +phpbb.registerDropdown = function(toggle, dropdown, options) { var ops = { parent: toggle.parent(), // Parent item to add classes to direction: 'auto', // Direction of dropdown menu. Possible values: auto, left, right @@ -1411,8 +1406,8 @@ phpbb.colorPalette = function(dir, width, height) { numberList[3] = 'BF'; numberList[4] = 'FF'; - var table_class = (dir == 'h') ? 'horizontal-palette' : 'vertical-palette'; - html += '<table class="not-responsive colour-palette ' + table_class + '" style="width: auto;">'; + var tableClass = (dir == 'h') ? 'horizontal-palette' : 'vertical-palette'; + html += '<table class="not-responsive colour-palette ' + tableClass + '" style="width: auto;">'; for (r = 0; r < 5; r++) { if (dir == 'h') { @@ -1442,7 +1437,7 @@ phpbb.colorPalette = function(dir, width, height) { } html += '</table>'; return html; -} +}; /** * Register a color palette. @@ -1492,12 +1487,14 @@ phpbb.toggleDisplay = function(id, action, type) { type = 'block'; } - var display = $('#' + id).css('display'); + var $element = $('#' + id); + + var display = $element.css('display'); if (!action) { action = (display === '' || display === type) ? -1 : 1; } - $('#' + id).css('display', ((action === 1) ? type : 'none')); -} + $element.css('display', ((action === 1) ? type : 'none')); +}; /** * Toggle additional settings based on the selected @@ -1508,9 +1505,9 @@ phpbb.toggleDisplay = function(id, action, type) { */ phpbb.toggleSelectSettings = function(el) { el.children().each(function() { - var option = $(this), - setting = $(option.data('toggle-setting')); - setting.toggle(option.is(':selected')); + var $this = $(this), + $setting = $($this.data('toggle-setting')); + $setting.toggle($this.is(':selected')); }); }; @@ -1536,49 +1533,61 @@ phpbb.getFunctionByName = function (functionName) { * Register page dropdowns. */ phpbb.registerPageDropdowns = function() { - $('body').find('.dropdown-container').each(function() { + var $body = $('body'); + + $body.find('.dropdown-container').each(function() { var $this = $(this), - trigger = $this.find('.dropdown-trigger:first'), - contents = $this.find('.dropdown'), + $trigger = $this.find('.dropdown-trigger:first'), + $contents = $this.find('.dropdown'), options = { direction: 'auto', verticalDirection: 'auto' }, data; - if (!trigger.length) { + if (!$trigger.length) { data = $this.attr('data-dropdown-trigger'); - trigger = data ? $this.children(data) : $this.children('a:first'); + $trigger = data ? $this.children(data) : $this.children('a:first'); } - if (!contents.length) { + if (!$contents.length) { data = $this.attr('data-dropdown-contents'); - contents = data ? $this.children(data) : $this.children('div:first'); + $contents = data ? $this.children(data) : $this.children('div:first'); } - if (!trigger.length || !contents.length) return; + if (!$trigger.length || !$contents.length) { + return; + } - if ($this.hasClass('dropdown-up')) options.verticalDirection = 'up'; - if ($this.hasClass('dropdown-down')) options.verticalDirection = 'down'; - if ($this.hasClass('dropdown-left')) options.direction = 'left'; - if ($this.hasClass('dropdown-right')) options.direction = 'right'; + if ($this.hasClass('dropdown-up')) { + options.verticalDirection = 'up'; + } + if ($this.hasClass('dropdown-down')) { + options.verticalDirection = 'down'; + } + if ($this.hasClass('dropdown-left')) { + options.direction = 'left'; + } + if ($this.hasClass('dropdown-right')) { + options.direction = 'right'; + } - phpbb.registerDropdown(trigger, contents, options); + phpbb.registerDropdown($trigger, $contents, options); }); // Hide active dropdowns when click event happens outside - $('body').click(function(e) { - var parents = $(e.target).parents(); - if (!parents.is(phpbb.dropdownVisibleContainers)) { + $body.click(function(e) { + var $parents = $(e.target).parents(); + if (!$parents.is(phpbb.dropdownVisibleContainers)) { $(phpbb.dropdownHandles).each(phpbb.toggleDropdown); } }); -} +}; /** * Apply code editor to all textarea elements with data-bbcode attribute */ -$(document).ready(function() { +$(function() { $('textarea[data-bbcode]').each(function() { phpbb.applyCodeEditor(this); }); @@ -1595,12 +1604,12 @@ $(document).ready(function() { // Hide settings that are not selected via select element. $('select[data-togglable-settings]').each(function() { - var select = $(this); + var $this = $(this); - select.change(function() { - phpbb.toggleSelectSettings(select); + $this.change(function() { + phpbb.toggleSelectSettings($this); }); - phpbb.toggleSelectSettings(select); + phpbb.toggleSelectSettings($this); }); }); diff --git a/phpBB/assets/javascript/editor.js b/phpBB/assets/javascript/editor.js index dfc7dab525..5fd4f7eae3 100644 --- a/phpBB/assets/javascript/editor.js +++ b/phpBB/assets/javascript/editor.js @@ -266,10 +266,6 @@ function mozWrap(txtarea, open, close) { var selEnd = txtarea.selectionEnd; var scrollTop = txtarea.scrollTop; - if (selEnd === 1 || selEnd === 2) { - selEnd = selLength; - } - var s1 = (txtarea.value).substring(0,selStart); var s2 = (txtarea.value).substring(selStart, selEnd); var s3 = (txtarea.value).substring(selEnd, selLength); |