<commit_before>/**
 * Copyright (C) 2008, 2009 FBK Foundation, (http://www.fbk.eu)
 * Author: <NAME> @ SoNet Group
 *
 * OpenLayers-Timeline is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation version 3 of the License.
 */

/**
 * Class: OpenLayers.Timeline
 * This class represents a timeline based on jQuery-UI Slider
 */
OpenLayers.Timeline = OpenLayers.Class({
    map: undefined,
    slider: undefined,
    current_date: undefined,
    display_layer: undefined,
    current_data: undefined,
    timerId: undefined,
    selectControl: undefined,
    selectedFeature: undefined,
    interval: 5,
    speeds: ["Really slow", "Slow", "Normal", "Fast", "Really fast"],
    curr_speed: undefined,
    data_format: undefined,
    cumulative: true,
    timedelta: 15552000,  // 6 months
    first: undefined,
    onFeatureInsert: undefined,

    initialize: function(options) {
        this.map = options.map;
		    this.display_layer = this.createDisplayLayer(),
        this.map.addLayer(this.display_layer);
        this.createSelectControl();
        this.slider = options.timeline;
        this.curr_speed = parseInt(this.speeds.length / 2);
//        this.data_format = new options.format();
        this.data_format = options.format;
        if (options.date_key) {
            this.data_format.date_key = options.date_key;
        }
        this.data_format.timestamp_funct = options.date_funct;
		    this.onFeatureInsert = options.onFeatureInsert;

        var self = this;
        $(this.slider).slider({
            range: "min",
            min: 0,
            max: 100,
            value: 0,
            disabled: true,
            change: function (e, ui) {
                if (self.display_layer) {
                    
                    if (self.selectControl) {
						var i;
						for( i in self.map.popups ){
							var popup = self.map.popups[i];
							self.map.removePopup(popup);
							popup.selectedFeature.popup.destroy();
			                popup = null;
						}
						self.selectControl.deactivate();
						self.map.removeControl(self.selectControl);
					}

                    self.display_layer = self.updateDisplayLayer();

                    if (ui) {
                        self.data_format.past_seconds =
                          Math.ceil(self.first+(self.current_date-self.first)*(ui.value / 100.0));
                    }
                    if (!self.cumulative) {
                        self.data_format.lowerlimit = self.data_format.past_seconds - self.timedelta;
                    }
                    else {
                        self.data_format.lowerlimit = undefined;
                    }

                    self.createSelectControl();
                }
                var bounds = self.display_layer.getDataExtent();
                if (bounds && self.cumulative) {
                    self.map.zoomToExtent( bounds, false);
					          console.log( 'zoom: ', self.map.zoom );
                }
            },
            slide: function(e, ui) {
                self.stopBar();
                if (self.current_data) {
                    self.data_format.past_seconds =
                      Math.ceil(self.first+(self.current_date-self.first)*(ui.value / 100.0));
                }

                var d = new Date(self.data_format.past_seconds*1000);
                d = d.getDate()+"/"+(d.getMonth()+1)+"/"+d.getFullYear();
                var tooltip = $("<div/>")
                          .css({ position: 'absolute', top: "20px", left: "0px"})
                          .addClass("slider-tooltip")
                          .text(d);
                $(self.slider).slider()
                    .find(".ui-slider-handle")
                    .append(tooltip);
                setTimeout(function() { tooltip.remove(); }, 500);
            }
        });
    },

    defaultStyle: new OpenLayers.Style({
                                graphicName: "circle",
                                strokeColor: "#ff0000",
                                fillColor: "#ff0000",
                                pointRadius: "${radius}",
                                fillOpacity: 0.5,
                                strokeWidth: "${width}"
                            },
                            { context : {
                                    radius: function(feature) {
                                        if (feature.attributes.count == 1) return 3;
                                        return Math.min(Math.ceil(feature.attributes.count / 12), 25) + 5;
                                    },
                                width: function(feature) {
                                        return (feature.attributes.count > 1) ? 2 : 0.5;
                                    }
                                }
                            }),

    createSelectControl: function(){
			  var l = this.display_layer;
        this.selectControl = new OpenLayers.Control.SelectFeature(
                           [l],{clickout: true, toggle: false,
                                multiple: false, hover: false }
                             );
        this.map.addControl(this.selectControl);
        this.selectControl.activate();
        var self = this;
        l.events.on({
            "featureselected": function(e) {
                self.onFeatureSelect(e.feature);
            },
            "featureunselected": function(e) {
                self.onFeatureUnselect(e.feature);
            }
        });
     },
		
    createDisplayLayer: function() {
        if (!this.map) {
            return false;
        }
        var l = new OpenLayers.Layer.Vector("display", {
                    strategies: [new OpenLayers.Strategy.Cluster()],
                    styleMap: this.defaultStyle
                });
        return l;
    },

    updateDisplayLayer: function() {
		    var l = this.display_layer;
		    l.destroyFeatures();
				this.data_format.firstFeature = undefined;
				
        if (this.current_data) {
            l.addFeatures(this.data_format.read(this.current_data));
            this.first = this.data_format.first;
						this.current_date = this.data_format.current_date;
						
						if( l.features.length == 0 && this.data_format.firstFeature )
              l.addFeatures( this.data_format.firstFeature );
        }
        this.onFeatureInsert(l);
		    return l;
    },

    update: function() {
        $(this.slider).slider("value", $(this.slider).slider("value"));
    },

    onPopupClose: function(evt) {
       this.selectControl.unselect(this.selectedFeature);
    },

    onFeatureSelect: function(feature) {
    	if( feature.popup )
    		return;
        
    	if (feature.attributes.count == 0) 
        	return;
        
        this.selectedFeature = feature;
        var desc = "";
    	
        if (feature.attributes.count == 1) {
        	var point = feature.cluster[0];
            desc += "<h5>" + point.attributes.name + "</h5>";
		      if (point.attributes.description ) 
		        desc = desc + point.attributes.description;
        } 
        else if (feature.attributes.count > 1) {<commit_msg>Restore new format() and skip callback if undefined<commit_after>/**
 * Copyright (C) 2008, 2009 FBK Foundation, (http://www.fbk.eu)
 * Author: <NAME> @ SoNet Group
 *
 * OpenLayers-Timeline is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation version 3 of the License.
 */

/**
 * Class: OpenLayers.Timeline
 * This class represents a timeline based on jQuery-UI Slider
 */
OpenLayers.Timeline = OpenLayers.Class({
    map: undefined,
    slider: undefined,
    current_date: undefined,
    display_layer: undefined,
    current_data: undefined,
    timerId: undefined,
    selectControl: undefined,
    selectedFeature: undefined,
    interval: 5,
    speeds: ["Really slow", "Slow", "Normal", "Fast", "Really fast"],
    curr_speed: undefined,
    data_format: undefined,
    cumulative: true,
    timedelta: 15552000,  // 6 months
    first: undefined,
    onFeatureInsert: undefined,

    initialize: function(options) {
        this.map = options.map;
		    this.display_layer = this.createDisplayLayer(),
        this.map.addLayer(this.display_layer);
        this.createSelectControl();
        this.slider = options.timeline;
        this.curr_speed = parseInt(this.speeds.length / 2);
        this.data_format = new options.format();
//        this.data_format = options.format;
        if (options.date_key) {
            this.data_format.date_key = options.date_key;
        }
        this.data_format.timestamp_funct = options.date_funct;
		    this.onFeatureInsert = options.onFeatureInsert;

        var self = this;
        $(this.slider).slider({
            range: "min",
            min: 0,
            max: 100,
            value: 0,
            disabled: true,
            change: function (e, ui) {
                if (self.display_layer) {
                    
                    if (self.selectControl) {
						var i;
						for( i in self.map.popups ){
							var popup = self.map.popups[i];
							self.map.removePopup(popup);
							popup.selectedFeature.popup.destroy();
			                popup = null;
						}
						self.selectControl.deactivate();
						self.map.removeControl(self.selectControl);
					}

                    self.display_layer = self.updateDisplayLayer();

                    if (ui) {
                        self.data_format.past_seconds =
                          Math.ceil(self.first+(self.current_date-self.first)*(ui.value / 100.0));
                    }
                    if (!self.cumulative) {
                        self.data_format.lowerlimit = self.data_format.past_seconds - self.timedelta;
                    }
                    else {
                        self.data_format.lowerlimit = undefined;
                    }

                    self.createSelectControl();
                }
                var bounds = self.display_layer.getDataExtent();
                if (bounds && self.cumulative) {
                    self.map.zoomToExtent( bounds, false);
					          console.log( 'zoom: ', self.map.zoom );
                }
            },
            slide: function(e, ui) {
                self.stopBar();
                if (self.current_data) {
                    self.data_format.past_seconds =
                      Math.ceil(self.first+(self.current_date-self.first)*(ui.value / 100.0));
                }

                var d = new Date(self.data_format.past_seconds*1000);
                d = d.getDate()+"/"+(d.getMonth()+1)+"/"+d.getFullYear();
                var tooltip = $("<div/>")
                          .css({ position: 'absolute', top: "20px", left: "0px"})
                          .addClass("slider-tooltip")
                          .text(d);
                $(self.slider).slider()
                    .find(".ui-slider-handle")
                    .append(tooltip);
                setTimeout(function() { tooltip.remove(); }, 500);
            }
        });
    },

    defaultStyle: new OpenLayers.Style({
                                graphicName: "circle",
                                strokeColor: "#ff0000",
                                fillColor: "#ff0000",
                                pointRadius: "${radius}",
                                fillOpacity: 0.5,
                                strokeWidth: "${width}"
                            },
                            { context : {
                                    radius: function(feature) {
                                        if (feature.attributes.count == 1) return 3;
                                        return Math.min(Math.ceil(feature.attributes.count / 12), 25) + 5;
                                    },
                                width: function(feature) {
                                        return (feature.attributes.count > 1) ? 2 : 0.5;
                                    }
                                }
                            }),

    createSelectControl: function(){
			  var l = this.display_layer;
        this.selectControl = new OpenLayers.Control.SelectFeature(
                           [l],{clickout: true, toggle: false,
                                multiple: false, hover: false }
                             );
        this.map.addControl(this.selectControl);
        this.selectControl.activate();
        var self = this;
        l.events.on({
            "featureselected": function(e) {
                self.onFeatureSelect(e.feature);
            },
            "featureunselected": function(e) {
                self.onFeatureUnselect(e.feature);
            }
        });
     },
		
    createDisplayLayer: function() {
        if (!this.map) {
            return false;
        }
        var l = new OpenLayers.Layer.Vector("display", {
                    strategies: [new OpenLayers.Strategy.Cluster()],
                    styleMap: this.defaultStyle
                });
        return l;
    },

    updateDisplayLayer: function() {
		    var l = this.display_layer;
		    l.destroyFeatures();
				this.data_format.firstFeature = undefined;
				
        if (this.current_data) {
            l.addFeatures(this.data_format.read(this.current_data));
            this.first = this.data_format.first;
						this.current_date = this.data_format.current_date;
						
						if( l.features.length == 0 && this.data_format.firstFeature )
              l.addFeatures( this.data_format.firstFeature );
        }
        if( this.onFeatureInsert )
          this.onFeatureInsert(l);
		    return l;
    },

    update: function() {
        $(this.slider).slider("value", $(this.slider).slider("value"));
    },

    onPopupClose: function(evt) {
       this.selectControl.unselect(this.selectedFeature);
    },

    onFeatureSelect: function(feature) {
    	if( feature.popup )
    		return;
        
    	if (feature.attributes.count == 0) 
        	return;
        
        this.selectedFeature = feature;
        var desc = "";
    	
        if (feature.attributes.count == 1) {
        	var point = feature.cluster[0];
            desc += "<h5>" + point.attributes.name + "</h5>";
		      if (point.attributes.description ) 
		        desc = desc + point.attributes.description;
        } 
        else if (feature.attributes.count > 1) {
{'fraction_non_alphanumeric': 0.11246994160082446, 'fraction_numerical': 0.009893507385778082, 'mean_word_length': 1.4091360476663357, 'pattern_counts': {'":': 4, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7165782', 'n_tokens_mistral': 4178, 'n_tokens_neox': 3711, 'n_words': 739}