-
Notifications
You must be signed in to change notification settings - Fork 2
/
extension.js
executable file
·127 lines (106 loc) · 4.46 KB
/
extension.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
/**
* An Openframe extension which loops through a users collection on a timed duration.
*/
var pjson = require('./package.json'),
debug = require('debug')('openframe:slideshow'),
Extension = require('openframe-extension');
// default duration in minutes
var DEFAULT_DURATION = 1;
/**
* Extension initialization method.
*
* Called when the extension (and its dependencies) have been installed.
*
* @param {object} OF An interface provided to extensions giving limitted access to the frame environment
*/
module.exports = new Extension({
init: function(OF) {
// do your extension thing
debug('=======> Openframe-Slideshow initialized! <=======');
/**
* Extensions also have access to the global event system
*/
var pubsub = this.pubsub;
/**
* Extensions also have access to the Swagger REST client (https://github.com/swagger-api/swagger-js)
* See openframe.io/explorer for API docs, or openframe.io/explorer/swagger.json for the swagger definition
* which shows the available methods as 'operationId'
*/
var rest = this.rest;
/**
* Reference to the frame model wrapper, allowing plugin to update frame data.
* (frame.state is the model data)
*/
var frame = this.frame;
var duration = getDuration(frame.state, frame.state.current_artwork);
debug('duration', duration);
var timer = setTimeout(getNextFromCollection, duration);
/**
* Use the REST API to fetch the collection, then select a random artwork to display.
*/
function getNextFromCollection() {
debug('getNextFromCollection', duration);
// get the logged-in user's primary collection
rest.OpenframeUser.OpenframeUser_prototype_get_created_artwork({
id: 'current'
}).then(function(data) {
console.log(data);
var artworkList = data.obj,
len = artworkList.length,
currentArtworkId = frame.state.current_artwork && frame.state.current_artwork.id,
nextArtwork;
debug('currentArtworkId', currentArtworkId);
// cur idx
var idx = artworkList.findIndex(function(el) {
return currentArtworkId === el.id;
});
debug('idx', idx);
idx = idx === -1 ? 0 : idx;
// next idx
if (idx < len - 1) {
idx += 1;
} else {
idx = 0;
}
nextArtwork = artworkList[idx];
debug('nextArtwork', nextArtwork);
// allow artwork-specific durations
var duration = getDuration(frame.state, nextArtwork);
debug('duration', duration);
// frame.state is the plain JS object representing the frame's state...
frame.state.currentArtworkId = nextArtwork.id;
// then save the frame to the server. when the db is updated on the server, the server
// will trigger a 'frame updated' event, which will in turn update this frame, forcing
// the artwork to change to the new one we just set
frame.save()
.then(function() {
debug('Success...');
timer = setTimeout(getNextFromCollection, duration);
})
.catch(function(err) {
debug('ERROR: ', err);
});
}).catch(function(err) {
debug('ERROR: ', err);
});
}
function getDuration(frameState, artwork) {
var duration;
if (artwork.settings && artwork.settings[pjson.name] && artwork.settings[pjson.name].duration) {
duration = minToMillis(artwork.settings[pjson.name].duration);
} else if (frameState.settings && frameState.settings[pjson.name] && frameState.settings[pjson.name].duration) {
duration = minToMillis(frameState.settings[pjson.name].duration);
} else {
duration = minToMillis(DEFAULT_DURATION);
}
return duration;
}
function minToMillis(min) {
return min * 60000;
}
},
settings: {
duration: 1,
enabled: true
}
});