| 1 |
/** |
| 2 |
* |
| 3 |
*/ |
| 4 |
|
| 5 |
var CJTBlockOptionalRevisionBase = null; |
| 6 |
|
| 7 |
/** |
| 8 |
* |
| 9 |
*/ |
| 10 |
(function($) { |
| 11 |
|
| 12 |
/** |
| 13 |
* put your comment there... |
| 14 |
* |
| 15 |
*/ |
| 16 |
CJTBlockOptionalRevisionBase = function() { |
| 17 |
|
| 18 |
/** |
| 19 |
* put your comment there... |
| 20 |
* |
| 21 |
* @type Boolean |
| 22 |
*/ |
| 23 |
this.block; |
| 24 |
|
| 25 |
/** |
| 26 |
* |
| 27 |
*/ |
| 28 |
this.original; |
| 29 |
|
| 30 |
/** |
| 31 |
* put your comment there... |
| 32 |
* |
| 33 |
* @type Boolean |
| 34 |
*/ |
| 35 |
this.revision; |
| 36 |
|
| 37 |
/** |
| 38 |
* |
| 39 |
*/ |
| 40 |
this.state; |
| 41 |
|
| 42 |
/** |
| 43 |
* Switch state event |
| 44 |
*/ |
| 45 |
this.onBeforeSwitchState; |
| 46 |
|
| 47 |
/** |
| 48 |
* Switch state event |
| 49 |
*/ |
| 50 |
this.onDoneRestore; |
| 51 |
|
| 52 |
/** |
| 53 |
* Switch state event |
| 54 |
*/ |
| 55 |
this.onSwitchState; |
| 56 |
|
| 57 |
/** |
| 58 |
* put your comment there... |
| 59 |
* |
| 60 |
*/ |
| 61 |
this.CJTBlockOptionalRevisionBase = function(block, revision) { |
| 62 |
// Initialize instance. |
| 63 |
this.block = block; |
| 64 |
this.revision = revision; |
| 65 |
this.original = {}; |
| 66 |
this.state = 'normal'; |
| 67 |
// Events. |
| 68 |
this.onSwitchState = function() {}; |
| 69 |
this.onRestoreDone = function() {}; |
| 70 |
this.onBeforeSwitchState = function() {}; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* |
| 75 |
*/ |
| 76 |
this.cancel = function() { |
| 77 |
// Initialize |
| 78 |
var block = this.block; |
| 79 |
var toolbox = block.toolbox; |
| 80 |
var saveButton = toolbox.buttons['save']; |
| 81 |
var mdlBlock = block.block; |
| 82 |
var properties = block.features.restoreRevision.fields; |
| 83 |
var pom; |
| 84 |
// Before switch state event. |
| 85 |
this.onBeforeSwitchState() |
| 86 |
// Copy original data back to the block. |
| 87 |
$.each(properties, $.proxy( |
| 88 |
function(index, propertyName) { |
| 89 |
// Get property om |
| 90 |
pom = mdlBlock.property(propertyName).om; |
| 91 |
// Set the original value. |
| 92 |
pom.setValue(this.original[propertyName]); |
| 93 |
}, this) |
| 94 |
); |
| 95 |
// Remove cancel button. |
| 96 |
toolbox.remove('cancel-restore'); |
| 97 |
// Enable block components. |
| 98 |
block.enable(true); |
| 99 |
toolbox.enable(true); |
| 100 |
// Set notification changes back. |
| 101 |
block.changes = this.original.changes; |
| 102 |
var isChanged = CJTBlocksPage.blocks.calculateChanges(block.changes, 0, false); |
| 103 |
// Enable button is there is a change not saved yet, disable it if not. |
| 104 |
saveButton.enable(isChanged); |
| 105 |
// Reset save buttons properties back. |
| 106 |
saveButton.jButton.text('Save') |
| 107 |
// Clear RESTORE button style |
| 108 |
.removeClass('cjttblSW-restore'); |
| 109 |
saveButton.callback = saveButton._callback; |
| 110 |
block._onsavechanges = block.__onsavechanges; |
| 111 |
saveButton._callback = null; |
| 112 |
// Notify blocks page. |
| 113 |
CJTBlocksPage.blockContentChanged(block.block.id, isChanged); |
| 114 |
// Change state / Enter revision mode. |
| 115 |
this.onSwitchState(this.state = null); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* |
| 120 |
*/ |
| 121 |
this.display = function() { |
| 122 |
// Initialize. |
| 123 |
var block = this.block; |
| 124 |
var toolbox = block.toolbox; |
| 125 |
var saveButton = toolbox.buttons['save']; |
| 126 |
var mdlBlock = block.block; |
| 127 |
var properties = block.features.restoreRevision.fields; |
| 128 |
var pom; |
| 129 |
// Before switch state event. |
| 130 |
this.onBeforeSwitchState() |
| 131 |
// Cache 'Changes' array, don't reference it from block.changes |
| 132 |
// as both got the same variable reference. |
| 133 |
this.original.changes = block.changes; block.changes = []; |
| 134 |
// Display revision data |
| 135 |
$.each(properties, $.proxy( |
| 136 |
function(index, propertyName) { |
| 137 |
// Get property om |
| 138 |
pom = mdlBlock.property(propertyName).om; |
| 139 |
// Get original property value. |
| 140 |
this.original[propertyName] = pom.getValueCache(); |
| 141 |
// Display Revision value. |
| 142 |
pom.setValue(this.revision[propertyName]); |
| 143 |
}, this) |
| 144 |
); |
| 145 |
// Empty changes array. As it affected by the copy code above! |
| 146 |
block.changes = []; |
| 147 |
// Force Notification Changes to detect that current block has changes. |
| 148 |
CJTBlocksPage.blockContentChanged(mdlBlock.get('id'), true); |
| 149 |
// DISABLE-ALL toolbox buttons |
| 150 |
toolbox.enable(false); |
| 151 |
// Except save button! |
| 152 |
saveButton.enable(true); |
| 153 |
saveButton._callback = saveButton.callback; |
| 154 |
saveButton.__onsavechanges = block._onsavechanges; |
| 155 |
block._onsavechanges = saveButton.callback = $.proxy(this.restore, this); |
| 156 |
// Rename it to 'Restore' |
| 157 |
saveButton.jButton.text('Restore') |
| 158 |
// Reflect new style |
| 159 |
.addClass('cjttblSW-restore'); |
| 160 |
// Add cancel restore button. |
| 161 |
$('<a href="#" class="cjt-tb-link cjttbl-cancel-restore il-60x23"></a>') |
| 162 |
.text('Cancel') |
| 163 |
.insertBefore(saveButton.jButton); |
| 164 |
toolbox.add('cancel-restore', {callback : $.proxy(this.cancel, this)}); |
| 165 |
// Disable block. |
| 166 |
block.enable(false); |
| 167 |
// Change state / Enter revision mode. |
| 168 |
this.onSwitchState(this.state = 'revision'); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* |
| 173 |
*/ |
| 174 |
this.restore = function() { |
| 175 |
// Initialize. |
| 176 |
var block = this.block; |
| 177 |
var toolbox = block.toolbox; |
| 178 |
var saveButton = toolbox.buttons['save']; |
| 179 |
var server = CJTBlocksPage.server; |
| 180 |
var request = {rid : this.revision['id'], bid : block.block.get('id')}; |
| 181 |
// Restore revision via server. |
| 182 |
saveButton.loading(true); |
| 183 |
server.send('block', 'restoreRevision', request) |
| 184 |
.success($.proxy( |
| 185 |
function() { |
| 186 |
// Fire restore done event. |
| 187 |
this.onRestoreDone(); |
| 188 |
// Stop loading |
| 189 |
saveButton.loading(false); |
| 190 |
// Clear notify-save-changes list. |
| 191 |
this.original.changes = []; |
| 192 |
// Cancel restore action will reset |
| 193 |
// the block UI elements to the normal state. |
| 194 |
this.cancel(true); |
| 195 |
// Sync fields with server value. |
| 196 |
// This refrssh required for notifying saving |
| 197 |
// change to detect changes. |
| 198 |
var diFields = block.block.getDIFields(); |
| 199 |
// Push aceEditor into diFields list. |
| 200 |
diFields[diFields.length++] = block.block.aceEditor; |
| 201 |
diFields.each( |
| 202 |
function() { |
| 203 |
this.cjtSyncInputField(); |
| 204 |
} |
| 205 |
); |
| 206 |
}, this) |
| 207 |
); |
| 208 |
} |
| 209 |
|
| 210 |
} // End Prototype. |
| 211 |
|
| 212 |
})(jQuery); |
| 213 |
|