Changeset 3196 for contribs

Show
Ignore:
Timestamp:
10/26/09 10:24:15 (3 years ago)
Author:
ochriste
Message:

refactored code to allow for easier code override

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • contribs/mapfish-api/trunk/MapFishApi/js/mapfish_api.js

    r3190 r3196  
    9696     */ 
    9797    apiName: 'MapFish', 
     98 
     99    /** 
     100     * Property: tools 
     101     * list of enabled tools's actions 
     102     */ 
     103    tools: null, 
    98104 
    99105    /** 
     
    431437     * config.items - array of function to activate. Possible values: 'ZoomToMaxExtent', 'Navigation', 'ZoomBox','LengthMeasure', 'AreaMeasure', 'NavigationHistory','ZoomOut', 'DrawFeature', 'ClearFeatures' 
    432438     */ 
     439 
    433440    createToolbar: function(config) { 
    434441        config = Ext.apply({items: [ 
     
    437444             'ZoomOut', 'DrawFeature', 'ClearFeatures' */ 
    438445        ]}, config); 
    439         var items = [], action; 
    440  
    441         // FIXME: be gentle with memory allocation 
    442         if (config.items.indexOf('ZoomToMaxExtent') != -1) { 
    443             action = new GeoExt.Action(Ext.apply({ 
     446        this.tools = []; 
     447         
     448        // init all enabled tools 
     449        for (var i = 0; i < config.items.length; i++) { 
     450          this['init' + config.items[i]](config); 
     451        } 
     452 
     453        return this.tools; 
     454    }, 
     455     
     456    initZoomToMaxExtent: function (config) { 
     457        var action = new GeoExt.Action(Ext.apply({ 
     458            map: this.map, 
     459            control: new MapFish.API.ZoomToExtent(config.controls), 
     460            iconCls: 'zoomfull' 
     461            //toggleGroup: 'navigation', 
     462            //allowDepress: false, 
     463            //text: "max extent" 
     464        }, config.actions)); 
     465        this.tools.push(action); 
     466    }, 
     467 
     468    initNavigation: function (config) { 
     469        var action = new Ext.Button(Ext.apply({ 
     470            toggleGroup: 'navigation', 
     471            allowDepress: false, 
     472            pressed: true, 
     473            id: 'navigationButton', 
     474            //text: 'nav', 
     475            iconCls: 'pan' 
     476        }, config.actions)); 
     477        this.tools.push(action); 
     478    }, 
     479 
     480    initZoomBox: function (config) { 
     481        var action = new GeoExt.Action(Ext.apply({ 
     482            map: this.map, 
     483            control: new OpenLayers.Control.ZoomBox(config.controls), 
     484            toggleGroup: 'navigation', 
     485            allowDepress: false, 
     486            //text: 'zoom box', 
     487            iconCls: 'zoomin' 
     488        }, config.actions)); 
     489        this.tools.push(action); 
     490    }, 
     491 
     492    initZoomOut: function (config) { 
     493        var action = new GeoExt.Action(Ext.apply({ 
     494            map: this.map, 
     495            control: new OpenLayers.Control.ZoomBox(Ext.apply({out: true}, config.controls)), 
     496            toggleGroup: 'navigation', 
     497            allowDepress: false, 
     498            //text: 'zoom box', 
     499            iconCls: 'zoomout' 
     500        }, config.actions)); 
     501        this.tools.push(action); 
     502    }, 
     503 
     504    initLengthMeasure: function (config) { 
     505        var measure = new MapFish.API.Measure(config.controls); 
     506        var action = new GeoExt.Action(Ext.apply({ 
     507            map: this.map, 
     508            control: measure.createLengthMeasureControl(), 
     509            toggleGroup: 'navigation', 
     510            allowDepress: false, 
     511            //text: 'length', 
     512            iconCls: 'measureLength' 
     513        }, config.actions)); 
     514        this.tools.push(action); 
     515    }, 
     516 
     517    initAreaMeasure: function (config) { 
     518        var measure = new MapFish.API.Measure(config.controls); 
     519        var action = new GeoExt.Action(Ext.apply({ 
     520            map: this.map, 
     521            control: measure.createAreaMeasureControl(), 
     522            toggleGroup: 'navigation', 
     523            allowDepress: false, 
     524            //text: 'area', 
     525            iconCls: 'measureArea' 
     526        }, config.actions)); 
     527        this.tools.push(action); 
     528    }, 
     529 
     530    initNavigationHistory: function (config) { 
     531        var history = new OpenLayers.Control.NavigationHistory(config.controls); 
     532        history.activate(); 
     533        this.map.addControl(history); 
     534 
     535        var action = new GeoExt.Action(Ext.apply({ 
     536            tooltip: OpenLayers.i18n("previous"), 
     537            control: history.previous, 
     538            iconCls: 'previous', 
     539            disabled: true 
     540        }, config.actions)); 
     541        this.tools.push(action); 
     542 
     543        action = new GeoExt.Action(Ext.apply({ 
     544            tooltip: OpenLayers.i18n("next"), 
     545            control: history.next, 
     546            iconCls: 'next', 
     547            disabled: true 
     548        }, config.actions)); 
     549        this.tools.push(action); 
     550    }, 
     551 
     552    initDrawFeature: function (config) { 
     553        var handlers = mapfish.Util.fixArray( 
     554                config.drawHandlers || ['Point', 'Path', 'Polygon']); 
     555        for (var i = 0; i < handlers.length; i++) { 
     556            var control = new OpenLayers.Control.DrawFeature( 
     557                    this.getDrawingLayer(), 
     558                    OpenLayers.Handler[handlers[i]], 
     559                    config.controls); 
     560            this.map.addControl(control); 
     561            var action = new GeoExt.Action(Ext.apply({ 
    444562                map: this.map, 
    445                 control: new MapFish.API.ZoomToExtent(config.controls), 
    446                 iconCls: 'zoomfull' 
    447                 //toggleGroup: 'navigation', 
    448                 //allowDepress: false, 
    449                 //text: "max extent" 
    450             }, config.actions)); 
    451             items.push(action); 
    452         } 
    453  
    454         if (config.items.indexOf('Navigation') != -1) { 
    455             action = new Ext.Button(Ext.apply({ 
     563                control: control, 
    456564                toggleGroup: 'navigation', 
    457565                allowDepress: false, 
    458                 pressed: true, 
    459                 id: 'navigationButton', 
    460                 //text: 'nav', 
    461                 iconCls: 'pan' 
     566                iconCls: 'draw' + handlers[i] 
    462567            }, config.actions)); 
    463             items.push(action); 
    464         } 
    465  
    466         if (config.items.indexOf('ZoomBox') != -1) { 
    467             action = new GeoExt.Action(Ext.apply({ 
    468                 map: this.map, 
    469                 control: new OpenLayers.Control.ZoomBox(config.controls), 
    470                 toggleGroup: 'navigation', 
    471                 allowDepress: false, 
    472                 //text: 'zoom box', 
    473                 iconCls: 'zoomin' 
    474             }, config.actions)); 
    475             items.push(action); 
    476         } 
    477  
    478         if (config.items.indexOf('ZoomOut') != -1) { 
    479             action = new GeoExt.Action(Ext.apply({ 
    480                 map: this.map, 
    481                 control: new OpenLayers.Control.ZoomBox(Ext.apply({out: true}, config.controls)), 
    482                 toggleGroup: 'navigation', 
    483                 allowDepress: false, 
    484                 //text: 'zoom box', 
    485                 iconCls: 'zoomout' 
    486             }, config.actions)); 
    487             items.push(action); 
    488         } 
    489  
    490         if (config.items.indexOf('LengthMeasure') != -1) { 
    491             var measure = new MapFish.API.Measure(config.controls); 
    492             action = new GeoExt.Action(Ext.apply({ 
    493                 map: this.map, 
    494                 control: measure.createLengthMeasureControl(), 
    495                 toggleGroup: 'navigation', 
    496                 allowDepress: false, 
    497                 //text: 'length', 
    498                 iconCls: 'measureLength' 
    499             }, config.actions)); 
    500             items.push(action); 
    501         } 
    502  
    503         if (config.items.indexOf('AreaMeasure') != -1) { 
    504             var measure = new MapFish.API.Measure(config.controls); 
    505             action = new GeoExt.Action(Ext.apply({ 
    506                 map: this.map, 
    507                 control: measure.createAreaMeasureControl(), 
    508                 toggleGroup: 'navigation', 
    509                 allowDepress: false, 
    510                 //text: 'area', 
    511                 iconCls: 'measureArea' 
    512             }, config.actions)); 
    513             items.push(action); 
    514         } 
    515  
    516         if (config.items.indexOf('NavigationHistory') != -1) { 
    517             var history = new OpenLayers.Control.NavigationHistory(config.controls); 
    518             history.activate(); 
    519             this.map.addControl(history); 
    520  
    521             action = new GeoExt.Action(Ext.apply({ 
    522                 tooltip: OpenLayers.i18n("previous"), 
    523                 control: history.previous, 
    524                 iconCls: 'previous', 
    525                 disabled: true 
    526             }, config.actions)); 
    527             items.push(action); 
    528  
    529             action = new GeoExt.Action(Ext.apply({ 
    530                 tooltip: OpenLayers.i18n("next"), 
    531                 control: history.next, 
    532                 iconCls: 'next', 
    533                 disabled: true 
    534             }, config.actions)); 
    535             items.push(action); 
    536         } 
    537  
    538         if (config.items.indexOf('DrawFeature') != -1) { 
    539             var handlers = mapfish.Util.fixArray( 
    540                     config.drawHandlers || ['Point', 'Path', 'Polygon']); 
    541             for (var i = 0; i < handlers.length; i++) { 
    542                 var control = new OpenLayers.Control.DrawFeature( 
    543                         this.getDrawingLayer(), 
    544                         OpenLayers.Handler[handlers[i]], 
    545                         config.controls); 
    546                 this.map.addControl(control); 
    547                 action = new GeoExt.Action(Ext.apply({ 
    548                     map: this.map, 
    549                     control: control, 
    550                     toggleGroup: 'navigation', 
    551                     allowDepress: false, 
    552                     iconCls: 'draw' + handlers[i] 
    553                 }, config.actions)); 
    554                 items.push(action); 
    555             } 
    556         } 
    557  
    558         if (config.items.indexOf('ClearFeatures') != -1) { 
    559             var scope = this; 
    560             action = new Ext.Button(Ext.apply({ 
    561                 handler: function() { 
    562                     scope.getDrawingLayer().destroyFeatures() 
    563                 }, 
    564                 iconCls: 'clearfeatures' 
    565             }, config.actions)); 
    566             items.push(action); 
    567         } 
    568  
    569         return items; 
     568            this.tools.push(action); 
     569        } 
     570    }, 
     571 
     572    initClearFeatures: function (config) { 
     573        var scope = this; 
     574        var action = new Ext.Button(Ext.apply({ 
     575            handler: function() { 
     576                scope.getDrawingLayer().destroyFeatures() 
     577            }, 
     578            iconCls: 'clearfeatures' 
     579        }, config.actions)); 
     580        this.tools.push(action); 
    570581    }, 
    571582