summaryrefslogtreecommitdiff
blob: b19c43046ac210fad24aabf456aac16243fa81bb (plain)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
var wmpAppBanner = wmpAppBanner || {};
wmpAppBanner.WIDGET = wmpAppBanner.WIDGET || {};

wmpAppBanner.WIDGET.appUrl = wmpAppBanner.WIDGET.appUrl || '';
wmpAppBanner.WIDGET.appIcon = wmpAppBanner.WIDGET.appIcon || '';
wmpAppBanner.WIDGET.appName = wmpAppBanner.WIDGET.appName || '';
wmpAppBanner.WIDGET.ref = wmpAppBanner.WIDGET.ref || '';
wmpAppBanner.WIDGET.trustedDevice = wmpAppBanner.WIDGET.trustedDevice || 0;
wmpAppBanner.WIDGET.iframeUrl = wmpAppBanner.WIDGET.iframeUrl || '';
wmpAppBanner.WIDGET.cssPath = wmpAppBanner.WIDGET.cssPath || '';
wmpAppBanner.WIDGET.openAppButton = wmpAppBanner.WIDGET.openAppButton || 'OPEN';

(function () {

        /**
         * @class Cookie
         * @constructor
         */
        var Cookie = function () {
            this.initialize();
        };

        var p = Cookie.prototype;

        /**
         *
         * Initialization method.
         * @method initialize
         */
        p.initialize = function () {

        };

        /**
         * Get cookie (public method)
         *
         * @param c_name
         * @returns {string}
         */
        p.get = function (c_name) {

            var i, x, y, ARRcookies = document.cookie.split(";");
            for (i = 0; i < ARRcookies.length; i++) {
                x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
                y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
                x = x.replace(/^\s+|\s+$/g, "");
                if (x == c_name) {
                    return decodeURIComponent(y);
                }
            }
        };

        /**
         * Set cookie (public method)
         *
         * @param c_name
         * @param value
         * @param expireDays
         */
        p.set = function (c_name, value, expireDays) {
            var expireDate = new Date();
            expireDate.setDate(expireDate.getDate() + expireDays);

            var c_value = encodeURIComponent(value) + ((expireDays == null) ? "" : "; expires=" + expireDate.toUTCString()) + "; path=/;";
            document.cookie = c_name + "=" + c_value;
        };

        wmpAppBanner.Cookie = Cookie;
    }()
);

