src/js/widgets-container/widget.js
changeset 1068 7623f9af9272
parent 1049 4e8b3df6e5be
child 1072 ac1eacb3aa33
--- a/src/js/widgets-container/widget.js	Fri Sep 25 14:45:38 2015 +0200
+++ b/src/js/widgets-container/widget.js	Fri Oct 02 11:27:17 2015 +0200
@@ -17,44 +17,44 @@
 
 
 IriSP.Widgets.Widget = function(player, config) {
-    
+
     if( typeof player === "undefined") {
         /* Probably an abstract call of the class when
          * individual widgets set their prototype */
         return;
     }
-    
+
     this.__subwidgets = [];
-    
+
     /* Setting all the configuration options */
     var _type = config.type || "(unknown)",
         _config = IriSP._.defaults({}, config, (player && player.config ? player.config.default_options : {}), this.defaults),
         _this = this;
-    
+
     IriSP._(_config).forEach(function(_value, _key) {
        _this[_key] = _value;
     });
-    
+
     this.$ = IriSP.jQuery('#' + this.container);
-    
+
     if (typeof this.width === "undefined") {
         this.width = this.$.width();
     } else {
         this.$.css("width", this.width);
     }
-    
+
     if (typeof this.height !== "undefined") {
         this.$.css("height", this.height);
     }
-    
+
     /* Setting this.player at the end in case it's been overriden
      * by a configuration option of the same name :-(
      */
     this.player = player || new IriSP.FakeClass(["on","trigger","off","loadWidget","loadMetadata"]);
-    
+
     /* Adding classes and html attributes */
     this.$.addClass("Ldt-TraceMe Ldt-Widget").attr("widget-type", _type);
-    
+
     this.l10n = (
         typeof this.messages[IriSP.language] !== "undefined"
         ? this.messages[IriSP.language]
@@ -64,10 +64,14 @@
             : this.messages["en"]
         )
     );
-    
+
     /* Loading Metadata if required */
-   
+
     function onsourceloaded() {
+        if (_this.localannotations) {
+            _this.localsource = player.loadLocalAnnotations(_this.localannotations);
+            _this.source.merge(_this.localsource);
+        }
         if (_this.media_id) {
                 _this.media = this.getElement(_this.media_id);
             } else {
@@ -76,8 +80,6 @@
                 };
                 _this.media = _this.source.getCurrentMedia(_mediaopts);
             }
-            
-
         if (_this.pre_draw_callback){
             IriSP.jQuery.when(_this.pre_draw_callback()).done(_this.draw());
         }
@@ -86,11 +88,10 @@
         }
         _this.player.trigger("widget-loaded");
     }
-    
+
     if (this.metadata) {
         /* Getting metadata */
         this.source = player.loadMetadata(this.metadata);
-        
         /* Call draw when loaded */
         this.source.onLoad(onsourceloaded);
     } else {
@@ -98,8 +99,8 @@
             onsourceloaded();
         }
     }
-    
-    
+
+
 };
 
 IriSP.Widgets.Widget.prototype.defaults = {};
@@ -217,7 +218,7 @@
     // offset is normally either -1 (previous slide) or +1 (next slide)
     var _this = this;
     var currentTime = _this.media.getCurrentTime();
-    var annotations = _this.source.getAnnotations().sortBy(function(_annotation) {
+    var annotations = _this.getWidgetAnnotations().sortBy(function(_annotation) {
         return _annotation.begin;
     });
     for (var i = 0; i < annotations.length; i++) {
@@ -230,6 +231,51 @@
     };
 };
 
+/*
+ * Propose an export of the widget's annotations
+ *
+ * Parameter: a list of annotations. If not specified, the widget's annotations will be exported.
+ */
+IriSP.Widgets.Widget.prototype.exportAnnotations = function(annotations) {
+    var widget = this;
+    if (annotations === undefined)
+        annotations = this.getWidgetAnnotations();
+    var $ = IriSP.jQuery;
+
+    // FIXME: this should belong to a proper serialize/deserialize component?
+    var content = Mustache.to_html("[video:{{url}}]\n", {url: widget.media.url}) + annotations.map( function(a) { return Mustache.to_html("[{{ a.begin }}]{{ a.title }} {{ a.description }}[{{ a.end }}]", { a: a }); }).join("\n");
+
+    var el = $("<pre>")
+            .addClass("exportContainer")
+            .text(content)
+            .dialog({
+                title: "Annotation export",
+                open: function( event, ui ) {
+                    // Select text
+                    var range;
+                    if (document.selection) {
+		                range = document.body.createTextRange();
+                        range.moveToElementText(this[0]);
+		                range.select();
+		            } else if (window.getSelection) {
+		                range = document.createRange();
+		                range.selectNode(this[0]);
+		                window.getSelection().addRange(range);
+		            }
+                },
+                autoOpen: true,
+                width: '80%',
+                minHeight: '400',
+                height: 400,
+                buttons: [ { text: "Close", click: function() { $( this ).dialog( "close" ); } },
+                           { text: "Download", click: function () {
+                               a = document.createElement('a');
+                               a.setAttribute('href', 'data:text/plain;base64,' + btoa(content));
+                               a.setAttribute('download', 'Annotations - ' + widget.media.title.replace(/[^ \w]/g, '') + '.txt');
+                               a.click();
+                           } } ]
+            });
+};
 
 /**
  * This method responsible of drawing a widget on screen.