Added contribution & favorite data
authorveltr
Fri, 11 Oct 2013 11:51:33 +0200
changeset 104 3fef455d214a
parent 103 b9a417017e19
child 105 fe4b70b9991d
Added contribution & favorite data
timeline/js/timeline.js
timeline/timeline-back.html
timeline/timeline-front.html
timeline/timeline.html
--- a/timeline/js/timeline.js	Fri Oct 04 16:01:38 2013 +0200
+++ b/timeline/js/timeline.js	Fri Oct 11 11:51:33 2013 +0200
@@ -134,31 +134,34 @@
         span : 7 * 86400 * 1000,
         grid_interval : 86400 * 1000,
         grid_date_format : '{{dayOfMonth}} {{monthName}}',
-        min_importance : 3 //0 //pour les tests, mettre à 0 pour tout afficher
+        max_importance : 0
     }, {
         label : "3 jours",
         span : 3 * 86400 * 1000,
         grid_interval : 6 * 3600 * 1000,
         grid_date_format : '{{^isDayStart}}{{0hours}}h{{0minutes}}{{/isDayStart}}{{#isDayStart}}{{dayOfMonth}} {{shortMonthName}}{{/isDayStart}}',
-        min_importance : 2 //0 //pour les tests, mettre à 0 pour tout afficher
+        max_importance : 1
     }, {
         label : "Journée",
         span : 86400 * 1000,
         grid_interval : 2 * 3600 * 1000,
         grid_date_format : '{{^isDayStart}}{{0hours}}h{{0minutes}}{{/isDayStart}}{{#isDayStart}}{{dayOfMonth}} {{shortMonthName}}{{/isDayStart}}',
-        min_importance : 1 //0 //pour les tests, mettre à 0 pour tout afficher
+        max_importance : 2
     }, {
         label : "Demi-Journée",
         span : 6 * 3600 * 1000,
         grid_interval : 3600 * 1000,
         grid_date_format : '{{^isDayStart}}{{0hours}}h{{0minutes}}{{/isDayStart}}{{#isDayStart}}{{dayOfMonth}} {{shortMonthName}}{{/isDayStart}}',
-        min_importance : 0
+        max_importance : 3
     }],
     level: 0,
     central_time: 0,
     sync_now: true,
-    api_endpoint: "",
-    api_method: "searchForTimelineEdito",
+    contents_endpoint: "http://anarchy2.solicis.fr/api/cms/content?method=searchForTimelinePublish",
+    get_favorite_endpoint: "http://anarchy2.solicis.fr/stream/timeline/favorites",
+    set_favorite_endpoint: "http://anarchy2.solicis.fr/stream/timeline/favorite",
+    contribution_endpoint: "http://anarchy2.solicis.fr/stream/timeline/contribs",
+    use_auth: true,
     occurrences: [],
     grid_spacing: 12,
     tooltip_date_format: '{{dayOfMonth}} {{monthName}} {{year}} à {{0hours}}:{{0minutes}}',
@@ -216,7 +219,9 @@
         }
     },
     maxtime: false,
-    url_base: ""
+    url_base: "http://anarchy2.solicis.fr",
+    user_id: false,
+    use_jsonp: false
 };
 
 for (var _i = 0; _i < Tlns.Defaults.Timeline.timescales.length; _i++) {
@@ -360,6 +365,46 @@
        
     this.onUniversLoaded(this.linelabels);
     
+    this.favoriteContents = [];
+    this.participationCounts = [];
+    
+    $.getJSON(
+        this.jsonpify(this.get_favorite_endpoint),
+        {
+            uid: this.user_id || undefined,
+            api_key: _this.use_auth ? _this.token : undefined,
+            mail: _this.use_auth ? _this.email : undefined
+        },
+        function(d) {
+            _this.favoriteContents = _(d.data).map(function(f) {
+                return f.id;
+            });
+            _(_this.favoriteContents).each(function(f) {
+                var o = _this.getOccurrence(f);
+                if (o) {
+                    o.isFavorite = true;
+                }
+            });
+        }
+    );
+    
+    $.getJSON(
+        this.jsonpify(this.contribution_endpoint),
+        {
+            api_key: _this.use_auth ? _this.token : undefined,
+            mail: _this.use_auth ? _this.email : undefined
+        },
+        function(d) {
+            _(d.data).each(function(c) {
+                _this.participationCounts[c.id] = c.nb;
+                var o = _this.getOccurrence(c.id);
+                if (o) {
+                    o.participationCount = c.nb;
+                }
+            });
+        }
+    );
+    
 };
 
 Tlns.Classes.Timeline.prototype.onMouseDown = function(_event) {
@@ -475,18 +520,25 @@
     this.drawOccurrences();
 };
 