(function () {

    /**
     * @class Bar
     * @constructor
     */
    var Bar = function (options) {
        this.initialize(options);
    };

    var p = Bar.prototype;

    /**
     * Public properties
     */

    /**
     * The DOM object to manage.
     * @property htmlElement
     * @type HTMLElement
     */
    p.htmlElement = null;
    p.iframe = null;
    p.iframeUrl = null;

    /**
     * Initialization method.
     * @method initialize
     */
    p.initialize = function (options) {

        var me = this;
        this.createWrapper(options);

        // add orientation change event
        if ('onorientationchange' in window) {

            // orientation change event functions
            this.orientationchangeFn = function () {
                setTimeout(function () {
                    me.resize();
                }, 250);
            };
            window.addEventListener("orientationchange", this.orientationchangeFn, false);
        }
        else {
            var mqOrientation = window.matchMedia("(orientation: portrait)");

            // The Listener will fire whenever this either matches or ceases to match
            mqOrientation.addListener(function (media) {
                // portrait
                if (media.matches) {
                    me.resize("portrait");
                }
                else {
                    me.resize("landscape");
                }
            }, false);
        }

        // add zoom event
        this.touchendFn = function () {
            clearTimeout(window.resizeEvt);
            window.resizeEvt = setTimeout(function () {
                //alert("resize");
                me.resize();
            }, 300);
        };

        window.resizeEvt;
        window.addEventListener("touchleave", this.touchendFn, false);
        window.addEventListener("touchcancel", this.touchendFn, false);
        window.addEventListener("touchmove", this.touchendFn, false);
        window.addEventListener("touchend", this.touchendFn, false);
    };


    /**
     * Create wrapper (public method)
     * @param options
     */
    p.createWrapper = function (options) {
        var wrapper = document.createElement("appticles-wrapper");
        this.htmlElement = wrapper;
        var height = Math.round(this.getHeight());
        var shadow = Math.round(0.1 * height);

        wrapper.style.position = "fixed";
        wrapper.style.width = "100%";
        wrapper.style.height = "0px";
        wrapper.style.top = "0px";
        wrapper.style.left = "0px";
        wrapper.style.boxSizing = "border-box";
        wrapper.style.zIndex = 1000000;
        wrapper.style.display = "block";
        wrapper.style.height = (height + shadow) + "px";

        document.body.appendChild(wrapper);

        var appName = options.appName || "";
        var appUrl = options.appUrl || "";
        var appIcon = options.appIcon || "";
        var cssPath = options.cssPath || "";
        var iframeUrl = this.iframeUrl = options.iframeUrl || "";
        var originUrl = window.location.href;
        var openAppButton = options.openAppButton || "";

        var _dc = (new Date()).getTime();
        var params = "cssPath=" + cssPath + "?_dc" + _dc;
        params += "&amp;height=" + height;
        params += "&amp;appName=" + encodeURIComponent(appName);
        params += "&amp;appUrl=" + encodeURIComponent(appUrl);
        params += "&amp;appIcon=" + appIcon;
        params += "&amp;originUrl=" + encodeURIComponent(originUrl);
        params += "&amp;openText=" + encodeURIComponent(openAppButton);

        var html = [
            '<iframe id="appticles-iframe-bar" width="100%" height="' + (height + shadow) + 'px" src="' + iframeUrl + '#' + params + '" frameborder="0" allowtransparency="true" scrolling="no" style="position:relative;"></iframe>'
        ].join("");

        wrapper.innerHTML = html;

        this.iframe = document.getElementById("appticles-iframe-bar");
    };


    /**
     * Resize banner (public method)
     *
     * @param orientation
     */
    p.resize = function (orientation) {

        var wrapper = this.htmlElement;
        if (wrapper == null) return;

        // compute new wrapper height
        var newH = Math.round(this.getHeight());
        var shadow = Math.round(0.1 * newH);
        wrapper.style.height = (newH + shadow) + "px";

        // change iframe params
        var params = "height=" + newH;
        this.iframe.style.height = (newH + shadow) + "px";
        this.iframe.src = this.iframeUrl + '#' + params;
    };


    /**
     * Get height (public method)
     *
     * @param orientation
     * @returns {number}
     */
    p.getHeight = function (orientation) {

        orientation = orientation || this.getOrientation();

        var screenWidth, screenHeight, windowWidth, newH;

        // resize wrapper
        if (orientation == "portrait") {
            screenWidth = Math.min(screen.width, screen.height);
            screenHeight = Math.max(screen.width, screen.height);
            windowWidth = window.innerWidth;
            newH = 90 * (windowWidth / screenWidth);

            return newH;
        }
        else if (orientation == "landscape") {
            screenWidth = Math.max(screen.width, screen.height);
            screenHeight = Math.min(screen.width, screen.height);
            windowWidth = window.innerWidth;
            newH = (90 * (windowWidth / screenHeight)) * (screenHeight / screenWidth);

            return newH;
        }
    };

    /**
     * Destroy bar (public method)
     */
    p.destroy = function () {

        // remove the bar from HTML
        var wrapper = this.htmlElement;
        wrapper.parentNode.removeChild(wrapper);
        this.htmlElement = null;

        // remove orientation change event
        if ('onorientationchange' in window) {
            window.removeEventListener("orientationchange", this.orientationchangeFn);
        }

        // remove touch end event
        window.removeEventListener("touchleave", this.touchendFn);
        window.removeEventListener("touchcancel", this.touchendFn);
        window.removeEventListener("touchmove", this.touchendFn);
        window.removeEventListener("touchend", this.touchendFn);

        window[this] = null;
        delete this;
    };


    /**
     * Get device orientation
     *
     * @return string (portrait | landscape)
     */
    p.getOrientation = function () {
        if (window.matchMedia("(orientation: portrait)").matches) {
            return "portrait";
        }
        else if (window.matchMedia("(orientation: landscape)").matches) {
            return "landscape";
        }
    };

    wmpAppBanner.Bar = Bar;
}());

wmpAppBanner = wmpAppBanner || {};

