timeline/js/timeline.js
changeset 104 3fef455d214a
parent 103 b9a417017e19
child 105 fe4b70b9991d
equal deleted inserted replaced
103:b9a417017e19 104:3fef455d214a
   132     timescales : [{
   132     timescales : [{
   133         label : "Semaine",
   133         label : "Semaine",
   134         span : 7 * 86400 * 1000,
   134         span : 7 * 86400 * 1000,
   135         grid_interval : 86400 * 1000,
   135         grid_interval : 86400 * 1000,
   136         grid_date_format : '{{dayOfMonth}} {{monthName}}',
   136         grid_date_format : '{{dayOfMonth}} {{monthName}}',
   137         min_importance : 3 //0 //pour les tests, mettre à 0 pour tout afficher
   137         max_importance : 0
   138     }, {
   138     }, {
   139         label : "3 jours",
   139         label : "3 jours",
   140         span : 3 * 86400 * 1000,
   140         span : 3 * 86400 * 1000,
   141         grid_interval : 6 * 3600 * 1000,
   141         grid_interval : 6 * 3600 * 1000,
   142         grid_date_format : '{{^isDayStart}}{{0hours}}h{{0minutes}}{{/isDayStart}}{{#isDayStart}}{{dayOfMonth}} {{shortMonthName}}{{/isDayStart}}',
   142         grid_date_format : '{{^isDayStart}}{{0hours}}h{{0minutes}}{{/isDayStart}}{{#isDayStart}}{{dayOfMonth}} {{shortMonthName}}{{/isDayStart}}',
   143         min_importance : 2 //0 //pour les tests, mettre à 0 pour tout afficher
   143         max_importance : 1
   144     }, {
   144     }, {
   145         label : "Journée",
   145         label : "Journée",
   146         span : 86400 * 1000,
   146         span : 86400 * 1000,
   147         grid_interval : 2 * 3600 * 1000,
   147         grid_interval : 2 * 3600 * 1000,
   148         grid_date_format : '{{^isDayStart}}{{0hours}}h{{0minutes}}{{/isDayStart}}{{#isDayStart}}{{dayOfMonth}} {{shortMonthName}}{{/isDayStart}}',
   148         grid_date_format : '{{^isDayStart}}{{0hours}}h{{0minutes}}{{/isDayStart}}{{#isDayStart}}{{dayOfMonth}} {{shortMonthName}}{{/isDayStart}}',
   149         min_importance : 1 //0 //pour les tests, mettre à 0 pour tout afficher
   149         max_importance : 2
   150     }, {
   150     }, {
   151         label : "Demi-Journée",
   151         label : "Demi-Journée",
   152         span : 6 * 3600 * 1000,
   152         span : 6 * 3600 * 1000,
   153         grid_interval : 3600 * 1000,
   153         grid_interval : 3600 * 1000,
   154         grid_date_format : '{{^isDayStart}}{{0hours}}h{{0minutes}}{{/isDayStart}}{{#isDayStart}}{{dayOfMonth}} {{shortMonthName}}{{/isDayStart}}',
   154         grid_date_format : '{{^isDayStart}}{{0hours}}h{{0minutes}}{{/isDayStart}}{{#isDayStart}}{{dayOfMonth}} {{shortMonthName}}{{/isDayStart}}',
   155         min_importance : 0
   155         max_importance : 3
   156     }],
   156     }],
   157     level: 0,
   157     level: 0,
   158     central_time: 0,
   158     central_time: 0,
   159     sync_now: true,
   159     sync_now: true,
   160     api_endpoint: "",
   160     contents_endpoint: "http://anarchy2.solicis.fr/api/cms/content?method=searchForTimelinePublish",
   161     api_method: "searchForTimelineEdito",
   161     get_favorite_endpoint: "http://anarchy2.solicis.fr/stream/timeline/favorites",
       
   162     set_favorite_endpoint: "http://anarchy2.solicis.fr/stream/timeline/favorite",
       
   163     contribution_endpoint: "http://anarchy2.solicis.fr/stream/timeline/contribs",
       
   164     use_auth: true,
   162     occurrences: [],
   165     occurrences: [],
   163     grid_spacing: 12,
   166     grid_spacing: 12,
   164     tooltip_date_format: '{{dayOfMonth}} {{monthName}} {{year}} à {{0hours}}:{{0minutes}}',
   167     tooltip_date_format: '{{dayOfMonth}} {{monthName}} {{year}} à {{0hours}}:{{0minutes}}',
   165     class_info: {
   168     class_info: {
   166         "Cms\\Previously": {
   169         "Cms\\Previously": {
   214             univers_id: 3,
   217             univers_id: 3,
   215             show: true
   218             show: true
   216         }
   219         }
   217     },
   220     },
   218     maxtime: false,
   221     maxtime: false,
   219     url_base: ""
   222     url_base: "http://anarchy2.solicis.fr",
       
   223     user_id: false,
       
   224     use_jsonp: false
   220 };
   225 };
   221 
   226 
   222 for (var _i = 0; _i < Tlns.Defaults.Timeline.timescales.length; _i++) {
   227 for (var _i = 0; _i < Tlns.Defaults.Timeline.timescales.length; _i++) {
   223     Tlns.Defaults.Timeline.timescales[_i].level = _i;
   228     Tlns.Defaults.Timeline.timescales[_i].level = _i;
   224 }
   229 }
   357     });
   362     });
   358     
   363     
   359     this.setLevel(this.level);
   364     this.setLevel(this.level);
   360        
   365        
   361     this.onUniversLoaded(this.linelabels);
   366     this.onUniversLoaded(this.linelabels);
       
   367     
       
   368     this.favoriteContents = [];
       
   369     this.participationCounts = [];
       
   370     
       
   371     $.getJSON(
       
   372         this.jsonpify(this.get_favorite_endpoint),
       
   373         {
       
   374             uid: this.user_id || undefined,
       
   375             api_key: _this.use_auth ? _this.token : undefined,
       
   376             mail: _this.use_auth ? _this.email : undefined
       
   377         },
       
   378         function(d) {
       
   379             _this.favoriteContents = _(d.data).map(function(f) {
       
   380                 return f.id;
       
   381             });
       
   382             _(_this.favoriteContents).each(function(f) {
       
   383                 var o = _this.getOccurrence(f);
       
   384                 if (o) {
       
   385                     o.isFavorite = true;
       
   386                 }
       
   387             });
       
   388         }
       
   389     );
       
   390     
       
   391     $.getJSON(
       
   392         this.jsonpify(this.contribution_endpoint),
       
   393         {
       
   394             api_key: _this.use_auth ? _this.token : undefined,
       
   395             mail: _this.use_auth ? _this.email : undefined
       
   396         },
       
   397         function(d) {
       
   398             _(d.data).each(function(c) {
       
   399                 _this.participationCounts[c.id] = c.nb;
       
   400                 var o = _this.getOccurrence(c.id);
       
   401                 if (o) {
       
   402                     o.participationCount = c.nb;
       
   403                 }
       
   404             });
       
   405         }
       
   406     );
   362     
   407     
   363 };
   408 };
   364 
   409 
   365 Tlns.Classes.Timeline.prototype.onMouseDown = function(_event) {
   410 Tlns.Classes.Timeline.prototype.onMouseDown = function(_event) {
   366     this.mouse_down = true;
   411     this.mouse_down = true;
   473     }
   518     }
   474     this.$.find('.Tl-Grid').html(_html);
   519     this.$.find('.Tl-Grid').html(_html);
   475     this.drawOccurrences();
   520     this.drawOccurrences();
   476 };
   521 };
   477 
   522 
       
   523 Tlns.Classes.Timeline.prototype.jsonpify = function(url) {
       
   524     if (this.use_jsonp) {
       
   525         return url + (/\?/.test(url) ? "&" : "?" ) + "callback=?";
       
   526     } else {
       
   527         return url;
       
   528     }
       
   529 };
       
   530 
   478 Tlns.Classes.Timeline.prototype.loadOccurrences = function() {
   531 Tlns.Classes.Timeline.prototype.loadOccurrences = function() {
   479     var _this = this;
   532     var _this = this;
   480     
   533     
   481     function getData(cursor) {
   534     function getData(cursor) {
   482         $.getJSON(_this.api_endpoint, {
   535         $.getJSON(_this.jsonpify(_this.contents_endpoint), {
   483             method: _this.api_method,
   536             api_key: _this.use_auth ? _this.token : undefined,
   484             api_key: _this.token,
   537             mail: _this.use_auth ? _this.email : undefined,
   485             mail: _this.email,
       
   486             cursor: cursor
   538             cursor: cursor
   487         }, function(_data) {
   539         }, function(_data) {
   488             _this.onOccurrencesLoaded(_data);
   540             _this.onOccurrencesLoaded(_data);
   489             if (_data.cursor.hasNext) {
   541             if (_data.cursor && _data.cursor.hasNext) {
   490                 getData(_data.cursor.next);
   542                 getData(_data.cursor.next);
   491             }
   543             }
   492         });
   544         });
   493     }
   545     }
   494     
   546     
   551     _(this.occurrences).each(function(_occ) {
   603     _(this.occurrences).each(function(_occ) {
   552         _occ.x = _this.current_scale * (_occ.date - _this.start_time);
   604         _occ.x = _this.current_scale * (_occ.date - _this.start_time);
   553         _occ.y = _occ.univers.y;
   605         _occ.y = _occ.univers.y;
   554     });
   606     });
   555     var minT = this.timeFromX(-32),
   607     var minT = this.timeFromX(-32),
   556         minI = this.timescales[this.level].min_importance;
   608         maxI = this.timescales[this.level].max_importance;
   557     var _visible = _(this.occurrences).filter(function(_occ) {
   609     var _visible = _(this.occurrences).filter(function(_occ) {
   558         _occ.visible = (_occ.date >= minT && _occ.date <= _this.end_time && (_occ.importance >= minI));
   610         _occ.visible = (_occ.date >= minT && _occ.date <= _this.end_time && (_occ.importance <= maxI));
   559         return _occ.visible;
   611         return _occ.visible;
   560     });
   612     });
   561     
   613     
   562     /* FILTRAGE SI TROP D'OCCURRENCES PAR UNITE DE TEMPS */
   614     /* FILTRAGE SI TROP D'OCCURRENCES PAR UNITE DE TEMPS */
   563     /* Commenter la partie ci-dessous pour les tests */
   615     /* Commenter la partie ci-dessous pour les tests */
   716         this.detail_image = media.media.carre.replace(/carre\/[\d]+\/[\d]+/,'carre/135/135');
   768         this.detail_image = media.media.carre.replace(/carre\/[\d]+\/[\d]+/,'carre/135/135');
   717     }
   769     }
   718     if (typeinfo.picto) {
   770     if (typeinfo.picto) {
   719         this.image = this.timeline.picto_url + typeinfo.picto;
   771         this.image = this.timeline.picto_url + typeinfo.picto;
   720     }
   772     }
       
   773     var taxonomy = _(_data.contentHasTaxonomys).find(function(t) {
       
   774         return !!t.taxonomy.taxonomyHasMedias;
       
   775     });
       
   776     if (taxonomy) {
       
   777         var taxonomyMedia = _(taxonomy.taxonomy.taxonomyHasMedias).find(function(m) {
       
   778             return !!m.media.carre;
       
   779         });
       
   780         if (taxonomyMedia) {
       
   781             this.image = taxonomyMedia.media.carre.replace(/carre\/[\d]+\/[\d]+/,'carre/32/32');
       
   782         }
       
   783     }
   721     this.univers = this.timeline.univers[this.univers_id];
   784     this.univers = this.timeline.univers[this.univers_id];
   722     this.format = typeinfo.label;
   785     this.format = typeinfo.label;
   723     
   786     
   724     /* Données temporaires aléatoires */
   787     if (this.timeline.favoriteContents.indexOf(this.id) !== -1) {
   725     this.isFavorite = (Math.random() > 1/2); // A random Boolean ;-)
   788         this.isFavorite = true;
   726     if (this.univers_id >= 2) {
   789     }
   727         this.participationCount = Math.floor(12*Math.random());
   790     this.participationCount = this.timeline.participationCounts[this.id];
   728     }
       
   729     /* End Temporary Data */
   791     /* End Temporary Data */
   730     
   792     
   731     var _tmp = $('<p>').html(_data.resume || "");
   793     var _tmp = $('<p>').html(_data.resume || "");
   732     var trimmedDesc = _tmp.text().trim().replace(/(\n|\r|\r\n)/mg,' ');
   794     var trimmedDesc = _tmp.text().trim().replace(/(\n|\r|\r\n)/mg,' ');
   733     this.description = trimmedDesc.replace(/(^.{60,80})[\s].+$/m,'$1&hellip;');
   795     this.description = trimmedDesc.replace(/(^.{60,80})[\s].+$/m,'$1&hellip;');
   739     return "Occurrence " + this.type + ': "' + this.title + '"';
   801     return "Occurrence " + this.type + ': "' + this.title + '"';
   740 };
   802 };
   741 
   803 
   742 Tlns.Classes.Occurrence.prototype.toggleFavorite = function() {
   804 Tlns.Classes.Occurrence.prototype.toggleFavorite = function() {
   743     var newFavStatus = !this.isFavorite;
   805     var newFavStatus = !this.isFavorite;
   744     /* Add API Call HERE */
   806     
   745     
   807     $.ajax({
   746     /* The Following status update should be moved to the SUCCESS callback of the API call */
   808         type: "POST",
   747     this.isFavorite = newFavStatus;
   809         url: this.timeline.set_favorite_endpoint,
   748     this.timeline.throttledDrawGrid();
   810         data: {
   749 };
   811             type: this.type,
       
   812             conId: this.id,
       
   813             status: +newFavStatus,
       
   814             uid: this.timeline.user_id || undefined
       
   815         },
       
   816         success: function(r) {
       
   817             console.log(r);
       
   818             _this.isFavorite = newFavStatus;
       
   819             _this.timeline.throttledDrawGrid();
       
   820         }
       
   821     });
       
   822     
       
   823 };