Skip to content

Commit

Permalink
Added warning when switching layout and the particles could potential…
Browse files Browse the repository at this point in the history
…ly get lost (fixes #275)
  • Loading branch information
w00fz committed Jun 21, 2015
1 parent 42a1d4d commit bfa1e01
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 21 deletions.
54 changes: 50 additions & 4 deletions platforms/common/application/lm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ var ready = require('elements/domready'),
trim = require('mout/string/trim'),
forEach = require('mout/collection/forEach'),

parseAjaxURI = require('../utils/get-ajax-url').parse,
getAjaxSuffix = require('../utils/get-ajax-suffix'),
getAjaxSuffix = require('../utils/get-ajax-suffix'),
parseAjaxURI = require('../utils/get-ajax-url').parse,
getAjaxURL = require('../utils/get-ajax-url').global,

flags = require('../utils/flags-state'),
Builder = require('./builder'),
History = require('../utils/history'),
validateField = require('../utils/field-validation'),
Expand Down Expand Up @@ -246,9 +248,11 @@ ready(function() {
});

// Switcher
var SWITCHER_HIT = false;
body.delegate('mouseover', '[data-lm-switcher]', function(event, element) {
if (event && event.preventDefault) { event.preventDefault(); }

SWITCHER_HIT = element;
if (!element.PopoverDefined) {
element.getPopover({
type: 'async',
Expand All @@ -259,7 +263,7 @@ ready(function() {
}
});

// Clear Layout
// Switch Layout
body.delegate('mousedown', '[data-switch]', function(event, element) {
if (event && event.preventDefault) { event.preventDefault(); }

Expand All @@ -285,7 +289,8 @@ ready(function() {
data.layout = JSON.stringify(lm.builder.serialize());
}

request(method, parseAjaxURI(element.data('switch') + getAjaxSuffix()), data, function(error, response) {
var uri = parseAjaxURI(element.data('switch') + getAjaxSuffix());
request(method, uri, data, function(error, response) {
element.hideIndicator();

if (!response.body.success) {
Expand All @@ -298,6 +303,47 @@ ready(function() {
return;
}

if (response.body.message && !flags.get('lm:switcher:' + window.btoa(uri), false)) {
// confirm before proceeding
flags.warning({
message: response.body.message,
callback: function(response, content) {
var confirm = content.find('[data-g-delete-confirm]'),
cancel = content.find('[data-g-delete-cancel]');

if (!confirm) { return; }

confirm.on('click', function(e) {
e.preventDefault();
if (this.attribute('disabled')) { return false; }

flags.get('lm:switcher:' + window.btoa(uri), true);
$([confirm, cancel]).attribute('disabled');
body.emit('mousedown', { target: element });

modal.close();
});

cancel.on('click', function(e) {
e.preventDefault();
if (this.attribute('disabled')) { return false; }

$([confirm, cancel]).attribute('disabled');
flags.get('lm:switcher:' + window.btoa(uri), false);

modal.close();
if (SWITCHER_HIT) {
setTimeout(function(){
SWITCHER_HIT.getPopover().show();
}, 5);
}
});
}
});

return false;
}

var preset = response.body.preset || 'default',
preset_name = response.body.title || 'Default',
structure = response.body.data,
Expand Down
90 changes: 75 additions & 15 deletions platforms/common/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1986,9 +1986,11 @@ var ready = require('elements/domready'),
trim = require('mout/string/trim'),
forEach = require('mout/collection/forEach'),

parseAjaxURI = require('../utils/get-ajax-url').parse,
getAjaxSuffix = require('../utils/get-ajax-suffix'),
getAjaxSuffix = require('../utils/get-ajax-suffix'),
parseAjaxURI = require('../utils/get-ajax-url').parse,
getAjaxURL = require('../utils/get-ajax-url').global,

flags = require('../utils/flags-state'),
Builder = require('./builder'),
History = require('../utils/history'),
validateField = require('../utils/field-validation'),
Expand Down Expand Up @@ -2221,9 +2223,11 @@ ready(function() {
});

// Switcher
var SWITCHER_HIT = false;
body.delegate('mouseover', '[data-lm-switcher]', function(event, element) {
if (event && event.preventDefault) { event.preventDefault(); }

SWITCHER_HIT = element;
if (!element.PopoverDefined) {
element.getPopover({
type: 'async',
Expand All @@ -2234,7 +2238,7 @@ ready(function() {
}
});

// Clear Layout
// Switch Layout
body.delegate('mousedown', '[data-switch]', function(event, element) {
if (event && event.preventDefault) { event.preventDefault(); }

Expand All @@ -2260,7 +2264,8 @@ ready(function() {
data.layout = JSON.stringify(lm.builder.serialize());
}

request(method, parseAjaxURI(element.data('switch') + getAjaxSuffix()), data, function(error, response) {
var uri = parseAjaxURI(element.data('switch') + getAjaxSuffix());
request(method, uri, data, function(error, response) {
element.hideIndicator();

if (!response.body.success) {
Expand All @@ -2273,6 +2278,47 @@ ready(function() {
return;
}

if (response.body.message && !flags.get('lm:switcher:' + window.btoa(uri), false)) {
// confirm before proceeding
flags.warning({
message: response.body.message,
callback: function(response, content) {
var confirm = content.find('[data-g-delete-confirm]'),
cancel = content.find('[data-g-delete-cancel]');

if (!confirm) { return; }

confirm.on('click', function(e) {
e.preventDefault();
if (this.attribute('disabled')) { return false; }

flags.get('lm:switcher:' + window.btoa(uri), true);
$([confirm, cancel]).attribute('disabled');
body.emit('mousedown', { target: element });

modal.close();
});

cancel.on('click', function(e) {
e.preventDefault();
if (this.attribute('disabled')) { return false; }

$([confirm, cancel]).attribute('disabled');
flags.get('lm:switcher:' + window.btoa(uri), false);

modal.close();
if (SWITCHER_HIT) {
setTimeout(function(){
SWITCHER_HIT.getPopover().show();
}, 5);
}
});
}
});

return false;
}

var preset = response.body.preset || 'default',
preset_name = response.body.title || 'Default',
structure = response.body.data,
Expand Down Expand Up @@ -2459,7 +2505,7 @@ module.exports = {
savestate: savestate
};

},{"../ui":46,"../ui/popover":48,"../utils/field-validation":57,"../utils/get-ajax-suffix":59,"../utils/get-ajax-url":60,"../utils/history":63,"../utils/save-state":65,"./builder":20,"./history":22,"./layoutmanager":24,"./particles-sidebar":25,"agent":66,"elements/attributes":88,"elements/domready":91,"elements/zen":96,"mout/array/contains":126,"mout/collection/forEach":147,"mout/collection/size":149,"mout/string/trim":220}],24:[function(require,module,exports){
},{"../ui":46,"../ui/popover":48,"../utils/field-validation":57,"../utils/flags-state":58,"../utils/get-ajax-suffix":59,"../utils/get-ajax-url":60,"../utils/history":63,"../utils/save-state":65,"./builder":20,"./history":22,"./layoutmanager":24,"./particles-sidebar":25,"agent":66,"elements/attributes":88,"elements/domready":91,"elements/zen":96,"mout/array/contains":126,"mout/collection/forEach":147,"mout/collection/size":149,"mout/string/trim":220}],24:[function(require,module,exports){
"use strict";
var prime = require('prime'),
$ = require('../utils/elements.utils'),
Expand Down Expand Up @@ -12626,22 +12672,36 @@ var FlagsState = new prime({
afterclose = options.afterclose || function() {},
warningURL = parseAjaxURI(options.url || getAjaxURL('unsaved') + getAjaxSuffix());

modal.open({
content: 'Loading...',
remote: warningURL,
remoteLoaded: function(response, modal) {
var content = modal.elements.content;
if (!callback) { return; }
if (options.url) {
modal.open({
content: 'Loading...',
remote: warningURL,
remoteLoaded: function(response, modal) {
var content = modal.elements.content;
if (!callback) { return; }

callback.call(this, response, content, modal);
},
afterClose: afterclose || function() {}
});
callback.call(this, response, content, modal);
},
afterClose: afterclose || function() {}
});
} else {
modal.open({
content: options.message,
afterOpen: function(response, modal) {
var content = modal.elements.content;
if (!callback) { return; }

callback.call(this, response, content, modal);
},
afterClose: afterclose || function() {}
});
}
}

});

module.exports = new FlagsState();

},{"../ui":46,"./get-ajax-suffix":59,"./get-ajax-url":60,"prime":242,"prime/emitter":241,"prime/map":243}],59:[function(require,module,exports){
"use strict";
var getAjaxSuffix = function() {
Expand Down
13 changes: 13 additions & 0 deletions platforms/common/templates/ajax/particles-loss.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div class="card settings-block">
<h4>Particles Loss</h4>
<div class="inner-params">
The section{{ particles|length > 1 ? 's' : '' }} <strong>{{ particles|join("</strong>, <strong>")|raw }}</strong> do{{ particles|length == 1 ? 'es' : '' }} not exist in the Layout that is about to get loaded. Any particles within those sections will be lost.
<br /><br />
Do you want to continue?
</div>
</div>

<div class="g-modal-actions">
<a class="button button-primary" data-g-delete-confirm>Continue</a>
<a class="button g5-dialog-close" data-g-delete-cancel>Cancel</a>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public function switchLayout($id)
$input = $this->request->post->getJson('layout');
$deleted = isset($input) ? $layout->clearSections()->copySections($input): [];
$message = $deleted
? sprintf('Warning: Following sections could not be found from the new layout: %s.', implode(', ', $deleted))
? $this->container['admin.theme']->render('@gantry-admin/ajax/particles-loss.html.twig', ['particles' => $deleted])
: null;

return new JsonResponse([
Expand Down Expand Up @@ -274,7 +274,7 @@ public function preset($id)
$input = $this->request->post->getJson('layout');
$deleted = isset($input) ? $layout->clearSections()->copySections($input): [];
$message = $deleted
? sprintf('Warning: Following sections could not be found from the new layout: %s.', implode(', ', $deleted))
? $this->container['admin.theme']->render('@gantry-admin/ajax/particles-loss.html.twig', ['particles' => $deleted])
: null;

return new JsonResponse([
Expand Down

0 comments on commit bfa1e01

Please sign in to comment.