timeline/js/timeline.js
author veltr
Tue, 13 Aug 2013 16:22:32 +0200
changeset 89 6b1a038edbf7
parent 88 8bab0eb0d791
child 90 50e3c4ba168a
permissions -rw-r--r--
Removed List

/*
 * Main Timeline code
 */

window.Tlns = {
    Utils : {},
    Defaults : {},
    Templates : {},
    Classes : {}
};

/* Utility Functions */

Tlns.Utils.zeroPad = function(_n) {
    return (_n < 10 ? "0" : "") + _n;
}

Tlns.Utils.SetDefaults = function(_object, _defaults, _options) {
    var _options = _options || {};
    _(_defaults).each(function(_v, _k) {
        if(/^m(in|ax)_/.test(_k)) {
            var _tab = _k.split('_')
            if( typeof _object[_tab[1]] !== "undefined") {
                var _fn = Math[_tab[0] === "max" ? "min":"max"];
                _object[_tab[1]] = _fn(_object[_tab[1]], _v);
            }
        } else {
            if( typeof _options[_k] !== "undefined") {
                _object[_k] = _options[_k];
            } else {
                _object[_k] = _v;
            }
        }
    });
}

Tlns.Utils.dateFormat = function(_date, _template) {
    if (typeof _date !== "object") {
        _date = new Date(parseInt(_date));
    }
    var _params = {
        hours: _date.getHours(),
        isDayStart: !_date.getHours(),
        "0hours": Tlns.Utils.zeroPad(_date.getHours()),
        minutes: _date.getMinutes(),
        "0minutes": Tlns.Utils.zeroPad(_date.getMinutes()),
        seconds: _date.getSeconds(),
        "0seconds": Tlns.Utils.zeroPad(_date.getSeconds()),
        dayOfWeek: ["dimanche","lundi","mardi","mercredi","jeudi","vendredi","samedi"][_date.getDay()],
        shortDayOfWeek: ["Dim","Lun","Mar","Mer","Jeu","Ven","Sam"][_date.getDay()],
        dayOfMonth: _date.getDate(),
        "0dayOfMonth": Tlns.Utils.zeroPad(_date.getDate()),
        monthNumber: 1+_date.getMonth(),
        "0monthNumber": Tlns.Utils.zeroPad(1+_date.getMonth()),
        monthName: ["janvier","février","mars","avril","mai","juin","juillet","août","septembre","octobre","novembre","décembre"][_date.getMonth()],
        shortMonthName: ["jan","fev","mar","avr","mai","jun","jul","aou","sep","oct","nov","dec"][_date.getMonth()],
        year: _date.getFullYear()
    }
    return Mustache.to_html(_template, _params);
}

Tlns.Utils.guid = function() {
    return 'xxxx-xxxx-xxxx-xxxx'.replace(/x/g,function() {
        return Math.floor(Math.random()*16).toString(16);
    });
}

Tlns.Utils.timeFieldProcess = function(_val) {
    var _h = 0,
        _m = 0,
        _matches = _val.match(/(\d+)/g);
    if (_matches && _matches.length) {
        _h = Math.min(23, +(_matches[0]));
        if (_matches.length > 1) {
            _m = Math.min(59, +(_matches[1]));
        }
    }
    return {
        hours: _h,
        minutes: _m,
        text: Tlns.Utils.zeroPad(_h) + ':' + Tlns.Utils.zeroPad(_m)
    }
}

Tlns.Utils.dateFieldProcess = function(_val) {
    var _now = new Date(),
        _y = _now.getFullYear(),
        _m = 1 + _now.getMonth(),
        _d = _now.getDate(),
        _matches = _val.match(/(\d+)/g);
    if (_matches && _matches.length) {
        _d = Math.min(31, +(_matches[0]));
        if (_matches.length > 1) {
            _m = Math.min(12, +(_matches[1]));
        }
        if (_matches.length > 2) {
            _y = parseInt(_matches[2]);
            if (_y < 2000) {
                _y += 2000;
            }
            _y = Math.min(2020, Math.max(2000, _y));
        }
    }
    return {
        year: _y,
        month: _m,
        date: _d,
        text: Tlns.Utils.zeroPad(_d) + '/' + Tlns.Utils.zeroPad(_m) + '/' + _y
    }
}

