integration/js/edition.js
changeset 28 a0a048ff33a1
parent 23 2a3159ee4ed0
child 34 e3a17ec94cd8
--- a/integration/js/edition.js	Tue May 21 17:41:57 2013 +0200
+++ b/integration/js/edition.js	Wed May 22 17:09:59 2013 +0200
@@ -1,16 +1,23 @@
+var myMedia = null,
+    currentChapter = null,
+    chapters = [],
+    annotations = [];
+
 $(function(){
 
 var global = {
     diaporama : null,
     idAnnotation : null,
-    colors : ['#f39c12', '#2ecc71', '#3498db', '#9b59b6',  
-    '#f1c40f', '#e67e22', '#e74c3c', '#ecf0f1', 
-    '#16a085', '#27ae60', '#2980b9', '#8e44ad', '#2c3e50',
-    '#f39c12', '#d35400', '#c0392b', '#bdc3c7', '#7f8c8d']
-},
-currentChapter = null,
-chapters = [],
-annotations = [];
+    colorsIndex : 0,
+    colors : 
+        ['#f39c12', '#2ecc71', '#3498db', '#9b59b6',  
+        '#f1c40f', '#e67e22', '#e74c3c', '#ecf0f1', 
+        '#16a085', '#27ae60', '#2980b9', '#8e44ad', 
+        '#f39c12', '#d35400', '#c0392b', '#bdc3c7']
+};
+
+
+
 
 myProject.onLoad(function() {
 
@@ -18,7 +25,7 @@
     
     myMedia = myProject.getCurrentMedia();
     
-    loadChapters();
+    loadInitChapters();
 
     IriSP.htmlPlayer(
         myMedia,
@@ -45,27 +52,6 @@
         }else{
             btnCutChapter.css("left",pos);
         }
-
-        /*
-        //chapitre edit
-        var formChapter = $('.form-chapter-edit'),
-            inputBeginChapter = formChapter.find('input[name=begin]'),
-            inputDurationChapter = formChapter.find('input[name=duration]'),
-            inputEndChapter = formChapter.find('input[name=end]'),
-            viewBeginChapter = formChapter.find('.begin'),
-            viewDurationChapter = formChapter.find('.duration'),
-            viewEndChapter = formChapter.find('.end'),
-            timeBegin = 0,
-            timeEnd = t.milliseconds,
-            timeDuration = timeEnd - timeBegin;
-
-        inputBeginChapter.val(timeBegin);
-        inputEndChapter.val(timeEnd);
-        inputDurationChapter.val(timeDuration);
-        viewBeginChapter.html(millisecondsToString(timeBegin));
-        viewDurationChapter.html(millisecondsToString(timeDuration));
-        viewEndChapter.html(millisecondsToString(timeEnd));
-        */
     });//timeupdate
     
 });//myProject.onLoad
@@ -166,7 +152,6 @@
         e.preventDefault();
         var idChapter = $(this).attr('data-chapter-id');
         loadFormChapter(idChapter);
-        currentChapter = _.find(chapters, function(c){ return c.id == idChapter; });
     });
 
     $('.chapter-segments').on('click', 'li', function(){
@@ -187,8 +172,8 @@
 
     function onTagItChange(e, ui) {
         var idChapter = $(this).parents('form').attr('data-chapter-id'),
-            value = $('input[name=tags]').val();
-        currentChapter.tags = value;
+            value = $('input[name=keywords]').val();
+        currentChapter.keywords = value;
         $('#row-list-chapter-'+idChapter).find('.list-chapter-tags').text(value);
     }
 
@@ -198,14 +183,18 @@
     }
 
     function loadFormChapter(idChapter){
-        var chapterData = _.find(chapters, function(c){ return c.id == idChapter; }),
-            chapterWrap = $('.chapter-widget-info');
+        currentChapter = _.find(chapters, function(c){ return c.id == idChapter; });
+        var chapterWrap = $('.chapter-widget-info'),
+            indexChapter = _.indexOf(chapters, currentChapter),
+            beginTangle = (indexChapter>0) ? true : false,
+            endTangle = (indexChapter<(chapters.length-1)) ? true : false;
 
-        currentChapter = chapterData;
+        currentChapter.beginTangle = beginTangle;
+        currentChapter.endTangle = endTangle;
 
         $.get('template.html', function(templates){
             var tpl = $(templates).filter('#tpl-chapter-edit').html();
-            tpl = Mustache.render(tpl, chapterData);
+            tpl = Mustache.render(tpl, currentChapter);
             chapterWrap.empty().append(tpl);
             chapterWrap.find('.tag-it').tagit(tagitParam);
         });
@@ -217,18 +206,18 @@
     e.preventDefault();
 
     if(chapters.length == 1){alert('Le projet doit contenir au moins un chapitre.'); return;}
+
     var idChapter = $(this).attr('data-chapter-id'),
         chapter = _.find(chapters, function(c){ return c.id == idChapter; }),
         indexChapter = _.indexOf(chapters, chapter),
         chapterModify;
     if(indexChapter == 0){
         chapterModify = chapters[1];
-        chapterModify.begin = 0;
-        chapterModify.duration = chapterModify.end;
+        chapterModify.setBegin(0);
     }else{
         chapterModify = chapters[indexChapter-1];
-        chapterModify.end = chapter.end;
-        chapterModify.duration = chapterModify.end - chapterModify.begin;
+        //var newEnd = new IriSP.Model.Time(chapter.end)
+        chapterModify.setEnd(chapter.end);
     }
     chapters = _(chapters).reject(function(c) { return c.id == idChapter; });
     renderChapter();
@@ -239,33 +228,34 @@
 });
 
 //nouveau chapitre