(function () {

    /**
     * @class Stage
     * @constructor
     */
    var Stage = function (WIDGET) {
        this.WIDGET = WIDGET;

        this.initialize(WIDGET);
    };

    var p = Stage.prototype;

    /**
     * Public properties
     */
    p.cookie = null;
    p.WIDGET = null;
    p.bar = null;


    /**
     * Initialization method.
     * @method initialize
     */
    p.initialize = function () {

        // create cookie obj
        this.cookie = new wmpAppBanner.Cookie();
    };


    /**
     * Detect device
     * @todo (Future releases) Add device detection using user agent
     */
    p.detectDevice = function () {

        var me = this;

        if (me.WIDGET.trustedDevice == 1){

            me.redirectFn(1);
            return;
        }

        me.redirectFn(0);
    };


    /**
     * Set mobile device cookie and call createBar() method
     *
     * @param isAllowedDevice
     *
     */
    p.redirectFn = function (isAllowedDevice) {

        var WIDGET = this.WIDGET;
        var cookie = this.cookie;
        cookie.set(WIDGET.cookiePrefix + "mobile_device", isAllowedDevice, 7);

        if (Boolean(Number(String(isAllowedDevice)))) {
            this.createBar();
        }
    };


    /**
     * Create bar
     */
    p.createBar = function () {

        // wait until the document body is created
        var me = this;
        var DOMLoadTimer = setInterval(function () {
            if (document.body && document.body.clientWidth != 0) {
                clearInterval(DOMLoadTimer);

                me.bar = new wmpAppBanner.Bar({
                    appIcon: me.WIDGET.appIcon,
                    appName: me.WIDGET.appName,
                    appUrl: me.WIDGET.appUrl,
                    cssPath: me.WIDGET.cssPath,
                    iframeUrl: me.WIDGET.iframeUrl,
                    openAppButton: me.WIDGET.openAppButton
                });
            }
        }, 10);
    };

    /**
     *
     * Redirect to app
     *
     */
    p.openApp = function () {

        var WIDGET = this.WIDGET;
        var mobileUrl = WIDGET.ref;

        document.cookie = WIDGET.cookiePrefix + "redirect=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/;";

        // redirect to the app
        if (mobileUrl && mobileUrl.length > 0) {
            window.location.href = mobileUrl;
        }
    };

    wmpAppBanner.Stage = Stage;
}());

(function() {

    var appticlesStage, stage;

    /**
     * Create timer that will check if the document is ready
     * @type {number}
     */
    var DOMLoadTimer = setInterval(function () {
        if (/loading|loaded|complete/i.test(document.readyState)) {
            clearInterval(DOMLoadTimer);
            documentLoaded();
        }
    }, 10);

    /**
     * Init method, called when the document is ready
     *
     * The 'redirect' GET param is used for hosted apps (on Appticles).
     * Setting redirect=false will deactivate the app banner.
     *
     */
    function documentLoaded() {

        // create stage
        appticlesStage = stage = new wmpAppBanner.Stage(wmpAppBanner.WIDGET);

        // get saved cookies
        var cookie = stage.cookie,
            mobileDevice = cookie.get(wmpAppBanner.WIDGET.cookiePrefix + "mobile_device"),
            redirect = cookie.get(wmpAppBanner.WIDGET.cookiePrefix + "redirect"),
            closed = cookie.get(wmpAppBanner.WIDGET.cookiePrefix + "closed"),
            appUrl = wmpAppBanner.WIDGET.appUrl;

        // if there was a previous detection and the device is mobile
        if (mobileDevice && Boolean(Number(String(mobileDevice))) == true && appUrl && appUrl.length > 1){

            // if there is a cookie already set, then convert it to a boolean value
            // redirect param is used for hosted apps (on Appticles)
            redirect = (redirect != null) ? Boolean(Number(String(redirect))) : true;

            var urlParams = window.location.href.split("?");

            // if the URL contains a redirect param, then set up a cookie with this value
            if (urlParams.length > 1){
                if (urlParams[urlParams.length-1].indexOf("redirect=false") != -1){
                    cookie.set(wmpAppBanner.WIDGET.cookiePrefix + "redirect", 0, 7);
                    redirect = false;
                }
                else if (urlParams[urlParams.length-1].indexOf("redirect=true") != -1){
                    cookie.set(wmpAppBanner.WIDGET.cookiePrefix + "redirect", 1, 7);
                    redirect = true;
                }
            }

            // create the wrapper bar
            if (redirect && !closed){

                // attach on hash change listener
                window.onhashchange = onHashChange;

                // create wrapper bar
                stage.createBar();
            }

            return;
        }
        // if there was a previous detection and the device is a desktop one
        else if (mobileDevice && Boolean(Number(String(mobileDevice))) == false){
            return;
        }
        else if (window.location.href.indexOf("redirect=false") != -1){
            return;
        }

        window.onhashchange = onHashChange;

        // detect device
        stage.detectDevice();
    }

    /**
     * The hashchange event fires when a window's hash changes (location.hash).
     * The hash is used for opening the app or hiding the banner (using close button from iframe).
     *
     */
    function onHashChange(){

        var params = window.location.hash.split("#")[1];

        if (params){
            params = params.split("=");

            if (params[0] == "app_action"){

                switch (params[1]){

                    case "closebar":
                        // set cookie
                        appticlesStage.cookie.set(wmpAppBanner.WIDGET.cookiePrefix + "closed", 1, 7);

                        // remove the bar
                        setTimeout(function(){
                            appticlesStage.bar.destroy();
                            appticlesStage = null;
                        }, 300);

                        window.onhashchange = null;
                        window.location.hash = "";
                        break;

                    case "openapp":
                        window.onhashchange = null;
                        window.location.hash = "";
                        appticlesStage.openApp();
                        break;

                    default: break;
                }
            }
        }
    }

}());