| author | ymh <ymh.work@gmail.com> |
| Fri, 13 Feb 2015 16:57:53 +0100 | |
| changeset 1033 | c20df1c080e6 |
| parent 1013 | 392ddcd212d7 |
| child 1049 | 4e8b3df6e5be |
| 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 |
|
|
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
80 |
_this.draw(); |
|
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
81 |
_this.player.trigger("widget-loaded"); |
|
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
82 |
} |
| 986 | 83 |
|
84 |
if (this.metadata) { |
|
85 |
/* Getting metadata */ |
|
86 |
this.source = player.loadMetadata(this.metadata); |
|
87 |
|
|
88 |
/* Call draw when loaded */ |
|
|
1001
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
89 |
this.source.onLoad(onsourceloaded); |
| 986 | 90 |
} else { |
|
1001
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
91 |
if (this.source) { |
|
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
92 |
onsourceloaded(); |
|
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
93 |
} |
| 986 | 94 |
} |
95 |
|
|
96 |
|
|
|
66
13013b9452af
Added a new file to the build, widgets.js, to contain widget classes.
hamidouk
parents:
diff
changeset
|
97 |
}; |
| 74 | 98 |
|
| 998 | 99 |
IriSP.Widgets.Widget.prototype.defaults = {}; |
|
875
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
100 |
|
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
101 |
IriSP.Widgets.Widget.prototype.template = ''; |
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
102 |
|
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
103 |
IriSP.Widgets.Widget.prototype.messages = {"en":{}}; |
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
104 |
|
|
1001
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
105 |
IriSP.Widgets.Widget.prototype.toString = function() { |
|
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
106 |
return "Widget " + this.type; |
|
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
107 |
}; |
|
3210bf928a11
Enabled loading widgets without the widgeting framework
veltr
parents:
998
diff
changeset
|
108 |
|
|
875
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
109 |
IriSP.Widgets.Widget.prototype.templateToHtml = function(_template) { |
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
110 |
return Mustache.to_html(_template, this); |
| 998 | 111 |
}; |
|
875
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
112 |
|
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
113 |
IriSP.Widgets.Widget.prototype.renderTemplate = function() { |
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
114 |
this.$.append(this.templateToHtml(this.template)); |
| 998 | 115 |
}; |
|
875
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
116 |
|
|
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
117 |
IriSP.Widgets.Widget.prototype.functionWrapper = function(_name) { |
|
870
2c025db10a10
Migrated playerWidget and started annotationsListWidget
veltr
parents:
868
diff
changeset
|
118 |
var _this = this, |
|
2c025db10a10
Migrated playerWidget and started annotationsListWidget
veltr
parents:
868
diff
changeset
|
119 |
_function = this[_name]; |
|
2c025db10a10
Migrated playerWidget and started annotationsListWidget
veltr
parents:
868
diff
changeset
|
120 |
if (typeof _function !== "undefined") { |
|
2c025db10a10
Migrated playerWidget and started annotationsListWidget
veltr
parents:
868
diff
changeset
|
121 |
return function() { |
|
2c025db10a10
Migrated playerWidget and started annotationsListWidget
veltr
parents:
868
diff
changeset
|
122 |
return _function.apply(_this, Array.prototype.slice.call(arguments, 0)); |
| 1013 | 123 |
}; |
|
870
2c025db10a10
Migrated playerWidget and started annotationsListWidget
veltr
parents:
868
diff
changeset
|
124 |
} else { |
| 1013 | 125 |
console.log("Error, Unknown function IriSP.Widgets." + this.type + "." + _name); |
| 957 | 126 |
} |
| 998 | 127 |
}; |
| 957 | 128 |
|
129 |
IriSP.Widgets.Widget.prototype.getFunctionOrName = function(_functionOrName) { |
|
130 |
switch (typeof _functionOrName) { |
|
131 |
case "function": |
|
132 |
return _functionOrName; |
|
133 |
case "string": |
|
134 |
return this.functionWrapper(_functionOrName); |
|
135 |
default: |
|
136 |
return undefined; |
|
|
870
2c025db10a10
Migrated playerWidget and started annotationsListWidget
veltr
parents:
868
diff
changeset
|
137 |
} |
| 998 | 138 |
}; |
|
870
2c025db10a10
Migrated playerWidget and started annotationsListWidget
veltr
parents:
868
diff
changeset
|
139 |
|
| 957 | 140 |
IriSP.Widgets.Widget.prototype.onMdpEvent = function(_eventName, _functionOrName) { |
141 |
this.player.on(_eventName, this.getFunctionOrName(_functionOrName)); |
|
| 998 | 142 |
}; |
| 957 | 143 |
|
144 |
IriSP.Widgets.Widget.prototype.onMediaEvent = function(_eventName, _functionOrName) { |
|
145 |
this.media.on(_eventName, this.getFunctionOrName(_functionOrName)); |
|
| 998 | 146 |
}; |
|
870
2c025db10a10
Migrated playerWidget and started annotationsListWidget
veltr
parents:
868
diff
changeset
|
147 |
|
| 876 | 148 |
IriSP.Widgets.Widget.prototype.getWidgetAnnotations = function() { |
| 1033 | 149 |
var result = null; |
| 982 | 150 |
if (typeof this.annotation_type === "undefined") { |
| 1033 | 151 |
result = this.media.getAnnotations(); |
152 |
} else if (this.annotation_type.elementType === "annotationType") { |
|
153 |
result = this.annotation_type.getAnnotations(); |
|
154 |
} else { |
|
155 |
result = this.media.getAnnotationsByTypeTitle(this.annotation_type); |
|
| 982 | 156 |
} |
| 1033 | 157 |
if (typeof this.annotation_filter !== "undefined") { |
158 |
return this.annotation_filter(result); |
|
159 |
} else { |
|
160 |
return result; |
|
| 982 | 161 |
} |
| 998 | 162 |
}; |
| 876 | 163 |
|
| 922 | 164 |
IriSP.Widgets.Widget.prototype.getWidgetAnnotationsAtTime = function() { |
| 957 | 165 |
var _time = this.media.getCurrentTime(); |
| 922 | 166 |
return this.getWidgetAnnotations().filter(function(_annotation) { |
167 |
return _annotation.begin <= _time && _annotation.end > _time; |
|
168 |
}); |
|
| 998 | 169 |
}; |
| 922 | 170 |
|
| 984 | 171 |
IriSP.Widgets.Widget.prototype.isLoaded = function() { |
172 |
var isloaded = !IriSP._(this.__subwidgets).any(function(w) { |
|
173 |
return !(w && w.isLoaded()); |
|
174 |
}); |
|
175 |
return isloaded; |
|
| 998 | 176 |
}; |
| 984 | 177 |
|
| 959 | 178 |
IriSP.Widgets.Widget.prototype.insertSubwidget = function(_selector, _widgetoptions, _propname) { |
| 924 | 179 |
var _id = _selector.attr("id"), |
| 927 | 180 |
_this = this, |
181 |
_type = _widgetoptions.type, |
|
| 984 | 182 |
$L = $LAB, |
183 |
key = this.__subwidgets.length; |
|
184 |
this.__subwidgets.push(null); |
|
| 924 | 185 |
if (typeof _id == "undefined") { |
186 |
_id = IriSP._.uniqueId(this.container + '_sub_widget_' + _widgetoptions.type); |
|
187 |
_selector.attr("id", _id); |
|
188 |
} |
|
189 |
_widgetoptions.container = _id; |
|
| 927 | 190 |
if (typeof IriSP.widgetsRequirements[_type] !== "undefined" && typeof IriSP.widgetsRequirements[_type].requires !== "undefined" ) { |
191 |
for (var _j = 0; _j < IriSP.widgetsRequirements[_type].requires.length; _j++) { |
|
192 |
$L.script(IriSP.getLib(IriSP.widgetsRequirements[_type].requires[_j])); |
|
193 |
} |
|
194 |
} |
|
195 |
$L.wait(function() { |
|
196 |
_this.player.loadWidget(_widgetoptions, function(_widget) { |
|
| 959 | 197 |
if (_propname) { |
198 |
_this[_propname] = _widget; |
|
199 |
} |
|
| 984 | 200 |
_this.__subwidgets[key] = _widget; |
| 927 | 201 |
}); |
| 924 | 202 |
}); |
| 998 | 203 |
}; |
| 924 | 204 |
|
| 1033 | 205 |
/* |
206 |
* Position the player to the next/previous annotations based on current player position |
|
207 |
* |
|
208 |
* Parameter: offset: -1 for previous annotation, +1 for next annotation |
|
209 |
*/ |
|
210 |
IriSP.Widgets.Widget.prototype.navigate = function(offset) { |
|
211 |
// offset is normally either -1 (previous slide) or +1 (next slide) |
|
212 |
var _this = this; |
|
213 |
var currentTime = _this.media.getCurrentTime(); |
|
214 |
var annotations = _this.source.getAnnotations().sortBy(function(_annotation) { |
|
215 |
return _annotation.begin; |
|
216 |
}); |
|
217 |
for (var i = 0; i < annotations.length; i++) { |
|
218 |
if (annotations[i].begin <= currentTime && currentTime < annotations[i].end) { |
|
219 |
// Found a current annotation - clamp i+offset value to [0, length - 1] |
|
220 |
i = Math.min(annotations.length - 1, Math.max(0, i + offset)); |
|
221 |
_this.media.setCurrentTime(annotations[i].begin); |
|
222 |
break; |
|
223 |
} |
|
224 |
}; |
|
225 |
}; |
|
226 |
||
227 |
||
|
520
fe008e95a716
added jsdoc support, and a script to generate the docs.
hamidouk
parents:
287
diff
changeset
|
228 |
/** |
| 868 | 229 |
* This method responsible of drawing a widget on screen. |
230 |
*/ |
|
|
875
43629caa77bc
Big refactoring of widget files + started migration of segmentwidget
veltr
parents:
874
diff
changeset
|
231 |
IriSP.Widgets.Widget.prototype.draw = function() { |
| 868 | 232 |
/* implemented by "sub-classes" */ |
| 1033 | 233 |
}; |