not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
4.0 KiB

  1. // Entry component with image and sound
  2. enyo.kind({
  3. name: "VideoViewer.Item",
  4. kind: enyo.Control,
  5. published: {
  6. code: "",
  7. title: "",
  8. category: "",
  9. isFavorite: false,
  10. image: "",
  11. tojournal: false
  12. },
  13. events: {
  14. onVideoPlayed: ""
  15. },
  16. classes: "item",
  17. components: [
  18. { name: "spinner", kind: "Image", src: "images/spinner-dark.gif", classes: "spinner" },
  19. { name: "background", classes: "itemImage", kind: "Image", src: "images/notloaded.png" },
  20. { name: "itemImage", classes: "itemImage", kind: "Image", showing: false, onload: "imageLoaded", onerror: "defaultImage", ontap: "showVideo" },
  21. { name: "itemPlay", classes: "itemPlay", kind: "Image", showing: false, src: "icons/play.svg", ontap: "showVideo" },
  22. { name: "itemFavorite", classes: "itemFavorite", kind: "Image", src: "icons/notfavorite.svg", showing: false, ontap: "showVideo" },
  23. { name: "itemOverlay", classes: "itemOverlay" },
  24. { name: "itemTitle", classes: "itemTitle", content: "" }
  25. ],
  26. // Constructor
  27. create: function() {
  28. this.inherited(arguments);
  29. this.nameChanged();
  30. this.titleChanged();
  31. this.isFavoriteChanged();
  32. this.tojournalChanged();
  33. },
  34. // Item setup
  35. nameChanged: function() {
  36. var image = this.image ? this.image : "";
  37. var imgurl = this.replaceValues(Util.getImages());
  38. if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
  39. // HACK: When in Chrome App image should be load using a XmlHttpRequest
  40. var xhr = new XMLHttpRequest();
  41. var that = this;
  42. xhr.open('GET', imgurl, true);
  43. xhr.responseType = 'blob';
  44. xhr.onload = function(e) {
  45. that.$.itemImage.setAttribute("src", window.URL.createObjectURL(this.response));
  46. };
  47. xhr.send();
  48. } else {
  49. this.$.itemImage.setAttribute("src", imgurl);
  50. }
  51. },
  52. titleChanged: function() {
  53. this.$.itemTitle.setContent(this.title);
  54. },
  55. isFavoriteChanged: function() {
  56. this.$.itemFavorite.setShowing(this.isFavorite);
  57. },
  58. imageLoaded: function() {
  59. this.$.itemImage.setShowing(true);
  60. this.$.itemPlay.setShowing(true);
  61. this.$.spinner.setShowing(false);
  62. this.$.background.setShowing(false);
  63. },
  64. tojournalChanged: function() {
  65. if (this.tojournal) {
  66. this.$.itemPlay.setSrc("icons/tojournal.svg");
  67. } else {
  68. this.$.itemPlay.setSrc("images/play.svg");
  69. }
  70. this.$.itemPlay.render();
  71. },
  72. defaultImage: function() {
  73. this.$.itemImage.setAttribute("src", "images/notloaded.png");
  74. this.$.itemImage.setShowing(true);
  75. this.$.itemPlay.setShowing(true);
  76. this.$.spinner.setShowing(false);
  77. this.$.background.setShowing(false);
  78. },
  79. replaceValues: function(template) {
  80. // Remplace values in the template
  81. var output = template;
  82. output = output.replace(new RegExp("%id%", "g"),this.code);
  83. output = output.replace(new RegExp("%image%", "g"), this.image);
  84. output = output.replace(new RegExp("%category%", "g"), this.category);
  85. output = output.replace(new RegExp("%title%", "g"), this.title);
  86. return output;
  87. },
  88. videoURL: function() {
  89. return this.replaceValues(Util.getVideos())+"."+constant.videoType;
  90. },
  91. exportToVideo: function() {
  92. // Load file
  93. var url =this.videoURL();
  94. var mimetype = (constant.videoType=="mp4"?"video/mp4":"video/webm");
  95. var request = new XMLHttpRequest();
  96. request.open("GET",url,true);
  97. request.setRequestHeader("Content-type",mimetype);
  98. request.responseType = "arraybuffer";
  99. var that = this;
  100. request.onload = function() {
  101. if (request.status == 200 || request.status == 0) {
  102. var blob = new Uint8Array(this.response);
  103. var base64 = "data:"+mimetype+";base64,"+Util.toBase64(blob);
  104. var metadata = {
  105. mimetype: mimetype,
  106. title: that.title+"."+constant.videoType,
  107. activity: "org.olpcfrance.MediaViewerActivity",
  108. timestamp: new Date().getTime(),
  109. creation_time: new Date().getTime(),
  110. file_size: 0
  111. };
  112. app.datastore.create(metadata, function() {
  113. console.log("video '"+that.title+"' saved in journal.");
  114. }, base64);
  115. }
  116. };
  117. request.send();
  118. },
  119. // Handle event
  120. showVideo: function() {
  121. if (this.tojournal) {
  122. this.exportToVideo();
  123. }
  124. this.doVideoPlayed();
  125. }
  126. });