/* Defaults */

Tlns.Defaults.Timeline = {
    email: "",
    token: "",
    container : "timeline",
    width : 780,
    height : 225,
    min_width : 400,
    min_height : 100,
    main_width : 700,
    linelabels : [],
    timescales : [{
        label : "Semaine",
        span : 7 * 86400 * 1000,
        grid_interval : 86400 * 1000,
        grid_date_format : '{{dayOfMonth}} {{monthName}}',
        start_date_format : '{{dayOfMonth}} {{shortMonthName}}',
        end_date_format : '{{dayOfMonth}} {{shortMonthName}}'
    }, {
        label : "3 jours",
        span : 3 * 86400 * 1000,
        grid_interval : 6 * 3600 * 1000,
        grid_date_format : '{{^isDayStart}}{{0hours}}h{{0minutes}}{{/isDayStart}}{{#isDayStart}}{{dayOfMonth}} {{shortMonthName}}{{/isDayStart}}',
        start_date_format : '{{dayOfMonth}} {{shortMonthName}}',
        end_date_format : '{{dayOfMonth}} {{shortMonthName}}'
    }, {
        label : "Journée",
        span : 86400 * 1000,
        grid_interval : 2 * 3600 * 1000,
        grid_date_format : '{{^isDayStart}}{{0hours}}h{{0minutes}}{{/isDayStart}}{{#isDayStart}}{{dayOfMonth}} {{shortMonthName}}{{/isDayStart}}',
        start_date_format : '{{dayOfMonth}} {{shortMonthName}} {{hours}}h',
        end_date_format : '{{dayOfMonth}} {{shortMonthName}} {{hours}}h'
    }, {
        label : "Demi-Journée",
        span : 6 * 3600 * 1000,
        grid_interval : 3600 * 1000,
        grid_date_format : '{{^isDayStart}}{{0hours}}h{{0minutes}}{{/isDayStart}}{{#isDayStart}}{{dayOfMonth}} {{shortMonthName}}{{/isDayStart}}',
        start_date_format : '{{dayOfMonth}} {{shortMonthName}} {{hours}}h',
        end_date_format : '{{dayOfMonth}} {{shortMonthName}} {{hours}}h'
    }],
    level: 0,
    central_time: 0,
    sync_now: true,
    api_endpoint: "",
    api_method: "searchForTimelineEdito",
    occurrences: [],
    cluster_spacing: 12,
    tooltip_date_format: '{{dayOfMonth}} {{shortMonthName}} {{year}} {{0hours}}:{{0minutes}}',
    statuses: {
        "valide": "Validée",
        "a_valider": "A valider",
        "a_realiser": "A réaliser"
    }
}

for (var _i = 0; _i < Tlns.Defaults.Timeline.timescales.length; _i++) {
    Tlns.Defaults.Timeline.timescales[_i].level = _i;
}

/* Templates */

Tlns.Templates.Timeline = '<div class="Tl-Main"><div class="Tl-Grid"></div><div class="Tl-TopBar"></div>'
    + '<div class="Tl-BottomPart"><ul class="Tl-UniversLabels"></ul>'
    + '<div class="Tl-MainPart"><div class="Tl-Occurrences"></div>'
    + '</div>'
    + '<div class="Tl-Overlay-Container"><div class="Tl-Overlay-Box"><div class="Tl-Overlay"><div class="Tl-Overlay-Tip-Top"></div><div class="Tl-Overlay-Main"></div><div class="Tl-Overlay-Tip-Bottom"></div></div></div></div></div></div>'
    
Tlns.Templates.Univers = '<div class="Tl-UniversText">{{title}}</div>';

