-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprezento.js
204 lines (156 loc) · 5.55 KB
/
prezento.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
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
(function () {
'use strict';
var root = window,
events = 'ready cursor step notes'.split(' '),
actions = 'init goTo prev next first last toggleMultimedia toggleOverview'.split(' '),
eventsAndActions = events.concat(actions);
var utils = {
parseMsg: function (msgEvt, callback) {
// Message event data type must be an array with length >= 1
if (!Array.isArray(msgEvt.data) || msgEvt.data.length < 1) {
return;
}
// Message event data array first item must be an event or an action
if (eventsAndActions.indexOf(msgEvt.data[0]) === -1) {
return;
}
callback(msgEvt.data[0], msgEvt.data.slice(1));
},
ucfirst: function (str) {
return str[0].toUpperCase() + str.slice(1);
},
postMsg: function (target, eventOrAction, args) {
// Transform function arguments iterable object to array
var argsArray = Array.prototype.slice.call(args),
msgData = [eventOrAction].concat(argsArray);
target.postMessage(msgData, '*');
},
getMetas: function () {
var metasDomList = document.querySelectorAll('meta'),
metasArray = Array.prototype.slice.call(metasDomList),
metasObj = {};
metasArray.forEach(function (meta) {
if (meta.name && meta.content) {
metasObj[meta.name] = meta.content;
}
});
return metasObj;
},
mergeProps: function (mergedObject, newStuffsObject) {
for (var prop in newStuffsObject) {
mergedObject[prop] = newStuffsObject[prop];
}
}
};
// Expose prezento as global
root.prezento = {
createSlideDeckProxy: function (initArg) {
var iframe,
target,
eventListeners = {},
slideDeckProxy = {};
// initArg can be a CSS selector for an iframe element
if (typeof initArg === 'string') {
initArg = document.querySelector(initArg);
}
// initArg can be an iframe element
if (initArg && initArg.contentWindow != null) {
iframe = initArg;
initArg = initArg.contentWindow;
}
// initArg can be an object with a postMessage method (window or iframe contentWindow)
if (initArg && initArg.postMessage != null) {
target = initArg;
} else {
// initArgs is incorrect
return null;
}
// Configure event listeners with config object
slideDeckProxy.configListeners = function (listenersConfig) {
utils.mergeProps(eventListeners, listenersConfig);
// Add load listener on slide deck when shell configures ready listener
if (listenersConfig.hasOwnProperty('ready')) {
(iframe || target).addEventListener('load', function () {
slideDeckProxy.init();
}, false);
}
return slideDeckProxy;
};
// Expose all actions as direct methods
actions.forEach(function (action) {
slideDeckProxy[action] = function () {
utils.postMsg(target, action, arguments);
return slideDeckProxy;
};
});
root.addEventListener('message', function (msgEvt) {
utils.parseMsg(msgEvt, function (event, args) {
var listener = eventListeners[event];
if (listener) {
listener.apply(null, args);
}
});
}, false);
return slideDeckProxy;
},
createShellProxy: function (slideDeckInfos) {
var target,
actionListeners = {},
eventArgsGetters = {},
shellProxy = {};
if (slideDeckInfos &&
Array.isArray(slideDeckInfos.steps) &&
Array.isArray(slideDeckInfos.features)) {
} else {
// slideDeckInfos is incorrect
return null;
}
// Additionnal automatic informations
slideDeckInfos.title = document.title;
slideDeckInfos.metas = utils.getMetas();
// Configure action listeners with config object
shellProxy.configListeners = function (listenersConfig) {
utils.mergeProps(actionListeners, listenersConfig);
return shellProxy;
};
// Configure getters for event arguments with config object
shellProxy.configGetters = function (gettersConfig) {
utils.mergeProps(eventArgsGetters, gettersConfig);
return shellProxy;
};
// Expose all events as direct methods "notify[eventName]()"
events.forEach(function (event) {
shellProxy['notify' + utils.ucfirst(event)] = function () {
utils.postMsg(target, event, arguments);
return shellProxy;
};
});
root.addEventListener('message', function (msgEvt) {
utils.parseMsg(msgEvt, function (action, args) {
// Getters for cursor and step must be configured
if (eventArgsGetters.cursor && eventArgsGetters.step) {
target = target || msgEvt.source;
if (action === 'init') {
shellProxy.notifyReady(slideDeckInfos);
}
var listener = actionListeners[action];
if (listener) {
listener.apply(null, args);
}
shellProxy.notifyCursor(eventArgsGetters.cursor());
shellProxy.notifyStep(eventArgsGetters.step());
if (eventArgsGetters.notes) {
shellProxy.notifyNotes(eventArgsGetters.notes());
}
}
});
}, false);
return shellProxy;
},
// Expose util functions for unit tests
_utils: utils,
// Expose actions and events functions for unit tests
_events: events,
_actions: actions
};
})();