{"version":3,"file":"usertours.min.js","sources":["../src/usertours.js"],"sourcesContent":["/**\n * User tour control library.\n *\n * @module     tool_usertours/usertours\n * @class      usertours\n * @package    tool_usertours\n * @copyright  2016 Andrew Nicols <andrew@nicols.co.uk>\n */\ndefine(\n['core/ajax', 'tool_usertours/tour', 'jquery', 'core/templates', 'core/str', 'core/log', 'core/notification'],\nfunction(ajax, BootstrapTour, $, templates, str, log, notification) {\n    var usertours = {\n        tourId: null,\n\n        currentTour: null,\n\n        context: null,\n\n        /**\n         * Initialise the user tour for the current page.\n         *\n         * @method  init\n         * @param   {Number}    tourId      The ID of the tour to start.\n         * @param   {Bool}      startTour   Attempt to start the tour now.\n         * @param   {Number}    context     The context of the current page.\n         */\n        init: function(tourId, startTour, context) {\n            // Only one tour per page is allowed.\n            usertours.tourId = tourId;\n\n            usertours.context = context;\n\n            if (typeof startTour === 'undefined') {\n                startTour = true;\n            }\n\n            if (startTour) {\n                // Fetch the tour configuration.\n                usertours.fetchTour(tourId);\n            }\n\n            usertours.addResetLink();\n            // Watch for the reset link.\n            $('body').on('click', '[data-action=\"tool_usertours/resetpagetour\"]', function(e) {\n                e.preventDefault();\n                usertours.resetTourState(usertours.tourId);\n            });\n        },\n\n        /**\n         * Fetch the configuration specified tour, and start the tour when it has been fetched.\n         *\n         * @method  fetchTour\n         * @param   {Number}    tourId      The ID of the tour to start.\n         */\n        fetchTour: function(tourId) {\n            M.util.js_pending('admin_usertour_fetchTour' + tourId);\n            $.when(\n                ajax.call([\n                    {\n                        methodname: 'tool_usertours_fetch_and_start_tour',\n                        args: {\n                            tourid:     tourId,\n                            context:    usertours.context,\n                            pageurl:    window.location.href,\n                        }\n                    }\n                ])[0],\n                templates.render('tool_usertours/tourstep', {})\n            )\n            .then(function(response, template) {\n                // If we don't have any tour config (because it doesn't need showing for the current user), return early.\n                if (!response.hasOwnProperty('tourconfig')) {\n                    return;\n                }\n\n                return usertours.startBootstrapTour(tourId, template[0], response.tourconfig);\n            })\n            .always(function() {\n                M.util.js_complete('admin_usertour_fetchTour' + tourId);\n\n                return;\n            })\n            .fail(notification.exception);\n        },\n\n        /**\n         * Add a reset link to the page.\n         *\n         * @method  addResetLink\n         */\n        addResetLink: function() {\n            var ele;\n            M.util.js_pending('admin_usertour_addResetLink');\n\n            // Append the link to the most suitable place on the page\n            // with fallback to legacy selectors and finally the body\n            // if there is no better place.\n            if ($('.tool_usertours-resettourcontainer').length) {\n                ele = $('.tool_usertours-resettourcontainer');\n            } else if ($('.logininfo').length) {\n                ele = $('.logininfo');\n            } else if ($('footer').length) {\n                ele = $('footer');\n            } else {\n                ele = $('body');\n            }\n            templates.render('tool_usertours/resettour', {})\n            .then(function(html, js) {\n                templates.appendNodeContents(ele, html, js);\n\n                return;\n            })\n            .always(function() {\n                M.util.js_complete('admin_usertour_addResetLink');\n\n                return;\n            })\n            .fail();\n        },\n\n        /**\n         * Start the specified tour.\n         *\n         * @method  startBootstrapTour\n         * @param   {Number}    tourId      The ID of the tour to start.\n         * @param   {String}    template    The template to use.\n         * @param   {Object}    tourConfig  The tour configuration.\n         * @return  {Object}\n         */\n        startBootstrapTour: function(tourId, template, tourConfig) {\n            if (usertours.currentTour) {\n                // End the current tour, but disable end tour handler.\n                tourConfig.onEnd = null;\n                usertours.currentTour.endTour();\n                delete usertours.currentTour;\n            }\n\n            // Normalize for the new library.\n            tourConfig.eventHandlers = {\n                afterEnd: [usertours.markTourComplete],\n                afterRender: [usertours.markStepShown],\n            };\n\n            // Sort out the tour name.\n            tourConfig.tourName = tourConfig.name;\n            delete tourConfig.name;\n\n            // Add the template to the configuration.\n            // This enables translations of the buttons.\n            tourConfig.template = template;\n\n            tourConfig.steps = tourConfig.steps.map(function(step) {\n                if (typeof step.element !== 'undefined') {\n                    step.target = step.element;\n                    delete step.element;\n                }\n\n                if (typeof step.reflex !== 'undefined') {\n                    step.moveOnClick = !!step.reflex;\n                    delete step.reflex;\n                }\n\n                if (typeof step.content !== 'undefined') {\n                    step.body = step.content;\n                    delete step.content;\n                }\n\n                return step;\n            });\n\n            usertours.currentTour = new BootstrapTour(tourConfig);\n            return usertours.currentTour.startTour();\n        },\n\n        /**\n         * Mark the specified step as being shownd by the user.\n         *\n         * @method  markStepShown\n         */\n        markStepShown: function() {\n            var stepConfig = this.getStepConfig(this.getCurrentStepNumber());\n            $.when(\n                ajax.call([\n                    {\n                        methodname: 'tool_usertours_step_shown',\n                        args: {\n                            tourid:     usertours.tourId,\n                            context:    usertours.context,\n                            pageurl:    window.location.href,\n                            stepid:     stepConfig.stepid,\n                            stepindex:  this.getCurrentStepNumber(),\n                        }\n                    }\n                ])[0]\n            ).fail(log.error);\n        },\n\n        /**\n         * Mark the specified tour as being completed by the user.\n         *\n         * @method  markTourComplete\n         */\n        markTourComplete: function() {\n            var stepConfig = this.getStepConfig(this.getCurrentStepNumber());\n            $.when(\n                ajax.call([\n                    {\n                        methodname: 'tool_usertours_complete_tour',\n                        args: {\n                            tourid:     usertours.tourId,\n                            context:    usertours.context,\n                            pageurl:    window.location.href,\n                            stepid:     stepConfig.stepid,\n                            stepindex:  this.getCurrentStepNumber(),\n                        }\n                    }\n                ])[0]\n            ).fail(log.error);\n        },\n\n        /**\n         * Reset the state, and restart the the tour on the current page.\n         *\n         * @method  resetTourState\n         * @param   {Number}    tourId      The ID of the tour to start.\n         */\n        resetTourState: function(tourId) {\n            $.when(\n                ajax.call([\n                    {\n                        methodname: 'tool_usertours_reset_tour',\n                        args: {\n                            tourid:     tourId,\n                            context:    usertours.context,\n                            pageurl:    window.location.href,\n                        }\n                    }\n                ])[0]\n            ).then(function(response) {\n                if (response.startTour) {\n                    usertours.fetchTour(response.startTour);\n                }\n                return;\n            }).fail(notification.exception);\n        }\n    };\n\n    return /** @alias module:tool_usertours/usertours */ {\n        /**\n         * Initialise the user tour for the current page.\n         *\n         * @method  init\n         * @param   {Number}    tourId      The ID of the tour to start.\n         * @param   {Bool}      startTour   Attempt to start the tour now.\n         */\n        init: usertours.init,\n\n        /**\n         * Reset the state, and restart the the tour on the current page.\n         *\n         * @method  resetTourState\n         * @param   {Number}    tourId      The ID of the tour to restart.\n         */\n        resetTourState: usertours.resetTourState\n    };\n});\n"],"names":["define","ajax","BootstrapTour","$","templates","str","log","notification","usertours","tourId","currentTour","context","init","startTour","fetchTour","addResetLink","on","e","preventDefault","resetTourState","M","util","js_pending","when","call","methodname","args","tourid","pageurl","window","location","href","render","then","response","template","hasOwnProperty","startBootstrapTour","tourconfig","always","js_complete","fail","exception","ele","length","html","js","appendNodeContents","tourConfig","onEnd","endTour","eventHandlers","afterEnd","markTourComplete","afterRender","markStepShown","tourName","name","steps","map","step","element","target","reflex","moveOnClick","content","body","stepConfig","this","getStepConfig","getCurrentStepNumber","stepid","stepindex","error"],"mappings":"AAQAA,kCACA,CAAC,YAAa,sBAAuB,SAAU,iBAAkB,WAAY,WAAY,sBACzF,SAASC,KAAMC,cAAeC,EAAGC,UAAWC,IAAKC,IAAKC,kBAC9CC,UAAY,CACZC,OAAQ,KAERC,YAAa,KAEbC,QAAS,KAUTC,KAAM,SAASH,OAAQI,UAAWF,SAE9BH,UAAUC,OAASA,OAEnBD,UAAUG,QAAUA,aAEK,IAAdE,YACPA,WAAY,GAGZA,WAEAL,UAAUM,UAAUL,QAGxBD,UAAUO,eAEVZ,EAAE,QAAQa,GAAG,QAAS,gDAAgD,SAASC,GAC3EA,EAAEC,iBACFV,UAAUW,eAAeX,UAAUC,YAU3CK,UAAW,SAASL,QAChBW,EAAEC,KAAKC,WAAW,2BAA6Bb,QAC/CN,EAAEoB,KACEtB,KAAKuB,KAAK,CACN,CACIC,WAAY,sCACZC,KAAM,CACFC,OAAYlB,OACZE,QAAYH,UAAUG,QACtBiB,QAAYC,OAAOC,SAASC,SAGrC,GACH3B,UAAU4B,OAAO,0BAA2B,KAE/CC,MAAK,SAASC,SAAUC,aAEhBD,SAASE,eAAe,qBAItB5B,UAAU6B,mBAAmB5B,OAAQ0B,SAAS,GAAID,SAASI,eAErEC,QAAO,WACJnB,EAAE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