Tlns.Templates.Occurrence = '{{#clusters}}<div class="Tl-Cluster" style="left: {{x}}px; top: {{y}}px;" cluster-contents="{{contents}}">'
    + '<div class="Tl-ClusterCount">{{occurrences.length}}</div></div>{{/clusters}}'
    + '{{#occurrences}}<div class="Tl-Occurrence Tl-OccOnGrid Tl-Occ{{type}} Tl-Occ{{status}}{{#editing}} Tl-Editing{{/editing}}" occurrence-id="{{id}}" style="left: {{x}}px; top: {{y}}px;">'
//  + '{{#locked}}<div class="Tl-Locked"></div>{{/locked}}'
    + '</div>{{/occurrences}}{{#open_cluster}}<div class="Tl-ClusterOverlay" style="left: {{x}}px; top: {{y}}px;">'
    + '{{#occurrences}}<div class="Tl-Occurrence Tl-OccInCluster Tl-Occ{{type}} Tl-Occ{{status}}{{#editing}} Tl-Editing{{/editing}}" occurrence-id="{{id}}">'
    + '{{#locked}}<div class="Tl-Locked"></div>{{/locked}}</div>{{/occurrences}}</div>{{/open_cluster}}';

Tlns.Templates.OccurrenceTooltip = '<h3 class="Tl-Tooltip-Title">{{title}}</h3><p class="Tl-Tooltip-Date">{{formatted_date}} &mdash; {{translated_status}} &mdash; {{#jt}}Au JT{{/jt}}{{^jt}}Hors JT{{/jt}}{{#format}} &mdash; {{format}}{{/format}}</p>'
    + '<p class="Tl-Tooltip-Description">{{description}}</p>'
//    + '<p class="Tl-Tooltip-Characters">{{univers.mainCharacter}}{{#characters}}, {{.}}{{/characters}}</p>'

/* Classes */

Tlns.Classes.Timeline = function(_options) {

    /* Setting Defaults */
    Tlns.Utils.SetDefaults(this, Tlns.Defaults.Timeline, _options);

    /* Setting container CSS */
    this.$ = $('#' + this.container).html(Mustache.to_html(Tlns.Templates.Timeline, this));
    
    this.$.find('.Tl-Main').css({
        width : this.width + "px",
        height : this.height + "px"
    });
    this.top_height = this.$.find('.Tl-TopBar').outerHeight();
    this.main_height = this.height - this.top_height;
    //this.main_height = this.height - 27;
    this.$.find('.Tl-BottomPart').css("height", this.main_height + "px");
    this.$.find('.Tl-MainPart, .Tl-Grid').css("width", this.main_width + "px");
    this.$.find('.Tl-Overlay-Container').css("left", (this.$.find('.Tl-BottomPart').outerWidth() - this.main_width) + "px");
    
    var _o = this.$.find('.Tl-MainPart').offset();
    this.dragging_bounds = {
        left: _o.left,
        top: _o.top,
        right: _o.left + this.$.find('.Tl-MainPart').outerWidth(),
        bottom: _o.top + this.$.find('.Tl-MainPart').outerHeight(),
    };
    this.$.find('.Tl-UniversLabels').css({
        width: this.width - this.main_width
    });
    
    var _this = this;
    
    this.throttledDrawGrid = _.throttle(function() {
        _this.drawGrid();
    }, 150);
    
    this.setLevel(this.level);
     
    this.$.find('.Tl-MainPart').mousedown(function(_event) {
        _this.onMouseDown(_event);
        return false;
    });
    
    this.$.find('.Tl-MainPart').mousemove(function(_event) {
        _this.onMouseMove(_event);
        return false;
    });
    
    this.$.find('.Tl-MainPart').mouseup(function(_event) {
        _this.onMouseUp(_event);
        return false;
    });
    
    this.$.find('.Tl-MainPart').mousewheel(function(_event, _delta) {
        var _newLevel = Math.max(0,Math.min(_this.timescales.length-1, (_delta < 0 ? -1 : 1) + parseInt(_this.level)));
        if (_newLevel != _this.level) {
            _this.hideTooltip();
            var _deltaX = _event.pageX - _this.dragging_bounds.left,
                _tAtMouse = _this.timeFromMouse(_event.pageX),
                _newScale = _this.main_width / (_this.timescales[_newLevel].span),
                _newStart = _tAtMouse - _deltaX / _newScale;
            _this.central_time = _newStart + _this.timescales[_newLevel].span / 2;
            _this.setLevel(_newLevel);
        }
        return false;
    });
    
    this.$.find('.Tl-Overlay-Box').mouseover(function(_event) {
        $(this).show();
    }).mouseout(function(_event) {
        $(this).hide();
    });
    
       
    this.onUniversLoaded(this.linelabels);
    
}

