64f973428c5b17ea47a22cb05b287b9647e19fbc
[odoo/odoo.git] / addons / website / static / src / js / website.tour.js
1 (function () {
2     'use strict';
3
4     var website = openerp.website;
5     website.add_template_file('/website/static/src/xml/website.tour.xml');
6
7     website.Tour = openerp.Class.extend({
8         tour: undefined,
9         steps: [],
10         tourStorage: window.localStorage,
11         init: function () {
12             this.tour = new Tour({
13                 name: this.id,
14                 storage: this.tourStorage,
15                 keyboard: false,
16                 template: this.popover(),
17             });
18             this.tour.addSteps(_.map(this.steps, function (step) {
19                step.title = openerp.qweb.render('website.tour_popover_title', { title: step.title });
20                return step;
21             }));
22         },
23         reset: function () {
24             this.tourStorage.removeItem(this.id+'_current_step');
25             this.tourStorage.removeItem(this.id+'_end');
26             this.tour._current = 0;
27             $('.popover.tour').remove();
28         },
29         start: function () {
30             if (this.continue() || ((this.currentStepIndex() === 0) && !this.tour.ended())) {
31                 this.tour.start();
32             }
33         },
34         currentStepIndex: function () {
35             var index = this.tourStorage.getItem(this.id+'_current_step') || 0;
36             return parseInt(index, 10);
37         },
38         indexOfStep: function (stepId) {
39             var index = -1;
40             _.each(this.steps, function (step, i) {
41                if (step.stepId === stepId) {
42                    index = i;
43                }
44             });
45             return index;
46         },
47         isCurrentStep: function (stepId) {
48             return this.currentStepIndex() === this.indexOfStep(stepId);
49         },
50         movetoStep: function (stepId) {
51             $('.popover.tour').remove();
52             var index = this.indexOfStep(stepId);
53             if (index > -1) {
54                 this.tour.goto(index);
55             }
56         },
57         saveStep: function (stepId) {
58             var index = this.indexOfStep(stepId);
59             this.tourStorage.setItem(this.id+'_current_step', index);
60         },
61         stop: function () {
62             this.tour.end();
63         },
64         redirect: function (url) {
65             url = url || new website.UrlParser(window.location.href);
66             if (this.startPath && url.pathname !== this.startPath) {
67                 var newUrl = this.startPath + (url.search ? (url.search + "&") : "?") + this.id + "=true"
68                 window.location.replace(newUrl);
69             }
70         },
71         continue: function () {
72             // Override if necessary
73             return this.currentStepIndex() === 0;
74         },
75         trigger: function (url) {
76             // Override if necessary
77             url = url || new website.UrlParser(window.location.href);
78             var urlTrigger = this.id + "=true";
79             return url.search.indexOf(urlTrigger) >= 0;
80         },
81         testUrl: function (pattern) {
82             var url = new website.UrlParser(window.location.href);
83             return pattern.test(url.pathname+url.search);
84         },
85         popover: function (options) {
86             return openerp.qweb.render('website.tour_popover', options);
87         },
88     });
89
90     website.UrlParser = openerp.Class.extend({
91         init: function (url) {
92             var a = document.createElement('a');
93             a.href = url;
94             this.href = a.href;
95             this.host = a.host;
96             this.protocol = a.protocol;
97             this.port = a.port;
98             this.hostname = a.hostname;
99             this.pathname = a.pathname;
100             this.origin = a.origin;
101             this.search = a.search;
102             this.hash = a.hash;
103         },
104     });
105
106     website.EditorBar.include({
107         tours: [],
108         start: function () {
109             $('.tour-backdrop').click(function (e) {
110                 e.stopImmediatePropagation();
111                 e.preventDefault();
112             });
113             var url = new website.UrlParser(window.location.href);
114             var menu = $('#help-menu');
115             _.each(this.tours, function (tour) {
116                 var $menuItem = $($.parseHTML('<li><a href="#">'+tour.name+'</a></li>'));
117                 $menuItem.click(function () {
118                     tour.redirect(url);
119                     tour.reset();
120                     tour.start();
121                 });
122                 menu.append($menuItem);
123                 if (tour.trigger()) {
124                     tour.start();
125                 }
126             });
127             return this._super();
128         },
129         registerTour: function (tour) {
130             this.tours.push(tour);
131         },
132     });
133
134 }());