Skip to content

Commit

Permalink
Fix confirm cancel
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbieTheWagner committed Aug 18, 2016
1 parent dc11223 commit d822e9b
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 161 deletions.
72 changes: 44 additions & 28 deletions addon/services/tour.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Ember from 'ember';

const { Evented, K, Service, isPresent, run, $, isEmpty, observer } = Ember;

/**
Expand Down Expand Up @@ -58,15 +57,7 @@ export default Service.extend(Evented, {
},

cancel() {
if (!this.get('confirmCancel')) {
this.get('tourObject').cancel();
} else {
let cancelMessage = this.get('confirmCancelMessage')
|| 'Are you sure you want to stop the tour?';
if (confirm(cancelMessage)) {
this.get('tourObject').cancel();
}
}
this.get('tourObject').cancel();
},

next() {
Expand Down Expand Up @@ -166,24 +157,6 @@ export default Service.extend(Evented, {
}
},

/**
* Set computed styles on the cloned element
*
* @method setComputedStylesOnClonedElement
* @param element element we want to copy
* @param clonedElement cloned element above the overlay
* @private
*/
setComputedStylesOnClonedElement(element, clonedElement) {
let computedStyle = window.getComputedStyle(element, null);

for (let i = 0; i < computedStyle.length; i++) {
let propertyName = computedStyle[i];

clonedElement[0].style[propertyName] = computedStyle.getPropertyValue(propertyName);
}
},

/**
* Return the element for a step
*
Expand Down Expand Up @@ -213,6 +186,46 @@ export default Service.extend(Evented, {
return element;
},

/**
* Set computed styles on the cloned element
*
* @method setComputedStylesOnClonedElement
* @param element element we want to copy
* @param clonedElement cloned element above the overlay
* @private
*/
setComputedStylesOnClonedElement(element, clonedElement) {
let computedStyle = window.getComputedStyle(element, null);

for (let i = 0; i < computedStyle.length; i++) {
let propertyName = computedStyle[i];

clonedElement[0].style[propertyName] = computedStyle.getPropertyValue(propertyName);
}
},

/**
* This wraps the cancel function in a confirm dialog
* @param {boolean} confirmCancel Whether to show the dialog or not
* @param {string} confirmCancelMessage The message to display
* @param {object} tourObject The tour object
* @private
*/
wrapCancelFunction(confirmCancel, confirmCancelMessage, tourObject) {
let cancelFunction = tourObject.cancel;
if (confirmCancel) {
let cancelMessage = confirmCancelMessage || 'Are you sure you want to stop the tour?';

let newCancelFunction = function() {
let stopTour = confirm(cancelMessage);
if (stopTour) {
cancelFunction();
}
};
tourObject.cancel = newCancelFunction;
}
},

/**
* Get the element from an option object
*
Expand Down Expand Up @@ -251,6 +264,9 @@ export default Service.extend(Evented, {
defaults
});

// Allow for confirm cancel dialog
this.wrapCancelFunction(this.get('confirmCancel'), this.get('confirmCancelMessage'), tourObject);

tourObject.on('start', run.bind(this, 'onTourStart'));
tourObject.on('complete', run.bind(this, 'onTourComplete'));
tourObject.on('cancel', run.bind(this, 'onTourCancel'));
Expand Down
Loading

0 comments on commit d822e9b

Please sign in to comment.