Make advanced tag panel listen to tag changes; fix issues with panel not responding...
authorRichard Fairhurst <richard@systemeD.net>
Sat, 2 Apr 2011 09:53:05 +0000 (10:53 +0100)
committerRichard Fairhurst <richard@systemeD.net>
Sat, 2 Apr 2011 09:53:05 +0000 (10:53 +0100)
net/systemeD/potlatch2/TagGrid.mxml
net/systemeD/potlatch2/TagViewer.mxml

index 555e6ef..6abbd69 100644 (file)
 
                /** Assign a new selected Entity, and update editing controls appropriately. */
                public function init(entity:Entity):void {
+                       if (selectedEntity==entity) return;
+                       if (selectedEntity) selectedEntity.removeEventListener(Connection.TAG_CHANGED, tagChanged);
+
+                       selectedEntity=entity;
+                       if (!selectedEntity) return;
+                       selectedEntity.addEventListener(Connection.TAG_CHANGED, tagChanged, false, 0, true);
+
                        if ( tagDataProvider == null ) {
                                tagDataProvider = new ArrayCollection();
                                dataProvider = tagDataProvider;
+                       } else {
+                               tagDataProvider.removeAll();
                        }
 
-                       if (selectedEntity==entity) return;
-                       selectedEntity=entity;
-                       updateTagDataProvider(true);
-               }
-
-               private function updateTagDataProvider(force:Boolean=false):void {
-                       if (editedItemPosition && !force) return;
-
-                       tagDataProvider.removeAll();
-                       if (selectedEntity==null) { return; }
                        var tags:Array = selectedEntity.getTagArray();
                        tags.sortOn("key");
                        for each(var tag:Tag in tags) { tagDataProvider.addItem(tag); }
                }
 
+               /** Update dataProvider in response to a tag being edited or removed */
+               private function tagChanged(event:TagEvent):void {
+                       var set:Boolean=false;
+                       for (var i:uint=0; i<tagDataProvider.length; i++) {
+                               if (tagDataProvider[i].key==event.oldKey) {
+                                       if (event.newValue && event.key) {
+                                               if (!set) {
+                                                       // this entry in the dataProvider has changed
+                                                       tagDataProvider[i].key=event.key;
+                                                       tagDataProvider[i].value=event.newValue;
+                                                       set=true;
+                                               } else {
+                                                       // trap duplicate tags
+                                                       tagDataProvider.removeItemAt(i);
+                                               }
+                                       } else {
+                                               // this entry in the dataProvider has been deleted
+                                               tagDataProvider.removeItemAt(i);
+                                       }
+                               }
+                       }
+                       // tell Flex to redraw the grid because we've changed a tag
+                       invalidateList();
+               }
+
                /** Create editing controls for a new key/value pair, with default values. */
                public function addNewTag():void {
                        var newKey:String = "(new tag)";
                public function removeTag():void {
                        var k:String = selectedItem.key;
                        selectedEntity.setTag(k, null, MainUndoStack.getGlobalStack().addAction);
-                       updateTagDataProvider();
                }
 
                /** Return the type ("node", "way", "relation") of the selected entity. */
index bc79c8d..3c4c9c0 100644 (file)
        }
 
       private function tagChanged(event:TagEvent):void {
-             if(selectedEntity != null && selectedEntity is EntityCollection) {
-                       setupMultiAdvanced(selectedEntity);
-                   return;
-                 }
-
           refreshFeatureIcon();
-          // ** FIXME - changing tags on Advanced tab should update simple view
-          // (but putting initialiseEditors here breaks other stuff, because it causes
-          // the Basic pane to be selected)
-
-          // if the advancedTagGrid has already been set up, it needs to be refreshed.
-          // FIXME make this better, maybe advancedTagGrid should be event listening.
-          if(advancedContainer.initialized) {
-            checkAdvanced();
-          }
       }
 
       public function loadFeatures():void {