-
Notifications
You must be signed in to change notification settings - Fork 13
/
publish.js
162 lines (146 loc) · 6.05 KB
/
publish.js
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
/*
* Copyright (c) 2011-2014 by Animatron.
* All rights are reserved.
*/
/* Variables we get from template:
playerDomain — a URL to player domain host, e.g. player.animatron.com
amazonDomain — a URL to snapshot storage host, e.g. http://snapshots.animatron.com/<snapshot-id>
width — width of the animation
height — height of the animation
playerVersion — player version, default 'latest'
filename — filename of snapshot, used as amazonDomain + filename
autostart — (boolean) autostart of movie on player load
loop — (boolean) loop animation instead of stoping at the end
animation — JSON object of animatron movie (currently not used)
*/
(function($global, $window, $document) {
var inIFrame = ($window.self !== $window.top);
var autostart = $global['autostart'] || false,
loop = $global['loop'] || false;
var utils = {
isInt: function(n) {
n = Number(n);
return !isNaN(n) && Math.floor(n) === n;
},
serializeToQueryString: function(obj) {
var str = [];
for(var p in obj) {
if (obj.hasOwnProperty(p)) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
}
}
return str.join("&");
},
parseQueryString: function() {
var queryString = location.search.substring(1);
if (!queryString) {
return {};
}
var queries = queryString.split("&"),
params = {}, temp, i, l;
for ( i = 0, l = queries.length; i < l; i++ ) {
temp = queries[i].split('=');
params[temp[0]] = temp[1];
}
return params;
},
getRequiredRect: function () {
if (inIFrame) {
return utils.getIframeSize();
} else {
return {w: width, h: height};
}
},
getIframeSize: function () {
var size = {w:0, h:0};
if (typeof $window.innerWidth == 'number') {
size.w = $window.innerWidth;
size.h = $window.innerHeight;
} else {
size.w = $document.documentElement.clientWidth;
size.h = $document.documentElement.clientHeight;
}
return size;
},
forcedJS: function (path, then) {
var scriptElm = $document.createElement('script');
scriptElm.type = 'text/javascript';
scriptElm.async = 'async';
scriptElm.src = path;
scriptElm.onload = scriptElm.onreadystatechange = (function () {
var success = false;
return function () {
if (this.readyState === 'loading') {
return;
}
if (!success && (!this.readyState ||
(this.readyState === 'complete' || this.readyState === 'loaded')
)) {
success = true;
then();
} else if (!success && $window.console) {
console.error('Request failed: ' + this.readyState);
}
};
})();
var headElm = $document.head || $document.getElementsByTagName('head')[0];
headElm.appendChild(scriptElm);
}
};
var TARGET_ID = 'target',
WRAPPER_CLASS = 'anm-wrapper',
PLAYER_VERSION_ID = playerVersion || 'latest';
// options to apply just _after_ the snapshot was completely loaded are stored in the url (width and height in `params`)
// initialization options are stored in `initOptions`
var params = utils.parseQueryString(),
initOptions = {},
minified = params.nomin ? false : true;
var rect = utils.getRequiredRect(),
targetWidth, targetHeight;
//floating-point w&h parameters mean percentage sizing
if (params.w && !utils.isInt(params.w)) {
targetWidth = params.w = Math.round(params.w * rect.w);
} else {
targetWidth = params.w = params.w || rect.w;
}
if (params.h && !utils.isInt(params.h)) {
targetHeight = params.h = Math.round(params.h * rect.h);
} else {
targetHeight = params.h = params.h || rect.h;
}
if (autostart) initOptions.autoPlay = true;
if (loop) initOptions.repeat = true;
if (params.ver || params.version) { PLAYER_VERSION_ID = (params.ver || params.version); }
if (params.stretch) initOptions.stretchToCanvas = true;
var snapshotUrl = amazonDomain + '/' + filename + '?' +
utils.serializeToQueryString(params);
var thumbnailUrl = amazonDomain + '/' + filename.slice(0, filename.indexOf('.')) + '.jpg';
initOptions.thumbnail = thumbnailUrl;
var start = function () {
try {
if (!inIFrame) {
$document.body.className ='no-iframe';
} else {
$document.body.style.overflow = 'hidden';
}
var target = $document.getElementById(TARGET_ID);
target.style.width = targetWidth + 'px';
target.style.height = targetHeight + 'px';
target.style.marginLeft = -Math.floor(targetWidth / 2) + 'px';
target.style.marginTop = -Math.floor(window.innerHeight / 2) + 'px';
target.style.position = 'absolute';
target.style.left = '50%';
target.style.top = '50%';
utils.forcedJS('//' + playerDomain + '/' + PLAYER_VERSION_ID + '/bundle/animatron' + (minified ? '.min' : '') + '.js',
function () {
var player = anm.Player.forSnapshot(TARGET_ID, snapshotUrl, anm.importers.create('animatron'), $window.actions, initOptions);
if (anm.interop && anm.interop.playerjs) {
anm.interop.playerjs.setPlayer(player);
}
});
} catch (e) {
if($window.console) console.error(e);
}
};
$window.start = start;
})(this, window, document);