Sort 'add to relation' dialogue by description
[potlatch2.git] / net / systemeD / potlatch2 / RelationSelectPanel.mxml
1 <?xml version="1.0" encoding="utf-8"?>
2 <mx:TitleWindow
3     xmlns:fx="http://ns.adobe.com/mxml/2009"
4     xmlns:mx="library://ns.adobe.com/flex/mx"
5     xmlns:potlatch2="net.systemeD.potlatch2.*"
6     title="Select Relation" width="350" height="400"
7     showCloseButton="true" close="PopUpManager.removePopUp(this);">
8
9     <fx:Script><![CDATA[
10         import net.systemeD.halcyon.*;
11         import net.systemeD.halcyon.connection.*;
12         import net.systemeD.potlatch2.*;
13         import mx.managers.PopUpManager;
14         import mx.core.Application;
15         import mx.core.FlexGlobals;
16
17         private var conn:Connection;
18         private var relationList:Array;
19         private var entity:Entity;
20                 private var defaulttags:Object;
21
22         /** Initialise panel used to add and remove relations from an entity.
23         *   @param e Entity that is being edited.
24         *   @param t Object containing tag match patterns as properties, for the kind of relation sought.
25         */
26         public function init(e:Entity, t:Object):void {
27           entity = e;
28           conn = e.connection;
29           defaulttags = {};
30           for (var k:String in t) {  
31             //tagmatches[k]=t[k];// deep copy match pattern tags
32             defaulttags[k]=t[k][0];  // route=[hiking,foot] -> route=hiking
33           }     
34           PopUpManager.addPopUp(this, Application(FlexGlobals.topLevelApplication), true);
35           PopUpManager.centerPopUp(this);
36     
37           var titles:Array = [];
38           relationList = conn.getMatchingRelationIDs(t);
39           if (relationList.length == 0) {
40             warning.text = "No relations available";
41           } else {
42             warning.text = "These relations are available:";
43           }
44           //relationSelector.dataProvider = relationList;
45           for each (var id:Number in relationList) {
46             var relation:Relation = conn.getRelation(id);
47             titles.push({label:relation.getDescription(), relId:id});
48           }
49           titles.sortOn('label');
50           titles.forEach(function(item:Object, index:int, arr:Array):void { arr[index].label=item.relId+": "+item.label; });
51           relationSelector.dataProvider = titles;
52         }
53         
54         public function updateEntityAndClose():void {
55                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Add to relation");
56                         for each (var relObject:Object in relationSelector.selectedItems) {
57                                 var relation:Relation = conn.getRelation(relObject.relId);
58                                 if (entity is EntityCollection) {
59                                         for each (var e:Entity in EntityCollection(entity).entities) {
60                                                 if (relation.findEntityMemberIndex(e)==-1) {
61                                                         relation.appendMember(new RelationMember(e, ''), undo.push);
62                                                 }
63                                         }
64                                 } else {
65                                         relation.appendMember(new RelationMember(entity, ''), undo.push);
66                                 }
67                         }
68                         MainUndoStack.getGlobalStack().addAction(undo);
69                         PopUpManager.removePopUp(this);
70         }
71         
72         public function closeAndNewRelation():void {
73                         var members:Array=[];
74                         if (entity is EntityCollection) {
75                                 for each (var e:Entity in EntityCollection(entity).entities) {
76                                         members.push(new RelationMember(e, ''));
77                                 }
78                         } else members.push(new RelationMember(entity, ''));
79                         var relation:Relation = conn.createRelation(defaulttags, members, MainUndoStack.getGlobalStack().addAction);
80
81                         PopUpManager.removePopUp(this);
82                         var panel:RelationEditorPanel = RelationEditorPanel(
83                             PopUpManager.createPopUp(Application(FlexGlobals.topLevelApplication), RelationEditorPanel, true));
84                         panel.setRelation(relation);
85                         PopUpManager.centerPopUp(panel);
86         }
87
88                 public function closeAndLoadRelation():void {
89                         PopUpManager.removePopUp(this);
90                         var panel:RelationLoaderPanel = RelationLoaderPanel(
91                             PopUpManager.createPopUp(Application(FlexGlobals.topLevelApplication), RelationLoaderPanel, true));
92                         panel.setEntity(entity);
93                         PopUpManager.centerPopUp(panel);
94                 }
95                 
96                 public function updateSelectButton():void {
97                         selectButton.enabled=relationSelector.selectedItems.length>0;
98                 }
99         
100     ]]></fx:Script>
101     <mx:Text id="warning" text="" />
102     <mx:List width="100%" height="100%" id="relationSelector" change="updateSelectButton();" verticalScrollPolicy="on" allowMultipleSelection="true" > 
103     </mx:List>
104     <mx:ControlBar>
105       <mx:Button label="New Relation..." click="closeAndNewRelation();" styleName="titleWindowButton" />
106       <mx:Button label="Load Relation..." click="closeAndLoadRelation();" styleName="titleWindowButton" />
107       <mx:Spacer width="100%"/>
108       <mx:Button label="Select" id="selectButton" click="updateEntityAndClose();" enabled="false" styleName="titleWindowButton" />
109     </mx:ControlBar>
110 </mx:TitleWindow>