Tlns.Classes.Timeline.prototype.onMouseDown = function(_event) {
    this.mouse_down = true;
    this.is_dragging = false;
    this.start_pos = {
        x: _event.pageX,
        y: _event.pageY
    };
    if (typeof this.dragging_type === "undefined") {
        this.time_at_start = this.central_time;
        this.dragging_type = "timeline";
    }
}

Tlns.Classes.Timeline.prototype.onMouseUp = function(_event) {
    this.mouse_down = false;
    this.is_dragging = false;
    this.dragging_type = undefined;
}

Tlns.Classes.Timeline.prototype.timeFromX = function(_x) {
    return Math.max(this.start_time,Math.min(this.end_time, this.start_time + _x / this.current_scale));
}

Tlns.Classes.Timeline.prototype.timeFromMouse = function(_pageX) {
    return this.timeFromX(_pageX - this.dragging_bounds.left);
}

Tlns.Classes.Timeline.prototype.universFromY = function(_y) {
    return Math.max(0,Math.min(this.univers.length, Math.floor(_y / this.univers_height)))
}

Tlns.Classes.Timeline.prototype.universFromMouse = function(_pageY) {
    return this.universFromY(_pageY - this.dragging_bounds.top);
}

Tlns.Classes.Timeline.prototype.onMouseMove = function(_event) {
    if (this.mouse_down && !this.is_dragging) {
        var _dx = this.start_pos.x - _event.pageX,
            _dy = this.start_pos.y - _event.pageY,
            _sqd = _dx * _dx + _dy * _dy;
        if (_sqd > 16) {
            this.is_dragging = true;
        }
    }
    if (this.is_dragging) {
        this.hideTooltip();
        switch (this.dragging_type) {
            case "timeline":
                this.setTime(this.time_at_start + Math.floor(( this.start_pos.x - _event.pageX ) / this.current_scale));
            break;
        }
    }
}

Tlns.Classes.Timeline.prototype.onUniversLoaded = function(_data) {
    this.univers = [];
    if(_data.length) {
        this.univers_height = Math.floor(this.main_height / _data.length);
    }
    for(var _i = 0; _i < _data.length; _i++) {
        this.univers.push(new Tlns.Classes.Univers(_data[_i], this, _i));
    }
    
    this.loadOccurrences();
}

Tlns.Classes.Timeline.prototype.offsetTime = function(_timeOffset) {
    this.setTime(this.central_time + _timeOffset);
}

Tlns.Classes.Timeline.prototype.setTime = function(_centralTime) {
    this.sync_now = false;
    this.central_time = _centralTime;
    this.changeSpan();
}

Tlns.Classes.Timeline.prototype.setLevel = function(_level) {
    if (_level >= 0 && _level < this.timescales.length) {
        this.level = _level;
        this.changeSpan();
    }
}

Tlns.Classes.Timeline.prototype.changeSpan = function() {
    var _now = new Date().valueOf();
    if (this.sync_now) {
        this.central_time = _now;
    }
    var _timescale = this.timescales[this.level];
    this.current_scale = this.main_width / (_timescale.span);
    this.start_time = this.central_time - (_timescale.span / 2);
    this.end_time = this.central_time + (_timescale.span / 2);
    this.throttledDrawGrid();
}

