2016-08-11 22:05:23 +08:00
|
|
|
/*
|
2016-08-26 04:26:53 +08:00
|
|
|
* Converts JSON data received from the server to flat array of values.
|
2016-08-11 22:05:23 +08:00
|
|
|
*/
|
2016-08-05 23:00:29 +08:00
|
|
|
function jsonToValuesArray(jsonData) {
|
2016-08-31 23:52:45 +08:00
|
|
|
var errMsgs = [];
|
2016-08-05 23:00:29 +08:00
|
|
|
for (var key in jsonData) {
|
|
|
|
var values = jsonData[key];
|
2016-08-31 23:52:45 +08:00
|
|
|
$.each(values, function (idx, val) {
|
|
|
|
errMsgs.push(val);
|
|
|
|
});
|
2016-08-05 23:00:29 +08:00
|
|
|
}
|
|
|
|
return errMsgs;
|
|
|
|
}
|
2016-08-26 04:26:53 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Calls callback function on AJAX success (because built-in functions don't
|
|
|
|
* work!)
|
|
|
|
*/
|
|
|
|
$.fn.onAjaxComplete = function (cb) {
|
2016-08-31 23:52:45 +08:00
|
|
|
$(this)
|
|
|
|
.on('ajax:success', function () {
|
|
|
|
cb();
|
|
|
|
})
|
|
|
|
.on('ajax:error', function () {
|
|
|
|
cb();
|
|
|
|
});
|
2016-08-26 04:26:53 +08:00
|
|
|
}
|
2016-11-14 21:27:35 +08:00
|
|
|
|
|
|
|
var TUTORIAL_STEPS_CNT = 20;
|
|
|
|
|
|
|
|
/**
|
2016-11-16 03:12:40 +08:00
|
|
|
* Initializes tutorial steps for the current page.
|
|
|
|
* NOTE: If some steps edit page, then this function needs to be called several
|
|
|
|
* times for the same page, but for different steps.
|
2016-11-14 21:27:35 +08:00
|
|
|
* @param {number} pageFirstStep Page's first step
|
|
|
|
* @param {number} pageLastStep Page's last step
|
|
|
|
* @param {string} nextPagePath Next page absolute path
|
2016-11-16 03:12:40 +08:00
|
|
|
* @param {function} beforeCb Callback called before the tutorial starts
|
|
|
|
* @param {function} endCb Callback called after the tutorial ends
|
2016-11-16 02:30:17 +08:00
|
|
|
* @param {object} steps Optional JSON containing introJs steps. They can be
|
|
|
|
* specified here, or hardcoded in HTML.
|
2016-11-14 21:27:35 +08:00
|
|
|
*/
|
|
|
|
function initPageTutorialSteps(pageFirstStep, pageLastStep, nextPagePath,
|
2016-11-16 03:12:40 +08:00
|
|
|
beforeCb, endCb, steps) {
|
2016-11-14 21:27:35 +08:00
|
|
|
var tutorialData = Cookies.get('tutorial_data');
|
|
|
|
if (tutorialData) {
|
|
|
|
tutorialData = JSON.parse(tutorialData);
|
|
|
|
var stepNum = parseInt(Cookies.get('current_tutorial_step'), 10);
|
|
|
|
if (isNaN(stepNum)) {
|
|
|
|
stepNum = 1;
|
|
|
|
Cookies.set('current_tutorial_step', stepNum);
|
2016-11-16 02:30:17 +08:00
|
|
|
tutorialData[0].backPagesPaths = [];
|
|
|
|
Cookies.set('tutorial_data', tutorialData);
|
2016-11-14 21:27:35 +08:00
|
|
|
}
|
2016-11-16 02:30:17 +08:00
|
|
|
var thisPagePath = window.location.pathname;
|
2016-11-14 21:27:35 +08:00
|
|
|
beforeCb();
|
|
|
|
|
|
|
|
// Initialize tutorial for the current page's steps
|
|
|
|
var doneLabel = (pageLastStep === TUTORIAL_STEPS_CNT) ?
|
|
|
|
'Start using sciNote' : 'End tutorial';
|
2016-11-16 02:30:17 +08:00
|
|
|
if (_.isUndefined(steps)) {
|
|
|
|
introJs()
|
|
|
|
.setOptions({
|
|
|
|
overlayOpacity: '0.2',
|
|
|
|
prevLabel: 'Back',
|
|
|
|
nextLabel: 'Next',
|
|
|
|
skipLabel: 'End tutorial',
|
|
|
|
doneLabel: doneLabel,
|
|
|
|
showBullets: false,
|
|
|
|
showStepNumbers: false,
|
|
|
|
exitOnOverlayClick: false,
|
|
|
|
exitOnEsc: false,
|
|
|
|
disableInteraction: true,
|
|
|
|
tooltipClass: 'custom next-page-link'
|
|
|
|
})
|
|
|
|
.goToStep(stepNum - (pageFirstStep - 1))
|
|
|
|
.start();
|
|
|
|
} else {
|
|
|
|
introJs()
|
|
|
|
.setOptions({
|
|
|
|
overlayOpacity: '0.2',
|
|
|
|
prevLabel: 'Back',
|
|
|
|
nextLabel: 'Next',
|
|
|
|
skipLabel: 'End tutorial',
|
|
|
|
doneLabel: doneLabel,
|
|
|
|
showBullets: false,
|
|
|
|
showStepNumbers: false,
|
|
|
|
exitOnOverlayClick: false,
|
|
|
|
exitOnEsc: false,
|
|
|
|
disableInteraction: true,
|
|
|
|
tooltipClass: 'custom next-page-link',
|
|
|
|
steps: steps
|
|
|
|
})
|
|
|
|
.goToStep(stepNum - (pageFirstStep - 1))
|
|
|
|
.start();
|
|
|
|
}
|
2016-11-14 21:27:35 +08:00
|
|
|
|
|
|
|
// Page navigation when coming to this page from previous/next page
|
|
|
|
$(function() {
|
|
|
|
if (stepNum === pageFirstStep && stepNum > 1) {
|
|
|
|
$('.introjs-prevbutton').removeClass('introjs-disabled');
|
2016-11-16 02:30:17 +08:00
|
|
|
} else if (stepNum === pageLastStep && stepNum < TUTORIAL_STEPS_CNT) {
|
2016-11-14 21:27:35 +08:00
|
|
|
$('.introjs-nextbutton').removeClass('introjs-disabled');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Page navigation when already on this page
|
|
|
|
|
|
|
|
$('.introjs-skipbutton').click(function() {
|
|
|
|
Cookies.remove('tutorial_data');
|
|
|
|
Cookies.remove('current_tutorial_step');
|
|
|
|
|
2016-11-16 03:12:40 +08:00
|
|
|
endCb();
|
2016-11-14 21:27:35 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
$('.introjs-prevbutton').click(function() {
|
|
|
|
if (stepNum > 1) {
|
|
|
|
Cookies.set('current_tutorial_step', --stepNum);
|
|
|
|
|
|
|
|
if (stepNum === pageFirstStep && stepNum > 1) {
|
|
|
|
$('.introjs-prevbutton').removeClass('introjs-disabled');
|
|
|
|
} else if (stepNum < pageFirstStep) {
|
2016-11-16 02:30:17 +08:00
|
|
|
// Go to previous page;
|
|
|
|
|
|
|
|
var prevPagePath = tutorialData[0].backPagesPaths.pop();
|
|
|
|
Cookies.set('tutorial_data', tutorialData);
|
|
|
|
$('.introjs-prevbutton').attr('href', prevPagePath);
|
2016-11-16 03:12:40 +08:00
|
|
|
endCb();
|
2016-11-14 21:27:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$('.introjs-nextbutton').click(function() {
|
|
|
|
if (stepNum < TUTORIAL_STEPS_CNT) {
|
|
|
|
Cookies.set('current_tutorial_step', ++stepNum);
|
|
|
|
|
|
|
|
if (stepNum === pageLastStep && stepNum < TUTORIAL_STEPS_CNT) {
|
|
|
|
$('.introjs-nextbutton').removeClass('introjs-disabled');
|
|
|
|
} else if (stepNum > pageLastStep) {
|
2016-11-16 02:30:17 +08:00
|
|
|
// Go to next page
|
2016-11-14 21:27:35 +08:00
|
|
|
|
2016-11-16 02:30:17 +08:00
|
|
|
tutorialData[0].backPagesPaths.push(thisPagePath);
|
2016-11-14 21:27:35 +08:00
|
|
|
Cookies.set('tutorial_data', tutorialData);
|
|
|
|
$('.introjs-nextbutton').attr('href', nextPagePath);
|
2016-11-16 03:12:40 +08:00
|
|
|
endCb();
|
2016-11-14 21:27:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|