76c9f622544da21bb01df6031bdbb56560865774
[yaffs-website] / node_modules / videojs-vtt.js / lib / vttcue.js
1 /**
2  * Copyright 2013 vtt.js Contributors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 (function(root, vttjs) {
18
19   var autoKeyword = "auto";
20   var directionSetting = {
21     "": true,
22     "lr": true,
23     "rl": true
24   };
25   var alignSetting = {
26     "start": true,
27     "middle": true,
28     "end": true,
29     "left": true,
30     "right": true
31   };
32
33   function findDirectionSetting(value) {
34     if (typeof value !== "string") {
35       return false;
36     }
37     var dir = directionSetting[value.toLowerCase()];
38     return dir ? value.toLowerCase() : false;
39   }
40
41   function findAlignSetting(value) {
42     if (typeof value !== "string") {
43       return false;
44     }
45     var align = alignSetting[value.toLowerCase()];
46     return align ? value.toLowerCase() : false;
47   }
48
49   function extend(obj) {
50     var i = 1;
51     for (; i < arguments.length; i++) {
52       var cobj = arguments[i];
53       for (var p in cobj) {
54         obj[p] = cobj[p];
55       }
56     }
57
58     return obj;
59   }
60
61   function VTTCue(startTime, endTime, text) {
62     var cue = this;
63     var isIE8 = (/MSIE\s8\.0/).test(navigator.userAgent);
64     var baseObj = {};
65
66     if (isIE8) {
67       cue = document.createElement('custom');
68     } else {
69       baseObj.enumerable = true;
70     }
71
72     /**
73      * Shim implementation specific properties. These properties are not in
74      * the spec.
75      */
76
77     // Lets us know when the VTTCue's data has changed in such a way that we need
78     // to recompute its display state. This lets us compute its display state
79     // lazily.
80     cue.hasBeenReset = false;
81
82     /**
83      * VTTCue and TextTrackCue properties
84      * http://dev.w3.org/html5/webvtt/#vttcue-interface
85      */
86
87     var _id = "";
88     var _pauseOnExit = false;
89     var _startTime = startTime;
90     var _endTime = endTime;
91     var _text = text;
92     var _region = null;
93     var _vertical = "";
94     var _snapToLines = true;
95     var _line = "auto";
96     var _lineAlign = "start";
97     var _position = 50;
98     var _positionAlign = "middle";
99     var _size = 50;
100     var _align = "middle";
101
102     Object.defineProperty(cue,
103       "id", extend({}, baseObj, {
104         get: function() {
105           return _id;
106         },
107         set: function(value) {
108           _id = "" + value;
109         }
110       }));
111
112     Object.defineProperty(cue,
113       "pauseOnExit", extend({}, baseObj, {
114         get: function() {
115           return _pauseOnExit;
116         },
117         set: function(value) {
118           _pauseOnExit = !!value;
119         }
120       }));
121
122     Object.defineProperty(cue,
123       "startTime", extend({}, baseObj, {
124         get: function() {
125           return _startTime;
126         },
127         set: function(value) {
128           if (typeof value !== "number") {
129             throw new TypeError("Start time must be set to a number.");
130           }
131           _startTime = value;
132           this.hasBeenReset = true;
133         }
134       }));
135
136     Object.defineProperty(cue,
137       "endTime", extend({}, baseObj, {
138         get: function() {
139           return _endTime;
140         },
141         set: function(value) {
142           if (typeof value !== "number") {
143             throw new TypeError("End time must be set to a number.");
144           }
145           _endTime = value;
146           this.hasBeenReset = true;
147         }
148       }));
149
150     Object.defineProperty(cue,
151       "text", extend({}, baseObj, {
152         get: function() {
153           return _text;
154         },
155         set: function(value) {
156           _text = "" + value;
157           this.hasBeenReset = true;
158         }
159       }));
160
161     Object.defineProperty(cue,
162       "region", extend({}, baseObj, {
163         get: function() {
164           return _region;
165         },
166         set: function(value) {
167           _region = value;
168           this.hasBeenReset = true;
169         }
170       }));
171
172     Object.defineProperty(cue,
173       "vertical", extend({}, baseObj, {
174         get: function() {
175           return _vertical;
176         },
177         set: function(value) {
178           var setting = findDirectionSetting(value);
179           // Have to check for false because the setting an be an empty string.
180           if (setting === false) {
181             throw new SyntaxError("An invalid or illegal string was specified.");
182           }
183           _vertical = setting;
184           this.hasBeenReset = true;
185         }
186       }));
187
188     Object.defineProperty(cue,
189       "snapToLines", extend({}, baseObj, {
190         get: function() {
191           return _snapToLines;
192         },
193         set: function(value) {
194           _snapToLines = !!value;
195           this.hasBeenReset = true;
196         }
197       }));
198
199     Object.defineProperty(cue,
200       "line", extend({}, baseObj, {
201         get: function() {
202           return _line;
203         },
204         set: function(value) {
205           if (typeof value !== "number" && value !== autoKeyword) {
206             throw new SyntaxError("An invalid number or illegal string was specified.");
207           }
208           _line = value;
209           this.hasBeenReset = true;
210         }
211       }));
212
213     Object.defineProperty(cue,
214       "lineAlign", extend({}, baseObj, {
215         get: function() {
216           return _lineAlign;
217         },
218         set: function(value) {
219           var setting = findAlignSetting(value);
220           if (!setting) {
221             throw new SyntaxError("An invalid or illegal string was specified.");
222           }
223           _lineAlign = setting;
224           this.hasBeenReset = true;
225         }
226       }));
227
228     Object.defineProperty(cue,
229       "position", extend({}, baseObj, {
230         get: function() {
231           return _position;
232         },
233         set: function(value) {
234           if (value < 0 || value > 100) {
235             throw new Error("Position must be between 0 and 100.");
236           }
237           _position = value;
238           this.hasBeenReset = true;
239         }
240       }));
241
242     Object.defineProperty(cue,
243       "positionAlign", extend({}, baseObj, {
244         get: function() {
245           return _positionAlign;
246         },
247         set: function(value) {
248           var setting = findAlignSetting(value);
249           if (!setting) {
250             throw new SyntaxError("An invalid or illegal string was specified.");
251           }
252           _positionAlign = setting;
253           this.hasBeenReset = true;
254         }
255       }));
256
257     Object.defineProperty(cue,
258       "size", extend({}, baseObj, {
259         get: function() {
260           return _size;
261         },
262         set: function(value) {
263           if (value < 0 || value > 100) {
264             throw new Error("Size must be between 0 and 100.");
265           }
266           _size = value;
267           this.hasBeenReset = true;
268         }
269       }));
270
271     Object.defineProperty(cue,
272       "align", extend({}, baseObj, {
273         get: function() {
274           return _align;
275         },
276         set: function(value) {
277           var setting = findAlignSetting(value);
278           if (!setting) {
279             throw new SyntaxError("An invalid or illegal string was specified.");
280           }
281           _align = setting;
282           this.hasBeenReset = true;
283         }
284       }));
285
286     /**
287      * Other <track> spec defined properties
288      */
289
290     // http://www.whatwg.org/specs/web-apps/current-work/multipage/the-video-element.html#text-track-cue-display-state
291     cue.displayState = undefined;
292
293     if (isIE8) {
294       return cue;
295     }
296   }
297
298   /**
299    * VTTCue methods
300    */
301
302   VTTCue.prototype.getCueAsHTML = function() {
303     // Assume WebVTT.convertCueToDOMTree is on the global.
304     return WebVTT.convertCueToDOMTree(window, this.text);
305   };
306
307   root.VTTCue = root.VTTCue || VTTCue;
308   vttjs.VTTCue = VTTCue;
309 }(this, (this.vttjs || {})));