diff options
author | Cesar G <prototech91@gmail.com> | 2013-11-10 18:56:07 -0800 |
---|---|---|
committer | Cesar G <prototech91@gmail.com> | 2014-01-17 19:20:36 -0800 |
commit | 508b43c19733d15fcacc2226e91b1ec26a20a562 (patch) | |
tree | 910d687261882d1385957fcfa64c2238870d0489 /phpBB | |
parent | 688ca7fadef6c8890a10b709b8f4c7615d33fff0 (diff) | |
download | forums-508b43c19733d15fcacc2226e91b1ec26a20a562.tar forums-508b43c19733d15fcacc2226e91b1ec26a20a562.tar.gz forums-508b43c19733d15fcacc2226e91b1ec26a20a562.tar.bz2 forums-508b43c19733d15fcacc2226e91b1ec26a20a562.tar.xz forums-508b43c19733d15fcacc2226e91b1ec26a20a562.zip |
[ticket/11915] Update the inline attachment bbcode indices on every upload.
PHPBB3-11915
Diffstat (limited to 'phpBB')
-rw-r--r-- | phpBB/assets/javascript/plupload.js | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/phpBB/assets/javascript/plupload.js b/phpBB/assets/javascript/plupload.js index 1479b64f47..9c0526eb9f 100644 --- a/phpBB/assets/javascript/plupload.js +++ b/phpBB/assets/javascript/plupload.js @@ -148,6 +148,7 @@ phpbb.plupload.setData = function(data) { * @return undefined */ phpbb.plupload.update = function (data, action, index) { + phpbb.plupload.updateBbcode(action, index); phpbb.plupload.setData(data); phpbb.plupload.updateRows(); phpbb.plupload.clearParams(); @@ -307,6 +308,60 @@ phpbb.plupload.hideEmptyList = function() { } /** + * Update the indices used in inline attachment bbcodes. This ensures that the bbcodes + * correspond to the correct file after a file is added or removed. This should be called + * before the phpbb.plupload,data and phpbb.plupload.ids arrays are updated, otherwise it will + * not work correctly. + * + * @param string action The action that occurred -- either "addition" or "removal" + * @param int index The index of the attachment from phpbb.plupload.ids that was affected. + * + * @return undefined + */ +phpbb.plupload.updateBbcode = function (action, index) { + var textarea = $(phpbb.plupload.form).find('textarea[name="message"]'), + text = textarea.val(), + removal = (action === 'removal'); + + // Return if the bbcode isn't used at all. + if (text.indexOf('[attachment=') === -1) { + return; + } + + // Private function used to replace the bbcode. + var updateBbcode = function(match, fileName) { + // Remove the bbcode if the file was removed. + if (removal && index === i) { + return ''; + } + var newIndex = i + ((removal) ? -1 : 1); + return '[attachment=' + newIndex +']' + fileName + '[/attachment]'; + }; + + // Private function used to generate search regexp + var searchRegexp = function (index) { + return new RegExp('\\[attachment=' + index + '\\](.*?)\\[\\/attachment\\]', 'g'); + } + // The update order of the indices is based on the action taken to ensure that we don't corrupt + // the bbcode index by updating it several times as we move through the loop. + // Removal loop starts at the removed index and moves to the end of the array. + // Addition loop starts at the end of the array and moves to the added index at 0. + var searchLoop = function () { + if (typeof i === 'undefined') { + i = (removal) ? index : phpbb.plupload.ids.length - 1; + } + return (removal) ? (i < phpbb.plupload.ids.length): (i >= index); + } + var i; + + while (searchLoop()) { + text = text.replace(searchRegexp(i), updateBbcode); + (removal) ? i++ : i--; + } + textarea.val(text); +}; + +/** * Get Plupload file objects based on their upload status. * * @param int status Plupload status - plupload.DONE, plupload.FAILED, plupload.QUEUED, |