| author | durandn |
| Fri, 18 Sep 2015 14:39:45 +0200 | |
| changeset 1049 | 4e8b3df6e5be |
| parent 1033 | c20df1c080e6 |
| child 1068 | 7623f9af9272 |
| permissions | -rw-r--r-- |
| 998 | 1 |
/* widgetsDefinition of an ancestor for the Widget classes */ |
|
875
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
2 |
|
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
3 |
if (typeof IriSP.Widgets === "undefined") { |
| 998 | 4 |
IriSP.Widgets = {}; |
|
875
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
5 |
} |
|
66
13013b9452af
Added a new file to the build, widgets.js, to contain widget classes.
hamidouk
parents:
diff
changeset
|
6 |
|
|
520
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
287
diff
changeset
|
7 |
/** |
| 868 | 8 |
* @class IriSP.Widget is an "abstract" class. It's mostly used to define some properties common to every widget. |
9 |
* |
|
10 |
* Note that widget constructors are never called directly by the user. Instead, the widgets are instantiated by functions |
|
11 |
* defined in init.js |
|
12 |
* |
|
13 |
* @constructor |
|
14 |
* @param player - a reference to the player widget |
|
15 |
* @param config - configuration options for the widget |
|
16 |
*/ |
|
|
875
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
17 |
|
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
18 |
|
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
19 |
IriSP.Widgets.Widget = function(player, config) { |
| 984 | 20 |
|
| 868 | 21 |
if( typeof player === "undefined") { |
22 |
/* Probably an abstract call of the class when |
|
23 |
* individual widgets set their prototype */ |
|
24 |
return; |
|
25 |
} |
|
26 |
|
|
| 984 | 27 |
this.__subwidgets = []; |
28 |
|
|
| 868 | 29 |
/* Setting all the configuration options */ |
|
1001
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
30 |
var _type = config.type || "(unknown)", |
|
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
31 |
_config = IriSP._.defaults({}, config, (player && player.config ? player.config.default_options : {}), this.defaults), |
| 868 | 32 |
_this = this; |
33 |
|
|
34 |
IriSP._(_config).forEach(function(_value, _key) { |
|
35 |
_this[_key] = _value; |
|
36 |
}); |
|
37 |
|
|
|
988
eefd336335f9
Some improvements: Hashtags can be used, links are now clickable, ....
veltr
parents:
986
diff
changeset
|
38 |
this.$ = IriSP.jQuery('#' + this.container); |
|
eefd336335f9
Some improvements: Hashtags can be used, links are now clickable, ....
veltr
parents:
986
diff
changeset
|
39 |
|
|
872
d777d05a16e4
finished AnnotationsList and started New PolemicWidget
veltr
parents:
870
diff
changeset
|
40 |
if (typeof this.width === "undefined") { |
|
988
eefd336335f9
Some improvements: Hashtags can be used, links are now clickable, ....
veltr
parents:
986
diff
changeset
|
41 |
this.width = this.$.width(); |
| 992 | 42 |
} else { |
43 |
this.$.css("width", this.width); |
|
44 |
} |
|
45 |
|
|
46 |
if (typeof this.height !== "undefined") { |
|
47 |
this.$.css("height", this.height); |
|
|
872
d777d05a16e4
finished AnnotationsList and started New PolemicWidget
veltr
parents:
870
diff
changeset
|
48 |
} |
|
d777d05a16e4
finished AnnotationsList and started New PolemicWidget
veltr
parents:
870
diff
changeset
|
49 |
|
| 868 | 50 |
/* Setting this.player at the end in case it's been overriden |
51 |
* by a configuration option of the same name :-( |
|
52 |
*/ |
|
|
1001
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
53 |
this.player = player || new IriSP.FakeClass(["on","trigger","off","loadWidget","loadMetadata"]); |
| 868 | 54 |
|
55 |
/* Adding classes and html attributes */ |
|
|
875
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
56 |
this.$.addClass("Ldt-TraceMe Ldt-Widget").attr("widget-type", _type); |
| 868 | 57 |
|
| 916 | 58 |
this.l10n = ( |
59 |
typeof this.messages[IriSP.language] !== "undefined" |
|
60 |
? this.messages[IriSP.language] |
|
61 |
: ( |
|
62 |
IriSP.language.length > 2 && typeof this.messages[IriSP.language.substr(0,2)] !== "undefined" |
|
63 |
? this.messages[IriSP.language.substr(0,2)] |
|
64 |
: this.messages["en"] |
|
65 |
) |
|
66 |
); |
|
|
875
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
67 |
|
| 986 | 68 |
/* Loading Metadata if required */ |
|
1001
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
69 |
|
|
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
70 |
function onsourceloaded() { |
|
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
71 |
if (_this.media_id) { |
|
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
72 |
_this.media = this.getElement(_this.media_id); |
|
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
73 |
} else { |
|
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
74 |
var _mediaopts = { |
|
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
75 |
is_mashup: _this.is_mashup || false |
| 1013 | 76 |
}; |
|
1001
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
77 |
_this.media = _this.source.getCurrentMedia(_mediaopts); |
|
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
78 |
} |
|
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
79 |
|
|
1049
4e8b3df6e5be
Added support for running a function before the draw() function of a widget, for example when a widget needs to fetch additional user informations that aren't provided by the metadatas
durandn
parents:
1033
diff
changeset
|
80 |
|
|
4e8b3df6e5be
Added support for running a function before the draw() function of a widget, for example when a widget needs to fetch additional user informations that aren't provided by the metadatas
durandn
parents:
1033
diff
changeset
|
81 |
if (_this.pre_draw_callback){ |
|
4e8b3df6e5be
Added support for running a function before the draw() function of a widget, for example when a widget needs to fetch additional user informations that aren't provided by the metadatas
durandn
parents:
1033
diff
changeset
|
82 |
IriSP.jQuery.when(_this.pre_draw_callback()).done(_this.draw()); |
|
4e8b3df6e5be
Added support for running a function before the draw() function of a widget, for example when a widget needs to fetch additional user informations that aren't provided by the metadatas
durandn
parents:
1033
diff
changeset
|
83 |
} |
|
4e8b3df6e5be
Added support for running a function before the draw() function of a widget, for example when a widget needs to fetch additional user informations that aren't provided by the metadatas
durandn
parents:
1033
diff
changeset
|
84 |
else { |
|
4e8b3df6e5be
Added support for running a function before the draw() function of a widget, for example when a widget needs to fetch additional user informations that aren't provided by the metadatas
durandn
parents:
1033
diff
changeset
|
85 |
_this.draw(); |
|
4e8b3df6e5be
Added support for running a function before the draw() function of a widget, for example when a widget needs to fetch additional user informations that aren't provided by the metadatas
durandn
parents:
1033
diff
changeset
|
86 |
} |
|
1001
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
87 |
_this.player.trigger("widget-loaded"); |
|
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
88 |
} |
| 986 | 89 |
|
90 |
if (this.metadata) { |
|
91 |
/* Getting metadata */ |
|
92 |
this.source = player.loadMetadata(this.metadata); |
|
93 |
|
|
94 |
/* Call draw when loaded */ |
|
|
1001
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
95 |
this.source.onLoad(onsourceloaded); |
| 986 | 96 |
} else { |
|
1001
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
97 |
if (this.source) { |
|
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
98 |
onsourceloaded(); |
|
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
99 |
} |
| 986 | 100 |
} |
101 |
|
|
102 |
|
|
|
66
13013b9452af
Added a new file to the build, widgets.js, to contain widget classes.
hamidouk
parents:
diff
changeset
|
103 |
}; |
| 74 | 104 |
|
| 998 | 105 |
IriSP.Widgets.Widget.prototype.defaults = {}; |
|
875
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
106 |
|
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
107 |
IriSP.Widgets.Widget.prototype.template = ''; |
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
108 |
|
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
109 |
IriSP.Widgets.Widget.prototype.messages = {"en":{}}; |
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
110 |
|
|
1001
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
111 |
IriSP.Widgets.Widget.prototype.toString = function() { |
|
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
112 |
return "Widget " + this.type; |
|
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
113 |
}; |
|
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
114 |
|
|
875
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
115 |
IriSP.Widgets.Widget.prototype.templateToHtml = function(_template) { |
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
116 |
return Mustache.to_html(_template, this); |
| 998 | 117 |
}; |
|
875
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
118 |
|
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
119 |
IriSP.Widgets.Widget.prototype.renderTemplate = function() { |
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
120 |
this.$.append(this.templateToHtml(this.template)); |
| 998 | 121 |
}; |
|
875
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
122 |
|
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
123 |
IriSP.Widgets.Widget.prototype.functionWrapper = function(_name) { |
|
870
2c025db10a10
Migrated playerWidget and started annotationsListWidget
veltr
parents:
868
diff
changeset
|
124 |
var _this = this, |
|
2c025db10a10
Migrated playerWidget and started annotationsListWidget
veltr
parents:
868
diff
changeset
|
125 |
_function = this[_name]; |
|
2c025db10a10
Migrated playerWidget and started annotationsListWidget
veltr
parents:
868
diff
changeset
|
126 |
if (typeof _function !== "undefined") { |
|
2c025db10a10
Migrated playerWidget and started annotationsListWidget
veltr
parents:
868
diff
changeset
|
127 |
return function() { |
|
2c025db10a10
Migrated playerWidget and started annotationsListWidget
veltr
parents:
868
diff
changeset
|
128 |
return _function.apply(_this, Array.prototype.slice.call(arguments, 0)); |
| 1013 | 129 |
}; |
|
870
2c025db10a10
Migrated playerWidget and started annotationsListWidget
veltr
parents:
868
diff
changeset
|
130 |
} else { |
| 1013 | 131 |
console.log("Error, Unknown function IriSP.Widgets." + this.type + "." + _name); |
| 957 | 132 |
} |
| 998 | 133 |
}; |
| 957 | 134 |
|
135 |
IriSP.Widgets.Widget.prototype.getFunctionOrName = function(_functionOrName) { |
|
136 |
switch (typeof _functionOrName) { |
|
137 |
case "function": |
|
138 |
return _functionOrName; |
|
139 |
case "string": |
|
140 |
return this.functionWrapper(_functionOrName); |
|
141 |
default: |
|
142 |
return undefined; |
|
|
870
2c025db10a10
Migrated playerWidget and started annotationsListWidget
veltr
parents:
868
diff
changeset
|
143 |
} |
| 998 | 144 |
}; |
|
870
2c025db10a10
Migrated playerWidget and started annotationsListWidget
veltr
parents:
868
diff
changeset
|
145 |
|
| 957 | 146 |
IriSP.Widgets.Widget.prototype.onMdpEvent = function(_eventName, _functionOrName) { |
147 |
this.player.on(_eventName, this.getFunctionOrName(_functionOrName)); |
|
| 998 | 148 |
}; |
| 957 | 149 |
|
150 |
IriSP.Widgets.Widget.prototype.onMediaEvent = function(_eventName, _functionOrName) { |
|
151 |
this.media.on(_eventName, this.getFunctionOrName(_functionOrName)); |
|
| 998 | 152 |
}; |
|
870
2c025db10a10
Migrated playerWidget and started annotationsListWidget
veltr
parents:
868
diff
changeset
|
153 |
|
| 876 | 154 |
IriSP.Widgets.Widget.prototype.getWidgetAnnotations = function() { |
| 1033 | 155 |
var result = null; |
| 982 | 156 |
if (typeof this.annotation_type === "undefined") { |
| 1033 | 157 |
result = this.media.getAnnotations(); |
158 |
} else if (this.annotation_type.elementType === "annotationType") { |
|
159 |
result = this.annotation_type.getAnnotations(); |
|
160 |
} else { |
|
161 |
result = this.media.getAnnotationsByTypeTitle(this.annotation_type); |
|
| 982 | 162 |
} |
| 1033 | 163 |
if (typeof this.annotation_filter !== "undefined") { |
164 |
return this.annotation_filter(result); |
|
165 |
} else { |
|
166 |
return result; |
|
| 982 | 167 |
} |
| 998 | 168 |
}; |
| 876 | 169 |
|
| 922 | 170 |
IriSP.Widgets.Widget.prototype.getWidgetAnnotationsAtTime = function() { |
| 957 | 171 |
var _time = this.media.getCurrentTime(); |
| 922 | 172 |
return this.getWidgetAnnotations().filter(function(_annotation) { |
173 |
return _annotation.begin <= _time && _annotation.end > _time; |
|
174 |
}); |
|
| 998 | 175 |
}; |
| 922 | 176 |
|
| 984 | 177 |
IriSP.Widgets.Widget.prototype.isLoaded = function() { |
178 |
var isloaded = !IriSP._(this.__subwidgets).any(function(w) { |
|
179 |
return !(w && w.isLoaded()); |
|
180 |
}); |
|
181 |
return isloaded; |
|
| 998 | 182 |
}; |
| 984 | 183 |
|
| 959 | 184 |
IriSP.Widgets.Widget.prototype.insertSubwidget = function(_selector, _widgetoptions, _propname) { |
| 924 | 185 |
var _id = _selector.attr("id"), |
| 927 | 186 |
_this = this, |
187 |
_type = _widgetoptions.type, |
|
| 984 | 188 |
$L = $LAB, |
189 |
key = this.__subwidgets.length; |
|
190 |
this.__subwidgets.push(null); |
|
| 924 | 191 |
if (typeof _id == "undefined") { |
192 |
_id = IriSP._.uniqueId(this.container + '_sub_widget_' + _widgetoptions.type); |
|
193 |
_selector.attr("id", _id); |
|
194 |
} |
|
195 |
_widgetoptions.container = _id; |
|
| 927 | 196 |
if (typeof IriSP.widgetsRequirements[_type] !== "undefined" && typeof IriSP.widgetsRequirements[_type].requires !== "undefined" ) { |
197 |
for (var _j = 0; _j < IriSP.widgetsRequirements[_type].requires.length; _j++) { |
|
198 |
$L.script(IriSP.getLib(IriSP.widgetsRequirements[_type].requires[_j])); |
|
199 |
} |
|
200 |
} |
|
201 |
$L.wait(function() { |
|
202 |
_this.player.loadWidget(_widgetoptions, function(_widget) { |
|
| 959 | 203 |
if (_propname) { |
204 |
_this[_propname] = _widget; |
|
205 |
} |
|
| 984 | 206 |
_this.__subwidgets[key] = _widget; |
| 927 | 207 |
}); |
| 924 | 208 |
}); |
| 998 | 209 |
}; |
| 924 | 210 |
|
| 1033 | 211 |
/* |
212 |
* Position the player to the next/previous annotations based on current player position |
|
213 |
* |
|
214 |
* Parameter: offset: -1 for previous annotation, +1 for next annotation |
|
215 |
*/ |
|
216 |
IriSP.Widgets.Widget.prototype.navigate = function(offset) { |
|
217 |
// offset is normally either -1 (previous slide) or +1 (next slide) |
|
218 |
var _this = this; |
|
219 |
var currentTime = _this.media.getCurrentTime(); |
|
220 |
var annotations = _this.source.getAnnotations().sortBy(function(_annotation) { |
|
221 |
return _annotation.begin; |
|
222 |
}); |
|
223 |
for (var i = 0; i < annotations.length; i++) { |
|
224 |
if (annotations[i].begin <= currentTime && currentTime < annotations[i].end) { |
|
225 |
// Found a current annotation - clamp i+offset value to [0, length - 1] |
|
226 |
i = Math.min(annotations.length - 1, Math.max(0, i + offset)); |
|
227 |
_this.media.setCurrentTime(annotations[i].begin); |
|
228 |
break; |
|
229 |
} |
|
230 |
}; |
|
231 |
}; |
|
232 |
||
233 |
||
|
520
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
287
diff
changeset
|
234 |
/** |
| 868 | 235 |
* This method responsible of drawing a widget on screen. |
236 |
*/ |
|
|
875
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
237 |
IriSP.Widgets.Widget.prototype.draw = function() { |
| 868 | 238 |
/* implemented by "sub-classes" */ |
| 1033 | 239 |
}; |