Move edit history JavaScript from template to WCF.Message.js
authorTim Düsterhus <duesterhus@woltlab.com>
Wed, 2 Jul 2014 15:47:05 +0000 (17:47 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Wed, 9 Jul 2014 19:58:58 +0000 (21:58 +0200)
com.woltlab.wcf/templates/editHistory.tpl
wcfsetup/install/files/js/WCF.Message.js

index 1c5fc9689b72808f9b9ad5611a348ca30a0debf0..d3c40fe44c1b6a7c0639ed7b870f4d8aca7d8997 100644 (file)
@@ -53,7 +53,7 @@
                                {foreach from=$objects item=edit}
                                        <tr class="jsEditRow">
                                                <td class="columnIcon">
-                                                       <span class="icon icon16 icon-undo"></span>
+                                                       <span class="icon icon16 icon-undo pointer jsRevertButton jsTooltip" title="{lang}wcf.edit.revert{/lang}" data-object-id="{@$edit->entryID}" data-confirm-message="{lang}wcf.edit.revert.sure{/lang}"></span>
                                                        <input type="radio" name="oldID" value="{@$edit->entryID}"{if $oldID == $edit->entryID} checked="checked"{/if} /> <input type="radio" name="newID" value="{@$edit->entryID}"{if $newID == $edit->entryID} checked="checked"{/if} />
                                                        {event name='rowButtons'}
                                                </td>
                        <script data-relocate="true">
                                //<![CDATA[
                                $(function () {
-                                       $('input[name=newID]').on('change', function (event) {
-                                               var newID = $(this).val();
-                                               
-                                               $('input[name=oldID]').each(function (event) {
-                                                       if ($(this).val() >= newID) $(this).disable();
-                                                       else $(this).enable();
-                                               });
-                                       });
-                                       
-                                       $('input[name=oldID]').on('change', function (event) {
-                                               var oldID = $(this).val();
-                                               
-                                               $('input[name=newID]').each(function (event) {
-                                                       if ($(this).val() <= oldID) $(this).disable();
-                                                       else $(this).enable();
-                                               });
-                                       });
-                                       $('input:checked').trigger('change');
+                                       new WCF.Message.EditHistory($('input[name=oldID]'), $('input[name=newID]'), '.jsEditRow');
                                });
                                //]]>
                        </script>
index bf338bbaf060a7aa9411fd0b8205ba9622f22623..61fcfccd7628aaf942be6f703218608305fd132c 100644 (file)
@@ -77,6 +77,154 @@ WCF.Message.BBCode.CodeViewer = Class.extend({
        }
 });
 
+/**
+ * Provides the dynamic parts of the edit history interface.
+ */
+WCF.Message.EditHistory = Class.extend({
+       /**
+        * jQuery object containing the radio buttons for the oldID
+        * @var object
+        */
+       _oldIDInputs: null,
+       
+       /**
+        * jQuery object containing the radio buttons for the oldID
+        * @var object
+        */
+       _newIDInputs: null,
+       
+       /**
+        * selector for the version rows
+        * @var string
+        */
+       _containerSelector: '',
+       
+       /**
+        * selector for the revert button
+        * @var string
+        */
+       _buttonSelector: '.jsRevertButton',
+       
+       /**
+        * Initializes the edit history interface.
+        * 
+        * @param       object  oldIDInputs
+        * @param       object  newIDInputs
+        * @param       string  containerSelector
+        * @param       string  buttonSelector
+        */
+       init: function(oldIDInputs, newIDInputs, containerSelector, buttonSelector) {
+               this._oldIDInputs = oldIDInputs;
+               this._newIDInputs = newIDInputs;
+               this._containerSelector = containerSelector;
+               this._buttonSelector = (buttonSelector) ? buttonSelector : '.jsRevertButton';
+               
+               this.proxy = new WCF.Action.Proxy({
+                       success: $.proxy(this._success, this)
+               });
+               
+               this._initInputs();
+               this._initElements();
+       },
+       
+       /**
+        * Initializes the radio buttons.
+        * Force the "oldID" to be lower than the "newID"
+        * 'current' is interpreted as Infinity.
+        */
+       _initInputs: function() {
+               var self = this;
+               this._newIDInputs.change(function(event) {
+                       var newID = $(this).val();
+                       
+                       self._oldIDInputs.each(function(event) {
+                               if ($(this).val() >= newID) {
+                                       $(this).disable();
+                               }
+                               else {
+                                       $(this).enable();
+                               }
+                       });
+               });
+               
+               this._oldIDInputs.change(function(event) {
+                       var oldID = $(this).val();
+                       
+                       self._newIDInputs.each(function(event) {
+                               if ($(this).val() <= oldID) {
+                                       $(this).disable();
+                               }
+                               else {
+                                       $(this).enable();
+                               }
+                       });
+               });
+               this._oldIDInputs.filter(':checked').change();
+               this._newIDInputs.filter(':checked').change();
+       },
+       
+       /**
+        * Initializes available element containers.
+        */
+       _initElements: function() {
+               var self = this;
+               $(this._containerSelector).each(function(index, container) {
+                       var $container = $(container);
+                       $container.find(self._buttonSelector).click($.proxy(self._click, self));
+               });
+       },
+       
+       /**
+        * Sends AJAX request.
+        * 
+        * @param       object          event
+        */
+       _click: function(event) {
+               var $target = $(event.currentTarget);
+               event.preventDefault();
+               
+               if ($target.data('confirmMessage')) {
+                       var self = this;
+                       
+                       WCF.System.Confirmation.show($target.data('confirmMessage'), function(action) {
+                               if (action === 'cancel') return;
+                               
+                               self._sendRequest($target);
+                       });
+               }
+               else {
+                       this._sendRequest($target);
+               }
+       },
+       
+       
+       /**
+        * Sends the request
+        * 
+        * @param       jQuery  object
+        */
+       _sendRequest: function(object) {
+               this.proxy.setOption('data', {
+                       actionName: 'revert',
+                       className: 'wcf\\data\\edit\\history\\entry\\EditHistoryEntryAction',
+                       objectIDs: [ $(object).data('objectID') ]
+               });
+               
+               this.proxy.sendRequest();
+       },
+       
+       /**
+        * Reloads the page to show the new versions.
+        * 
+        * @param       object          data
+        * @param       string          textStatus
+        * @param       object          jqXHR
+        */
+       _success: function(data, textStatus, jqXHR) {
+               window.location.reload(true);
+       }
+});
+
 /**
  * Prevents multiple submits of the same form by disabling the submit button.
  */