File size: 4,431 Bytes
1abfaca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*global define, amplify*/
define([
    'jquery',
    'loglevel',
    'introjs',
    'underscore',
    'amplify'
], function ($,
             log,
             introJs,
             _
) {

    'use strict';

    var s = {
    },

    defaultOptions = {
        storageKey: 'onboarding_',
        interval: 1000,
        intervalForce: 350,
        failures: 0,
        maxFailures: 4
    };

    function OnBoarding() {
        this.intro = new introJs();
        return this;
    }

    OnBoarding.prototype.setOptions = function (options) {

        this.o = $.extend(true, {}, defaultOptions, options);

        if (this.o.id === undefined) {
            log.error("OnBoarding.setOptions; id is undefined");
        }

        this.o.storageKey = this.o.storageKey + this.o.id;

    };

    OnBoarding.prototype.start = function (force) {

        var self = this,
            force = force || false;

        this.intro.oncomplete(function () {
            self._onComplete();
        });

        this.intro.onexit(function () {
            self._onExit();
        });

        // TODO: check if it is skipped and not exit

        if (this.o == undefined) {
            log.error("OnBoarding.start; options are empty");
        }

        // filter visible steps
        var checkElements = setInterval(function () {

            var steps = self._filterVisibleSteps();
            if (steps.length === self.o.steps.length) {

                log.info("OnBoarding.start; starting", steps.length === self.o.steps.length);

                // putting the filters steps as steps to use
                self.o.steps = steps;

                // setting the options
                self.intro.setOptions(self.o);

                self._startReady(force);
                clearInterval(checkElements);
            } else {

                if (self.o.failures++ >= self.o.maxFailures) {
                    // exit
                    log.error("OnBoarding.start; reached max failures", self.o.maxFailures);
                    clearInterval(checkElements);
                }
                log.warn("OnBoarding.start; failures", self.o.failures)
            }

        }, force === true ? this.o.intervalForce : this.o.interval);


    };

    OnBoarding.prototype._startReady = function (force) {

        if (this.isSeen() === undefined || force === true) {
            if (!this._isAlreadyOnTour()) {
                this.intro.start();
            } else {
                log.error("OnBoarding._startReady; Another onboarding is already visible.");
            }

        }

    };

    /* TODO: check if it is skipped and not exit */
    OnBoarding.prototype._onSkip = function () {
        amplify.store(this.o.storageKey, "skip");
    };

    OnBoarding.prototype._isAlreadyOnTour = function () {
        return $(".introjs-overlay").is(":visible");
    };

    /*  the user exit the tour, but didn't skip it */
    OnBoarding.prototype._onExit = function () {
        // TODO: in theory on exit should not be saved the onboarding
        amplify.store(this.o.storageKey, "exit");
        log.info("OnBoarding.onExit; has been saved", this.isSeen()? true: false)
    };

    /* the user completed the tour, so should not be annoyed anymore */
    OnBoarding.prototype._onComplete = function () {
        amplify.store(this.o.storageKey, "complete");
        log.info("OnBoarding.onExit; has been saved", this.isSeen()? true: false)
    };

    /* check if the used seen the tour */
    OnBoarding.prototype.isSeen = function () {
        return amplify.store(this.o.storageKey);
    };

    OnBoarding.prototype._filterVisibleSteps = function () {

        var r = [],
            steps = this.o.steps;

        for(var i=0; i < steps.length; i++) {

            var element = this._getElement(steps[i]);

            if ( element !== undefined && element.is(':visible')) {
                r.push($.extend(true, {}, steps[i], {element: element[0]}));
            }
        }

        return r;
    };

    OnBoarding.prototype._getElement = function (step) {
        var element = step.element,
            target = step.target;

        if (target !== undefined) {
            return target.find(element);
        }

        return $(element);

    };

    OnBoarding.prototype.destroy = function () {

        if (this.intro !== undefined) {
            this.intro.exit();
        }

    };

    return OnBoarding;
});