src/js/widgets/createAnnotationWidget.js
branchpopcorn-port
changeset 836 526f91f5253e
parent 834 573c7ca752e0
child 837 353a78021ebc
equal deleted inserted replaced
835:a8af9da7c622 836:526f91f5253e
   243   
   243   
   244   if (this.cinecast_version) {
   244   if (this.cinecast_version) {
   245     this.selector.find(".Ldt-createAnnotation-Title").parent().show();      
   245     this.selector.find(".Ldt-createAnnotation-Title").parent().show();      
   246   }
   246   }
   247 
   247 
   248   var url = typeof annotation.url == "string" ? annotation.url : document.location.href + "#id=" + annotation.id;
   248   var url = ( (typeof annotation.meta == "object" && typeof annotation.meta.url == "string" && annotation.meta.url.length)
       
   249     ? annotation.meta.url
       
   250     : ( document.location.href + "#id=" + annotation.id ) );
   249   var twStatus = IriSP.mkTweetUrl(url);
   251   var twStatus = IriSP.mkTweetUrl(url);
   250   var gpStatus = IriSP.mkGplusUrl(url);
   252   var gpStatus = IriSP.mkGplusUrl(url);
   251   var fbStatus = IriSP.mkFbUrl(url);
   253   var fbStatus = IriSP.mkFbUrl(url);
   252   
   254   
   253   this.selector.find(".Ldt-createAnnotation-endScreen-TweetLink").attr("href", twStatus);
   255   this.selector.find(".Ldt-createAnnotation-endScreen-TweetLink").attr("href", twStatus);
   310 };
   312 };
   311 
   313 
   312 IriSP.createAnnotationWidget.prototype.sendLdtData = function(contents, callback) {
   314 IriSP.createAnnotationWidget.prototype.sendLdtData = function(contents, callback) {
   313   var _this = this;
   315   var _this = this;
   314   var apiJson = {annotations : [{}], meta: {}};
   316   var apiJson = {annotations : [{}], meta: {}};
   315   var annotation = apiJson["annotations"][0];
   317   var annotation = apiJson.annotations[0];
   316   
   318   
   317   annotation["media"] = this._serializer.currentMedia()["id"];
   319   annotation.media = this._serializer.currentMedia()["id"];
   318   
   320   
   319   if (this.cinecast_version) {   
   321   if (this.cinecast_version) {   
   320       annotation["begin"] = Math.round(this._Popcorn.currentTime() * 1000);
   322       annotation.begin = Math.round(this._Popcorn.currentTime() * 1000);
   321       annotation["end"] = annotation["begin"];      
   323       annotation.end = annotation.begin;      
   322   } else {
   324   } else {
   323     var duration = this._serializer.getDuration();    
   325     var duration = this._serializer.getDuration();    
   324     annotation["begin"] = +((duration * (this.sliceLeft / 100)).toFixed(0));
   326     annotation.begin = +((duration * (this.sliceLeft / 100)).toFixed(0));
   325     annotation["end"] = +((duration * ((this.sliceWidth + this.sliceLeft) / 100)).toFixed(0));
   327     annotation.end = +((duration * ((this.sliceWidth + this.sliceLeft) / 100)).toFixed(0));
   326   }
   328   }
   327 
   329 
   328   // boundary checks
   330   // boundary checks
   329   if (annotation["begin"] < 0)
   331   annotation.begin = Math.max(0, annotation.begin);
   330         annotation["begin"] = 0;
   332   annotation.end = Math.min(this._serializer.getDuration(), annotation.end);
   331   
   333   
   332   if (annotation["end"] > this._serializer.getDuration())
   334   annotation.type = ( this.cinecast_version ? "cinecast:UserAnnotation" : ( this._serializer.getContributions() || "" ));
   333     annotation["end"] = this._serializer.getDuration();
   335   if (typeof(annotation.type) === "undefined")
   334       
   336     annotation.type = "";
   335   
   337   
   336   annotation["type"] = this._serializer.getContributions();
   338   annotation.type_title = "Contributions";
   337   if (typeof(annotation["type"]) === "undefined")
       
   338     annotation["type"] = "";
       
   339   
       
   340   annotation["type_title"] = "Contributions";
       
   341   annotation.content = {};
   339   annotation.content = {};
   342   annotation.content["data"] = contents;
   340   annotation.content.data = contents;
   343   
   341   
   344   var meta = apiJson["meta"];
   342   var meta = apiJson.meta;
   345   
   343   
   346   
   344   
   347   var _username = this.selector.find(".Ldt-createAnnotation-userName").val();
   345   var _username = this.selector.find(".Ldt-createAnnotation-userName").val();
   348   meta.creator = (
   346   meta.creator = (
   349       (_username && _username.length)
   347       (_username && _username.length)
   356   );
   354   );
   357   
   355   
   358   meta.created = Date().toString();
   356   meta.created = Date().toString();
   359   
   357   
   360   // All #hashtags are added to tags
   358   // All #hashtags are added to tags
   361   annotation.tags = contents.match(/(#[\S]*)/g);
   359   annotation.tags = contents.match(/#[^#\s]+\b/gim) || [];
   362   
   360   
   363   var jsonString = JSON.stringify(apiJson);
   361   var jsonString = JSON.stringify(apiJson);
   364   var project_id = this._serializer._data.meta.id;
   362   var project_id = this._serializer._data.meta.id;
   365   
   363   
   366   //TODO: extract magic url
   364   //TODO: extract magic url
   373       contentType: 'application/json',
   371       contentType: 'application/json',
   374       data: jsonString,               
   372       data: jsonString,               
   375       //dataType: 'json',
   373       //dataType: 'json',
   376       success: IriSP.wrap(this, function(json, textStatus, XMLHttpRequest) {                   
   374       success: IriSP.wrap(this, function(json, textStatus, XMLHttpRequest) {                   
   377                     /* add the annotation to the annotation and tell the world */
   375                     /* add the annotation to the annotation and tell the world */
   378                     
   376                     if (this.cinecast_version) {
       
   377                         var annotation = json.annotations[0];
       
   378                     } else {
   379                     /* if the media doesn't have a contributions line, we need to add one */
   379                     /* if the media doesn't have a contributions line, we need to add one */
   380                     if (typeof(this._serializer.getContributions()) === "undefined") {
   380                         if (typeof(this._serializer.getContributions()) === "undefined") {
   381                       /* set up a basic view */
   381                           /* set up a basic view */
   382                       var tmp_view = {"dc:contributor": "perso", "dc:creator": "perso", "dc:title": "Contributions",
   382                           var tmp_view = {"dc:contributor": "perso", "dc:creator": "perso", "dc:title": "Contributions",
   383                                       "id": json.annotations[0].type}
   383                                           "id": json.annotations[0].type}
   384 
   384     
   385                       
   385                           
   386                         IriSP.get_aliased(this._serializer._data, ["annotation_types", "annotation-types"]).push(tmp_view);
   386                             IriSP.get_aliased(this._serializer._data, ["annotation_types", "annotation-types"]).push(tmp_view);
       
   387                         }
       
   388                         
       
   389                         delete annotation.tags;
       
   390                         annotation.content.description = annotation.content.data;
       
   391                         annotation.content.title = "";
       
   392                         delete annotation.content.data;
       
   393                         annotation.id = json.annotations[0].id;
       
   394     
       
   395                         annotation.meta = meta;
       
   396                         annotation.meta["id-ref"] = json.annotations[0]["type"];
   387                     }
   397                     }
   388                     
   398                         
   389                     delete annotation.tags;
       
   390                     annotation.content.description = annotation.content.data;
       
   391                     annotation.content.title = "";
       
   392                     delete annotation.content.data;
       
   393                     annotation.id = json.annotations[0].id;
       
   394 
       
   395                     annotation.meta = meta;
       
   396                     annotation.meta["id-ref"] = json.annotations[0]["type"];
       
   397                     
       
   398                     // everything is shared so there's no need to propagate the change
   399                     // everything is shared so there's no need to propagate the change
   399                     _this._serializer._data.annotations.push(annotation);
   400                     _this._serializer._data.annotations.push(annotation);
   400  
   401  
   401                     _this._Popcorn.trigger("IriSP.createAnnotationWidget.addedAnnotation", annotation);
   402                     _this._Popcorn.trigger("IriSP.createAnnotationWidget.addedAnnotation", annotation);
   402                     callback(annotation);
   403                     callback(annotation);