vttcue-extended.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. // If we're in Node.js then require VTTCue so we can extend it, otherwise assume
  17. // VTTCue is on the global.
  18. if (typeof module !== "undefined" && module.exports) {
  19. this.VTTCue = this.VTTCue || require("./vttcue").VTTCue;
  20. }
  21. // Extend VTTCue with methods to convert to JSON, from JSON, and construct a
  22. // VTTCue from an options object. The primary purpose of this is for use in the
  23. // vtt.js test suite (for testing only properties that we care about). It's also
  24. // useful if you need to work with VTTCues in JSON format.
  25. (function(root) {
  26. root.VTTCue.prototype.toJSON = function() {
  27. var cue = {},
  28. self = this;
  29. // Filter out getCueAsHTML as it's a function and hasBeenReset and displayState as
  30. // they're only used when running the processing model algorithm.
  31. Object.keys(this).forEach(function(key) {
  32. if (key !== "getCueAsHTML" && key !== "hasBeenReset" && key !== "displayState") {
  33. cue[key] = self[key];
  34. }
  35. });
  36. return cue;
  37. };
  38. root.VTTCue.create = function(options) {
  39. if (!options.hasOwnProperty("startTime") || !options.hasOwnProperty("endTime") ||
  40. !options.hasOwnProperty("text")) {
  41. throw new Error("You must at least have start time, end time, and text.");
  42. }
  43. var cue = new root.VTTCue(options.startTime, options.endTime, options.text);
  44. for (var key in options) {
  45. if (cue.hasOwnProperty(key)) {
  46. cue[key] = options[key];
  47. }
  48. }
  49. return cue;
  50. };
  51. root.VTTCue.fromJSON = function(json) {
  52. return this.create(JSON.parse(json));
  53. };
  54. }(this));