Tlns.Classes.Timeline.prototype.drawGrid = function() {
    var _now = new Date().valueOf(),
        _timescale = this.timescales[this.level],
        _offset = new Date().getTimezoneOffset() * 60000,
        _grid_width = Math.floor(_timescale.grid_interval * this.current_scale),
        _roundstart = Math.floor((this.start_time - _offset) / _timescale.grid_interval) * _timescale.grid_interval + _offset,
        _html = '';
    for (var _t = _roundstart; _t < this.end_time; _t += _timescale.grid_interval) {
        var _x = this.current_scale * (_t - this.start_time);
        if (_x > 0) {
            _html += '<div class="Tl-Grid-Column" style="width:' + _grid_width + 'px; left: ' + _x + 'px">'
            + '<div class="Tl-Grid-Label">' + Tlns.Utils.dateFormat(_t, _timescale.grid_date_format) + '</div></div>';
        }
    }
    if (this.start_time <= _now && this.end_time >= _now) {
        _html += '<div class="Tl-Grid-Now" style="left: ' + this.current_scale * (_now - this.start_time) + 'px"></div>'
    }
    this.$.find('.Tl-Grid').html(_html);
    this.drawOccurrences();
}

Tlns.Classes.Timeline.prototype.loadOccurrences = function() {
    var _this = this;
    $.getJSON(this.api_endpoint, {
        method: this.api_method,
        api_key: this.token,
        mail: this.email
    }, function(_data) {
        console.log(_data);
        _this.onOccurrencesLoaded(_data);
    });
    
}

Tlns.Classes.Timeline.prototype.onOccurrencesLoaded = function(_data) {
    for (var _i = 0; _i < _data.data.length; _i++) {
        this.createOrUpdateOccurrence(_data.data[_i]);
    }
    if (!this.mouse_down) {
        this.drawOccurrences();
    }
}

Tlns.Classes.Timeline.prototype.deleteOccurrence = function(_id) {
    this.occurrences = _(this.occurrences).reject(function(_occ) {
        return _occ.id == _id;
    });
}

Tlns.Classes.Timeline.prototype.getOccurrence = function(_id) {
    return _(this.occurrences).find(function(_occ) {
        return _occ.id == _id;
    });
}

Tlns.Classes.Timeline.prototype.createOrUpdateOccurrence = function(_data) {
    var _id = _data.id,
        _occurrence = this.getOccurrence(_id);
    if (typeof _occurrence === "undefined") {
        _occurrence = new Tlns.Classes.Occurrence(this);
        this.occurrences.push(_occurrence);
    }
    _occurrence.update(_data);
    return _occurrence;
}

Tlns.Classes.Timeline.prototype.showTooltip = function(_x, _y, _html, _isUp) {
    this.$.find('.Tl-Overlay-Box')
        .removeClass(_isUp ? 'Tl-Overlay-Down' : 'Tl-Overlay-Up')
        .addClass(_isUp ? 'Tl-Overlay-Up' : 'Tl-Overlay-Down')
        .show()
        .css({
            left: _x + "px",
            top: _y + "px"
        });
    this.$.find('.Tl-Overlay-Main').html(_html);
    
}

Tlns.Classes.Timeline.prototype.hideTooltip = function() {
    this.$.find('.Tl-Overlay-Box').hide();
}

