| 1 | = Adding Events In Fusion Widgets = |
| 2 | |
| 3 | I though I would give a little walk through on how to add events in Fusion Widgets for those of you who want to "extend" events in some of the widgets. |
| 4 | |
| 5 | For this example lets use the legend widget. |
| 6 | |
| 7 | First add the new event obj, before the "OpenLayers.Class(Fusion.Widget" |
| 8 | |
| 9 | {{{ |
| 10 | // CODE BLOCK [legend.js] --------------------------------- |
| 11 | Fusion.Event.LEGEND_LAYER_VIS_ON = Fusion.Event.lastEventId++; |
| 12 | Fusion.Event.LEGEND_LAYER_VIS_OFF = Fusion.Event.lastEventId++; |
| 13 | |
| 14 | Fusion.Widget.Legend = OpenLayers.Class(Fusion.Widget, { |
| 15 | |
| 16 | //END CODE BLOCK --------------- |
| 17 | }}} |
| 18 | |
| 19 | Next bind this event to the init section of the class around line 70: |
| 20 | |
| 21 | {{{ |
| 22 | // CODE BLOCK [legend.js] --------------------------------- |
| 23 | //console.log('Legend.initialize'); |
| 24 | Fusion.Widget.prototype.initialize.apply(this, [widgetTag, true]); |
| 25 | |
| 26 | var json = widgetTag.extension; |
| 27 | |
| 28 | this.registerEventID(Fusion.Event.LEGEND_LAYER_VIS_ON); |
| 29 | this.registerEventID(Fusion.Event.LEGEND_LAYER_VIS_OFF); |
| 30 | |
| 31 | //END CODE BLOCK --------------- |
| 32 | }}} |
| 33 | |
| 34 | Now trigger the event in the legend class, for this we want to trigger it when the layer checkbox is clicked: around line 499: |
| 35 | |
| 36 | {{{ |
| 37 | // CODE BLOCK [legend.js] --------------------------------- |
| 38 | stateChanged: function(obj) { |
| 39 | if (obj.legend && obj.legend.checkBox) { |
| 40 | if (obj.legend.checkBox.checked) { |
| 41 | this.triggerEvent(Fusion.Event.LEGEND_LAYER_VIS_ON, obj); |
| 42 | obj.show(); |
| 43 | } else { |
| 44 | this.triggerEvent(Fusion.Event.LEGEND_LAYER_VIS_OFF, obj); |
| 45 | obj.hide(); |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | //END CODE BLOCK --------------- |
| 50 | }}} |
| 51 | |
| 52 | Now in your supporting js for the Fusion application you should have a function called when fusion is initialized. inside this function you will need to registerForEvent on the legend widget to handle this trigger. : |
| 53 | |
| 54 | {{{ |
| 55 | // CODE BLOCK --------------------------------- |
| 56 | var legend = Fusion.getWidgetsByType('Legend'); |
| 57 | if (legend.length > 0) { // 0 is the first legend widget. |
| 58 | legend[0].registerForEvent(Fusion.Event.LEGEND_LAYER_VIS_ON, layerON); |
| 59 | legend[0].registerForEvent(Fusion.Event.LEGEND_LAYER_VIS_OFF, layerOFF); |
| 60 | } |
| 61 | // END CODE BLOCK --------------------------------- |
| 62 | }}} |
| 63 | |
| 64 | and finally add your layerON and layerOff functions: |
| 65 | |
| 66 | {{{ |
| 67 | // CODE BLOCK --------------------------------- |
| 68 | function layerON(obj,resp){ |
| 69 | console.dir(resp); |
| 70 | } |
| 71 | |
| 72 | function layerOFF(obj,resp){ |
| 73 | console.dir(resp); |
| 74 | } |
| 75 | // END CODE BLOCK --------------------------------- |
| 76 | |
| 77 | }}} |
| 78 | That's it you should see the object passed back from the widget to the application. |