summaryrefslogtreecommitdiff
blob: f7e012378b9b8b6810dc05611a7f7a68e3139815 (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
wmpAppBanner = wmpAppBanner || {};

(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 += "&height=" + height;
        params += "&appName=" + encodeURIComponent(appName);
        params += "&appUrl=" + encodeURIComponent(appUrl);
        params += "&appIcon=" + appIcon;
        params += "&originUrl=" + encodeURIComponent(originUrl);
        params += "&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;
}());