+Tlns.Classes.Timeline.prototype.jsonpify = function(url) {
+    if (this.use_jsonp) {
+        return url + (/\?/.test(url) ? "&" : "?" ) + "callback=?";
+    } else {
+        return url;
+    }
+};
+
 Tlns.Classes.Timeline.prototype.loadOccurrences = function() {
     var _this = this;
     
     function getData(cursor) {
-        $.getJSON(_this.api_endpoint, {
-            method: _this.api_method,
-            api_key: _this.token,
-            mail: _this.email,
+        $.getJSON(_this.jsonpify(_this.contents_endpoint), {
+            api_key: _this.use_auth ? _this.token : undefined,
+            mail: _this.use_auth ? _this.email : undefined,
             cursor: cursor
         }, function(_data) {
             _this.onOccurrencesLoaded(_data);
-            if (_data.cursor.hasNext) {
+            if (_data.cursor && _data.cursor.hasNext) {
                 getData(_data.cursor.next);
             }
         });
@@ -553,9 +605,9 @@
         _occ.y = _occ.univers.y;
     });
     var minT = this.timeFromX(-32),
-        minI = this.timescales[this.level].min_importance;
+        maxI = this.timescales[this.level].max_importance;
     var _visible = _(this.occurrences).filter(function(_occ) {
-        _occ.visible = (_occ.date >= minT && _occ.date <= _this.end_time && (_occ.importance >= minI));
+        _occ.visible = (_occ.date >= minT && _occ.date <= _this.end_time && (_occ.importance <= maxI));
         return _occ.visible;
     });
     
@@ -718,14 +770,24 @@
     if (typeinfo.picto) {
         this.image = this.timeline.picto_url + typeinfo.picto;
     }
+    var taxonomy = _(_data.contentHasTaxonomys).find(function(t) {
+        return !!t.taxonomy.taxonomyHasMedias;
+    });
+    if (taxonomy) {
+        var taxonomyMedia = _(taxonomy.taxonomy.taxonomyHasMedias).find(function(m) {
+            return !!m.media.carre;
+        });
+        if (taxonomyMedia) {
+            this.image = taxonomyMedia.media.carre.replace(/carre\/[\d]+\/[\d]+/,'carre/32/32');
+        }
+    }
     this.univers = this.timeline.univers[this.univers_id];
     this.format = typeinfo.label;
     
-    /* Données temporaires aléatoires */
-    this.isFavorite = (Math.random() > 1/2); // A random Boolean ;-)
-    if (this.univers_id >= 2) {
-        this.participationCount = Math.floor(12*Math.random());
+    if (this.timeline.favoriteContents.indexOf(this.id) !== -1) {
+        this.isFavorite = true;
     }
+    this.participationCount = this.timeline.participationCounts[this.id];
     /* End Temporary Data */
     
     var _tmp = $('<p>').html(_data.resume || "");
@@ -741,9 +803,21 @@
 
 Tlns.Classes.Occurrence.prototype.toggleFavorite = function() {
     var newFavStatus = !this.isFavorite;
-    /* Add API Call HERE */
     
-    /* The Following status update should be moved to the SUCCESS callback of the API call */
-    this.isFavorite = newFavStatus;
-    this.timeline.throttledDrawGrid();
+    $.ajax({
+        type: "POST",
+        url: this.timeline.set_favorite_endpoint,
+        data: {
+            type: this.type,
+            conId: this.id,
+            status: +newFavStatus,
+            uid: this.timeline.user_id || undefined
+        },
+        success: function(r) {
+            console.log(r);
+            _this.isFavorite = newFavStatus;
+            _this.timeline.throttledDrawGrid();
+        }
+    });
+    
 };
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/timeline/timeline-back.html	Fri Oct 11 11:51:33 2013 +0200
@@ -0,0 +1,47 @@
+<!DOCTYPE html>
+<html lang="fr">
+    <head>
+        <meta charset="utf-8" />
+        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
+        <title>Tests Timeline</title>
+        <meta name="description" content="" />
+        <meta name="author" content="Institut de Recherche et d'Innovation (IRI)" />
+        <link rel="stylesheet" type="text/css" href="lib/jquery-ui.min.css" />
+        <link rel="stylesheet" type="text/css" href="fonts/GoudyOldStyleRegular/stylesheet.css" />
+        <link rel="stylesheet" type="text/css" href="css/base.css" />
+        <link rel="stylesheet" type="text/css" href="css/timeline.css" />
+        <style type="text/css">
+        </style>
+        <script type="text/javascript" src="lib/jquery.min.js"> </script>
+        <script type="text/javascript" src="lib/jquery.mousewheel.min.js"> </script>
+        <script type="text/javascript" src="lib/jquery-ui.min.js"> </script>
+        <script type="text/javascript" src="lib/underscore-min.js"> </script>
+        <script type="text/javascript" src="lib/mustache.js"> </script>
+        <script type="text/javascript" src="js/timeline.js"> </script>
+        <script type="text/javascript">
+            var _tl;
+            $.getJSON(
+                'http://anarchy2.solicis.fr/api/back/user?process=orm_entity_authenticate&login=anarchy@solicis.fr&password=anarchy&callback=?',
+                function(data) {
+                    var token = data.data[0].token;
+                    console.log(token);
+                _tl = new Tlns.Classes.Timeline({
+                    email: 'anarchy@solicis.fr',
+                    token: token,
+                    container: "timeline",
+                    sync_now: false,
+                    central_time: Date.now(),//Date.parse("2013-07-18T00:00:00Z"),
+                    maxtime: Date.now(),
+                    picto_url: "img/",
+                    user_id: "10",
+                    use_jsonp: true
+                });
+            }); 
+        </script>
+    </head>
+
+    <body>
+        <h1>Tests Timeline</h1>
+        <div id="timeline"> </div>
+    </body>
+</html>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/timeline/timeline-front.html	Fri Oct 11 11:51:33 2013 +0200
@@ -0,0 +1,40 @@
+<!DOCTYPE html>
+<html lang="fr">
+    <head>
+        <meta charset="utf-8" />
+        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
+        <title>Tests Timeline</title>
+        <meta name="description" content="" />
+        <meta name="author" content="Institut de Recherche et d'Innovation (IRI)" />
+        <link rel="stylesheet" type="text/css" href="lib/jquery-ui.min.css" />
+        <link rel="stylesheet" type="text/css" href="fonts/GoudyOldStyleRegular/stylesheet.css" />
+        <link rel="stylesheet" type="text/css" href="css/base.css" />
+        <link rel="stylesheet" type="text/css" href="css/timeline.css" />
+        <style type="text/css">
+        </style>
+        <script type="text/javascript" src="lib/jquery.min.js"> </script>
+        <script type="text/javascript" src="lib/jquery.mousewheel.min.js"> </script>
+        <script type="text/javascript" src="lib/jquery-ui.min.js"> </script>
+        <script type="text/javascript" src="lib/underscore-min.js"> </script>
+        <script type="text/javascript" src="lib/mustache.js"> </script>
+        <script type="text/javascript" src="js/timeline.js"> </script>
+    </head>
+
+    <body>
+        <h1>Tests Timeline</h1>
+        <div id="timeline"> </div>
+        <script type="text/javascript">
+            var _tl = new Tlns.Classes.Timeline({
+                container: "timeline",
+                sync_now: false,
+                central_time: Date.now(),//Date.parse("2013-07-18T00:00:00Z"),
+                maxtime: Date.now(),
+                picto_url: "img/",
+                use_auth: false,
+                contents_endpoint: "http://anarchy2.solicis.fr/stream/timeline/contents",
+                user_id: "10",
+                use_jsonp: true
+            });
+        </script>
+    </body>
+</html>
--- a/timeline/timeline.html	Fri Oct 04 16:01:38 2013 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,47 +0,0 @@
-<!DOCTYPE html>
-<html lang="fr">
-    <head>
-        <meta charset="utf-8" />
-        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
-        <title>Tests Timeline</title>
-        <meta name="description" content="" />
-        <meta name="author" content="Institut de Recherche et d'Innovation (IRI)" />
-        <link rel="stylesheet" type="text/css" href="lib/jquery-ui.min.css" />
-        <link rel="stylesheet" type="text/css" href="fonts/GoudyOldStyleRegular/stylesheet.css" />
-        <link rel="stylesheet" type="text/css" href="css/base.css" />
-        <link rel="stylesheet" type="text/css" href="css/timeline.css" />
-        <style type="text/css">
-        </style>
-        <script type="text/javascript" src="lib/jquery.min.js"> </script>
-        <script type="text/javascript" src="lib/jquery.mousewheel.min.js"> </script>
-        <script type="text/javascript" src="lib/jquery-ui.min.js"> </script>
-        <script type="text/javascript" src="lib/underscore-min.js"> </script>
-        <script type="text/javascript" src="lib/mustache.js"> </script>
-        <script type="text/javascript" src="js/timeline.js"> </script>
-        <script type="text/javascript">
-            var _tl;
-            $.getJSON(
-                'http://anarchy2.solicis.fr/api/back/user?process=orm_entity_authenticate&login=anarchy@solicis.fr&password=anarchy&callback=?',
-                function(data) {
-                    var token = data.data[0].token;
-                    console.log(token);
-                _tl = new Tlns.Classes.Timeline({
-                    email: 'anarchy@solicis.fr',
-                    token: token,
-                    api_endpoint: "http://anarchy2.solicis.fr/api/cms/content?callback=?",
-                    container: "timeline",
-                    sync_now: false,
-                    central_time: Date.now(),//Date.parse("2013-07-18T00:00:00Z"),
-                    maxtime: Date.now(),
-                    picto_url: "img/",
-                    url_base: "http://anarchy2.solicis.fr"
-                });
-            }); 
-        </script>
-    </head>
-
-    <body>
-        <h1>Tests Timeline</h1>
-        <div id="timeline"> </div>
-    </body>
-</html>