[76] | 1 | /** |
---|
| 2 | * Copyright (c) 2008-2010 The Open Source Geospatial Foundation |
---|
| 3 | * |
---|
| 4 | * Published under the BSD license. |
---|
| 5 | * See http://svn.geoext.org/core/trunk/geoext/license.txt for the full text |
---|
| 6 | * of the license. |
---|
| 7 | */ |
---|
| 8 | |
---|
| 9 | /** api: (define) |
---|
| 10 | * module = GeoExt |
---|
| 11 | * class = Action |
---|
| 12 | * base_link = `Ext.Action <http://dev.sencha.com/deploy/dev/docs/?class=Ext.Action>`_ |
---|
| 13 | */ |
---|
| 14 | Ext.namespace("GeoExt"); |
---|
| 15 | |
---|
| 16 | /** api: example |
---|
| 17 | * Sample code to create a toolbar with an OpenLayers control into it. |
---|
| 18 | * |
---|
| 19 | * .. code-block:: javascript |
---|
| 20 | * |
---|
| 21 | * var action = new GeoExt.Action({ |
---|
| 22 | * text: "max extent", |
---|
| 23 | * control: new OpenLayers.Control.ZoomToMaxExtent(), |
---|
| 24 | * map: map |
---|
| 25 | * }); |
---|
| 26 | * var toolbar = new Ext.Toolbar([action]); |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | /** api: constructor |
---|
| 30 | * .. class:: Action(config) |
---|
| 31 | * |
---|
| 32 | * Create a GeoExt.Action instance. A GeoExt.Action is created |
---|
| 33 | * to insert an OpenLayers control in a toolbar as a button or |
---|
| 34 | * in a menu as a menu item. A GeoExt.Action instance can be |
---|
| 35 | * used like a regular Ext.Action, look at the Ext.Action API |
---|
| 36 | * doc for more detail. |
---|
| 37 | */ |
---|
| 38 | GeoExt.Action = Ext.extend(Ext.Action, { |
---|
| 39 | |
---|
| 40 | /** api: config[control] |
---|
| 41 | * ``OpenLayers.Control`` The OpenLayers control wrapped in this action. |
---|
| 42 | */ |
---|
| 43 | control: null, |
---|
| 44 | |
---|
| 45 | /** api: config[map] |
---|
| 46 | * ``OpenLayers.Map`` The OpenLayers map that the control should be added |
---|
| 47 | * to. For controls that don't need to be added to a map or have already |
---|
| 48 | * been added to one, this config property may be omitted. |
---|
| 49 | */ |
---|
| 50 | map: null, |
---|
| 51 | |
---|
| 52 | /** private: property[uScope] |
---|
| 53 | * ``Object`` The user-provided scope, used when calling uHandler, |
---|
| 54 | * uToggleHandler, and uCheckHandler. |
---|
| 55 | */ |
---|
| 56 | uScope: null, |
---|
| 57 | |
---|
| 58 | /** private: property[uHandler] |
---|
| 59 | * ``Function`` References the function the user passes through |
---|
| 60 | * the "handler" property. |
---|
| 61 | */ |
---|
| 62 | uHandler: null, |
---|
| 63 | |
---|
| 64 | /** private: property[uToggleHandler] |
---|
| 65 | * ``Function`` References the function the user passes through |
---|
| 66 | * the "toggleHandler" property. |
---|
| 67 | */ |
---|
| 68 | uToggleHandler: null, |
---|
| 69 | |
---|
| 70 | /** private: property[uCheckHandler] |
---|
| 71 | * ``Function`` References the function the user passes through |
---|
| 72 | * the "checkHandler" property. |
---|
| 73 | */ |
---|
| 74 | uCheckHandler: null, |
---|
| 75 | |
---|
| 76 | /** private */ |
---|
| 77 | constructor: function(config) { |
---|
| 78 | |
---|
| 79 | // store the user scope and handlers |
---|
| 80 | this.uScope = config.scope; |
---|
| 81 | this.uHandler = config.handler; |
---|
| 82 | this.uToggleHandler = config.toggleHandler; |
---|
| 83 | this.uCheckHandler = config.checkHandler; |
---|
| 84 | |
---|
| 85 | config.scope = this; |
---|
| 86 | config.handler = this.pHandler; |
---|
| 87 | config.toggleHandler = this.pToggleHandler; |
---|
| 88 | config.checkHandler = this.pCheckHandler; |
---|
| 89 | |
---|
| 90 | // set control in the instance, the Ext.Action |
---|
| 91 | // constructor won't do it for us |
---|
| 92 | var ctrl = this.control = config.control; |
---|
| 93 | delete config.control; |
---|
| 94 | |
---|
| 95 | // register "activate" and "deactivate" listeners |
---|
| 96 | // on the control |
---|
| 97 | if(ctrl) { |
---|
| 98 | // If map is provided in config, add control to map. |
---|
| 99 | if(config.map) { |
---|
| 100 | config.map.addControl(ctrl); |
---|
| 101 | delete config.map; |
---|
| 102 | } |
---|
| 103 | if((config.pressed || config.checked) && ctrl.map) { |
---|
| 104 | ctrl.activate(); |
---|
| 105 | } |
---|
| 106 | ctrl.events.on({ |
---|
| 107 | activate: this.onCtrlActivate, |
---|
| 108 | deactivate: this.onCtrlDeactivate, |
---|
| 109 | scope: this |
---|
| 110 | }); |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | arguments.callee.superclass.constructor.call(this, config); |
---|
| 114 | }, |
---|
| 115 | |
---|
| 116 | /** private: method[pHandler] |
---|
| 117 | * :param cmp: ``Ext.Component`` The component that triggers the handler. |
---|
| 118 | * |
---|
| 119 | * The private handler. |
---|
| 120 | */ |
---|
| 121 | pHandler: function(cmp) { |
---|
| 122 | var ctrl = this.control; |
---|
| 123 | if(ctrl && |
---|
| 124 | ctrl.type == OpenLayers.Control.TYPE_BUTTON) { |
---|
| 125 | ctrl.trigger(); |
---|
| 126 | } |
---|
| 127 | if(this.uHandler) { |
---|
| 128 | this.uHandler.apply(this.uScope, arguments); |
---|
| 129 | } |
---|
| 130 | }, |
---|
| 131 | |
---|
| 132 | /** private: method[pTogleHandler] |
---|
| 133 | * :param cmp: ``Ext.Component`` The component that triggers the toggle handler. |
---|
| 134 | * :param state: ``Boolean`` The state of the toggle. |
---|
| 135 | * |
---|
| 136 | * The private toggle handler. |
---|
| 137 | */ |
---|
| 138 | pToggleHandler: function(cmp, state) { |
---|
| 139 | this.changeControlState(state); |
---|
| 140 | if(this.uToggleHandler) { |
---|
| 141 | this.uToggleHandler.apply(this.uScope, arguments); |
---|
| 142 | } |
---|
| 143 | }, |
---|
| 144 | |
---|
| 145 | /** private: method[pCheckHandler] |
---|
| 146 | * :param cmp: ``Ext.Component`` The component that triggers the check handler. |
---|
| 147 | * :param state: ``Boolean`` The state of the toggle. |
---|
| 148 | * |
---|
| 149 | * The private check handler. |
---|
| 150 | */ |
---|
| 151 | pCheckHandler: function(cmp, state) { |
---|
| 152 | this.changeControlState(state); |
---|
| 153 | if(this.uCheckHandler) { |
---|
| 154 | this.uCheckHandler.apply(this.uScope, arguments); |
---|
| 155 | } |
---|
| 156 | }, |
---|
| 157 | |
---|
| 158 | /** private: method[changeControlState] |
---|
| 159 | * :param state: ``Boolean`` The state of the toggle. |
---|
| 160 | * |
---|
| 161 | * Change the control state depending on the state boolean. |
---|
| 162 | */ |
---|
| 163 | changeControlState: function(state) { |
---|
| 164 | if(state) { |
---|
| 165 | if(!this._activating) { |
---|
| 166 | this._activating = true; |
---|
| 167 | this.control.activate(); |
---|
| 168 | this._activating = false; |
---|
| 169 | } |
---|
| 170 | } else { |
---|
| 171 | if(!this._deactivating) { |
---|
| 172 | this._deactivating = true; |
---|
| 173 | this.control.deactivate(); |
---|
| 174 | this._deactivating = false; |
---|
| 175 | } |
---|
| 176 | } |
---|
| 177 | }, |
---|
| 178 | |
---|
| 179 | /** private: method[onCtrlActivate] |
---|
| 180 | * |
---|
| 181 | * Called when this action's control is activated. |
---|
| 182 | */ |
---|
| 183 | onCtrlActivate: function() { |
---|
| 184 | var ctrl = this.control; |
---|
| 185 | if(ctrl.type == OpenLayers.Control.TYPE_BUTTON) { |
---|
| 186 | this.enable(); |
---|
| 187 | } else { |
---|
| 188 | // deal with buttons |
---|
| 189 | this.safeCallEach("toggle", [true]); |
---|
| 190 | // deal with check items |
---|
| 191 | this.safeCallEach("setChecked", [true]); |
---|
| 192 | } |
---|
| 193 | }, |
---|
| 194 | |
---|
| 195 | /** private: method[onCtrlDeactivate] |
---|
| 196 | * |
---|
| 197 | * Called when this action's control is deactivated. |
---|
| 198 | */ |
---|
| 199 | onCtrlDeactivate: function() { |
---|
| 200 | var ctrl = this.control; |
---|
| 201 | if(ctrl.type == OpenLayers.Control.TYPE_BUTTON) { |
---|
| 202 | this.disable(); |
---|
| 203 | } else { |
---|
| 204 | // deal with buttons |
---|
| 205 | this.safeCallEach("toggle", [false]); |
---|
| 206 | // deal with check items |
---|
| 207 | this.safeCallEach("setChecked", [false]); |
---|
| 208 | } |
---|
| 209 | }, |
---|
| 210 | |
---|
| 211 | /** private: method[safeCallEach] |
---|
| 212 | * |
---|
| 213 | */ |
---|
| 214 | safeCallEach: function(fnName, args) { |
---|
| 215 | var cs = this.items; |
---|
| 216 | for(var i = 0, len = cs.length; i < len; i++){ |
---|
| 217 | if(cs[i][fnName]) { |
---|
| 218 | cs[i].rendered ? |
---|
| 219 | cs[i][fnName].apply(cs[i], args) : |
---|
| 220 | cs[i].on({ |
---|
| 221 | "render": cs[i][fnName].createDelegate(cs[i], args), |
---|
| 222 | single: true |
---|
| 223 | }); |
---|
| 224 | } |
---|
| 225 | } |
---|
| 226 | } |
---|
| 227 | }); |
---|