Tlns.Classes.Timeline.prototype.drawOccurrences = function() {
    var _this = this;
    _(this.occurrences).each(function(_occ) {
        _occ.x = _this.current_scale * (_occ.date - _this.start_time);
        _occ.y = _occ.univers.y;
        _occ.in_cluster = false;
    });
    var _visible = _(this.occurrences).filter(function(_occ) {
        return (_occ.date >= _this.start_time && _occ.date <= _this.end_time && _occ.status);
    });
    
    var _moved = true;
    while (_moved) {
        _moved = false;
        for (var _i = 0; _i < _visible.length; _i++) {
            for (var _j = 0; _j < _i; _j++) {
                if (_visible[_j].univers_id == _visible[_i].univers_id
                    && _visible[_j].x != _visible[_i].x
                    && Math.abs(_visible[_j].x-_visible[_i].x) < this.cluster_spacing
                ) {
                    _moved = true;
                    _visible[_i].x = this.cluster_spacing * Math.round(_visible[_i].x / this.cluster_spacing);
                    _visible[_j].x = this.cluster_spacing * Math.round(_visible[_j].x / this.cluster_spacing);
                }
            }
        }
    }
    var _clusters = [],
        _openCluster = false;
    for (var _i = 0; _i < _visible.length; _i++) {
        for (var _j = 0; _j < _i; _j++) {
            if (_visible[_j].univers_id == _visible[_i].univers_id && _visible[_j].x == _visible[_i].x) {
                _visible[_j].in_cluster = true;
                _visible[_i].in_cluster = true;
                var _x = _visible[_j].x,
                    _y = _visible[_j].y;
                    _cluster = _(_clusters).find(function(_c) { return _c.x == _x && _c.y == _y });
                if (typeof _cluster === "undefined") {
                    _cluster = { x: _x, y: _y, occurrences: [] };
                    _clusters.push(_cluster);
                }
                if ("undefined" === typeof _(_cluster.occurrences).find(function(_o) {
                    return _o.type == _visible[_j].type && _o.id == _visible[_j].id;
                })) {
                    _cluster.occurrences.push(_visible[_j]);
                }
                if ("undefined" === typeof _(_cluster.occurrences).find(function(_o) {
                    return _o.type == _visible[_i].type && _o.id == _visible[_i].id;
                })) {
                    _cluster.occurrences.push(_visible[_i]);
                }
            }
        }
    }
    _(_clusters).each(function(_cluster) {
        _cluster.occurrences = _(_cluster.occurrences).sortBy(function(_o) {
            return _o.date;
        });
        _cluster.contents = _cluster.occurrences.map(function(_o) {
            return _o.id;
        }).join("|");
        if (_cluster.contents == _this.open_cluster) {
            _openCluster = _cluster;
        }
    });
    
    var _html = Mustache.to_html(Tlns.Templates.Occurrence, {
        occurrences:_(_visible).reject(function(_o) {return _o.in_cluster}),
        clusters: _clusters,
        open_cluster: _openCluster
    });
    this.$.find('.Tl-Occurrences').html(_html);
    
    
    if (_openCluster) {
        var _w = this.$.find('.Tl-Occurrence').width(),
            _ww = _w * _openCluster.occurrences.length;
        this.$.find('.Tl-ClusterOverlay').css({
            "margin-left": - Math.floor(_ww/2) + "px",
            width: _ww
        });
        _(_openCluster.occurrences).each(function(_o, _i) {
            _o.y = _o.y - 20;
            _o.x = _o.x - (_ww / 2) + ((_i + .5) * _w);
        });
    }
    
    this.$.find('.Tl-Occurrence').mousedown(function() {
        var _el = $(this),
            _id = _el.attr("occurrence-id");
        if (typeof _id !== "undefined") {
            _this.editing_occurrence = _this.getOccurrence(_id);
            if (typeof _this.dragging_type === "undefined" && typeof _this.editing_occurrence !== "undefined" /* && !_this.editing_occurrence.locked */ ) {
                _this.dragging_type = "occurrence";
            }
            if (!_this.editing_occurrence.editing) {
                _(_this.occurrences).each(function(_occ) {
                    _occ.editing = false;
                });
                _this.editing_occurrence.editing = true;
            }
            _this.throttledDrawGrid();
        }
    }).mouseover(function(_event) {
        var _el = $(this),
            _id = _el.attr("occurrence-id");
        if (typeof _id !== "undefined") {
            var _occurrence = _this.getOccurrence(_id);
            if (!_this.is_dragging) {
                var _html = Mustache.to_html(Tlns.Templates.OccurrenceTooltip, _occurrence);
                _this.showTooltip(_occurrence.x, _occurrence.y, _html, (_event.pageY - _this.dragging_bounds.top) >= (.4 * _this.main_height) );
            }
        }
    }).mouseout(function() {
        var _el = $(this),
            _id = _el.attr("occurrence-id");
        if (typeof _id !== "undefined") {
            var _occurrence = _this.getOccurrence(_id);
            _this.hideTooltip();
        }
    });
        
    this.$.find('.Tl-Cluster').click(function() {
        var _el = $(this),
            _contents = _el.attr("cluster-contents");
        if (_this.open_cluster == _contents) {
            _this.open_cluster = false;
        } else {
            _this.open_cluster = _contents;
        }
        _this.throttledDrawGrid();
    })
}