+    function newChapter(dataChapter){
+        var chapter = new IriSP.Model.Annotation(false, myProject);
+            chapter.setMedia(myMedia.id);
+            chapter.setBegin(dataChapter.begin);
+            chapter.setEnd(dataChapter.end);
+            chapter.title = dataChapter.title;
+            chapter.description = dataChapter.description;
+            chapter.keywords = dataChapter.keywords;
+            chapter.color = global.colors[(global.colorsIndex<global.colors.length) ? global.colorsIndex++ : (global.colorsIndex=0)];
+    
+        chapters.push(chapter);
+        renderChapter();
+        loadFormChapter(chapter.id);
+    }
+
     $('.chapter-widget').on('click', '.btn-cut-chapter', function(e){
         e.preventDefault();
 
-        var uniqId = 'id' + (new Date()).getTime();
-
-        var title = 'New',
-            tags = 'tag',
-            begin = myMedia.currentTime.milliseconds,
-            end = organizeNewChapter(begin),
-            duration = end - begin,
-            description = '',
-            id = uniqId;
+        var dataChapter = {
+                title : 'New',
+                begin : myMedia.currentTime,
+                end : organizeNewChapter(myMedia.currentTime),
+                description : 'description',
+                keywords : 'tag1,tag2'
+            };
 
-        var dataChapter = {
-            title : title,
-            tags : tags,
-            begin : begin,
-            duration : duration,
-            end : end,
-            description : description,
-            color : global.colors[chapters.length],
-            id : uniqId
-        };
-      
-        chapters.push(dataChapter);
-        renderChapter();
-        loadFormChapter(id);
+        newChapter(dataChapter);
+
     });
     
     function organizeNewChapter(beginNew){
@@ -275,11 +265,11 @@
             var begin = v.begin,
                 end = v.end;
             if(beginNew>=begin && beginNew<=end){
-                v.end = beginNew;
-                v.duration = v.end - v.begin;
-                returnEnd = end;
+                returnEnd = new IriSP.Model.Time(end);
+                v.setEnd(beginNew); 
             }
         });
+ 
         return returnEnd;
     }
     
@@ -296,24 +286,21 @@
         chapterList.empty();
 
         $.each(chapters, function(k, v){
-
+            //form
+            if($('#form-chapter-edit-'+v.id).length){
+                loadFormChapter(v.id);
+            }
             //segments
-                color = global.colors[k],
-                width = Math.floor(v.duration * wChapterSegmentWrap / myMedia.duration.milliseconds),
+            var width = Math.floor(v.getDuration() * wChapterSegmentWrap / myMedia.duration),
                 segment = $('<li>'+v.title+'</li>').css({
                     width : width,
                     backgroundColor : v.color
                 }).attr('id', v.id);
     
             chapterSegmentWrap.append(segment);
- 
+
             //liste
             $.get('template.html', function(templates){
-
-                v.beginString = millisecondsToString(v.begin);
-                v.durationString = millisecondsToString(v.duration);
-                v.endString = millisecondsToString(v.end);
-             
                 var tplChapterRow = $(templates).filter('#tpl-chapter-row').html();
                 tplChapterRow = Mustache.render(tplChapterRow, v);
                 chapterList.append(tplChapterRow);
@@ -323,37 +310,21 @@
     }
 
 //init
-    function loadChapters(){//nouveau projet, 1 chapitre
-
-        var uniqId = 'id' + (new Date()).getTime();
-
-        var title = 'chapitre 1',
-            tags = 'tag1,tag2',
-            begin = 0,
-            duration = myMedia.duration.milliseconds,
-            end = myMedia.duration.milliseconds,
-            description = 'description du chapitre 1',
-            id = uniqId;
-
+    function loadInitChapters(){//nouveau projet, 1 chapitre
         var dataChapter = {
-            title : title,
-            tags : tags,
-            begin : begin,
-            duration : duration,
-            end : end,
-            description : description,
-            color : global.colors[chapters.length],
-            id : id
-        };
+                title : 'New',
+                begin : 0,
+                end : myMedia.duration,
+                description : 'description',
+                keywords : 'tag1,tag2'
+            };
 
-        chapters.push(dataChapter);
-        renderChapter();
+        newChapter(dataChapter);
     }
 
 
 
 
-
     //edit annotation
     $('#list-annotations').on('click', 'a.btn-edit-annotation', function(e){
         e.preventDefault();