Tlns.Classes.Timeline.prototype.getUnivers = function(_id) {
    return _(this.univers).find(function(_univ) {
        return (_univ.id == _id);
    });
}

/*
 * Univers
 */

Tlns.Classes.Univers = function(_data, _timeline, _index) {
    this.id = "u_" + _index;
    this.index = _index;
    this.title = _data;
//    this.mainCharacter = _data.personnage;
    this.y = (_timeline.univers_height * _index);

    this.$label = $('<li>').css({
        height : _timeline.univers_height + "px"
    }).html(Mustache.to_html(Tlns.Templates.Univers, this));
    
    _timeline.$.find('.Tl-UniversLabels').append(this.$label);
    
    var txtdiv = this.$label.find(".Tl-UniversText");
    txtdiv.css("margin-top", Math.floor((_timeline.univers_height - txtdiv.height()) / 2));
}

/*
 * Occurrence
 */

Tlns.Classes.Occurrence = function(_timeline) {
    this.timeline = _timeline;
}

Tlns.Classes.Occurrence.prototype.update = function(_data) {
    this.original_data = _data;
    this.id = _data.id;
    this.date = new Date(1000 * (_data.dateFirstPublication || _data.dateCreate) || Date.now);
    this.formatted_date = Tlns.Utils.dateFormat(this.date,Tlns.Defaults.Timeline.tooltip_date_format);
    this.title = _data.title;
    this.univers_id = this.timeline.univers[Math.floor(this.timeline.univers.length * Math.random())].id;
    this.univers = this.timeline.getUnivers(this.univers_id);
    this.type = "publication";
    if (typeof _data.statut !== "undefined" || typeof this.status === "undefined") {
        switch(_data.statut) {
            case "Validée":
            case "valide":
                this.status = "valide"
            break;
            case "A réaliser":
            case "a_realiser":
                this.status = "a_realiser";
            break;
            case "A valider":
            case "a_valider":
            default:
                this.status = "a_valider";
        }
    }
    if (typeof _data.typeOccurrencePublication !== "undefined" || typeof this.format === "undefined") {
        this.format = _data.typeOccurrencePublication || 'Format non défini';
    }
    this.translated_status = Tlns.Defaults.Timeline.statuses[this.status];
//    this.published = (_data.publication && _data.publication == "En ligne");
//    this.locked = _data.verrouille || false;
//    this.characters = _data.personnagesSecondaires || [];
    var _tmp = $('<p>').html(_data.resume || "");
    this.description = _tmp.text().trim().replace(/(\n|\r|\r\n)/mg,' ').replace(/(^.{60,80})[\s].+$/m,'$1&hellip;');
}

Tlns.Classes.Occurrence.prototype.addDependency = function(_id) {
    if (_(this.dependsOn).indexOf(_id) == -1) {
        this.dependsOn.push(_id);
    }
}

Tlns.Classes.Occurrence.prototype.removeDependency = function(_id) {
    this.dependsOn = _(this.dependsOn).reject(function(_n) {
        return _n == _id;
    });
}

Tlns.Classes.Occurrence.prototype.toString = function() {
    return "Occurrence " + this.type + ': "' + this.title + '"';
}