source: ComponentRegistry/branches/ComponentRegistry-1.12.0/ComponentBrowserGui/src/main/flex/clarin/cmdi/componentregistry/editor/EditorAS.as @ 2080

Last change on this file since 2080 was 2080, checked in by twagoo, 12 years ago

Added cancel button to editor

File size: 7.1 KB
Line 
1// ActionScript file
2import clarin.cmdi.componentregistry.browser.BrowserColumns;
3import clarin.cmdi.componentregistry.common.ItemDescription;
4import clarin.cmdi.componentregistry.common.components.RegistryViewStack;
5import clarin.cmdi.componentregistry.editor.model.CMDModelFactory;
6import clarin.cmdi.componentregistry.editor.model.CMDSpec;
7import clarin.cmdi.componentregistry.importer.UploadCompleteEvent;
8import clarin.cmdi.componentregistry.services.ComponentInfoService;
9import clarin.cmdi.componentregistry.services.ComponentListService;
10import clarin.cmdi.componentregistry.services.ComponentUsageCheckEvent;
11import clarin.cmdi.componentregistry.services.ComponentUsageService;
12import clarin.cmdi.componentregistry.services.Config;
13import clarin.cmdi.componentregistry.services.ProfileInfoService;
14import clarin.cmdi.componentregistry.services.UploadService;
15
16import flash.events.Event;
17
18import mx.controls.Alert;
19import mx.events.CloseEvent;
20import mx.managers.CursorManager;
21
22
23private var profileSrv:ProfileInfoService = new ProfileInfoService();
24private var componentSrv:ComponentInfoService = new ComponentInfoService();
25private var itemDescription:ItemDescription;
26
27[Bindable]
28private var componentsSrv:ComponentListService = ComponentListService.getInstance(Config.instance.userSpace);
29
30[Bindable]
31public var cmdComponent:XML;
32
33[Bindable]
34private var cmdSpec:CMDSpec;
35
36[Bindable]
37private var browserColumns:BrowserColumns = new BrowserColumns();
38
39[Bindable]
40private var uploadService:UploadService = new UploadService();
41
42[Bindable]
43private var viewStack:RegistryViewStack;
44
45public function init():void {
46        cmdSpec  = CMDSpec.createEmptyProfile();
47        profileSrv.addEventListener(ProfileInfoService.PROFILE_LOADED, profileLoaded);
48        componentSrv.addEventListener(ComponentInfoService.COMPONENT_LOADED, componentLoaded);
49        uploadService.addEventListener(UploadCompleteEvent.UPLOAD_COMPLETE, handleSaveComplete);
50        uploadService.init(uploadProgress);
51        Config.instance.addEventListener(Config.USER_SPACE_TOGGLE_EVENT, toggleUserSpace);
52        viewStack = this.parent as RegistryViewStack;
53}
54
55private function toggleUserSpace(event:Event):void {
56        componentsSrv = ComponentListService.getInstance(Config.instance.userSpace);
57}
58
59private function determineSaveButtonEnabled():void {
60        buttonBar.saveBtn.enabled = (itemDescription != null && itemDescription.isInUserSpace && null != itemDescription.id && null != xmlEditor.cmdSpec.headerId); 
61}
62
63private function profileLoaded(event:Event):void {
64        var cmdComponent:XML = profileSrv.profile.profileSource;
65        this.cmdSpec = CMDModelFactory.createModel(cmdComponent, profileSrv.profile.description);
66        this.cmdSpec.changeTracking = true;
67        determineSaveButtonEnabled();
68        CursorManager.removeBusyCursor();
69}
70
71private function componentLoaded(event:Event):void {
72        var cmdComponent:XML = componentSrv.component.componentMD.xml;
73        this.cmdSpec = CMDModelFactory.createModel(cmdComponent, componentSrv.component.description);
74        // Track changes for components being edited
75        this.cmdSpec.changeTracking = true;
76        determineSaveButtonEnabled();
77        CursorManager.removeBusyCursor();
78}
79
80
81public function setDescription(itemDescription:ItemDescription):void {
82        if (itemDescription) {
83                this.itemDescription = itemDescription;
84                CursorManager.setBusyCursor();
85                if (itemDescription.isProfile) {
86                        profileSrv.load(itemDescription);
87                } else {
88                        componentSrv.load(itemDescription);
89                }
90                buttonBar.saveBtn.enabled = false;
91        }
92}
93
94public function startNewProfile():void {
95        xmlEditor.clearEditorProfile();
96}
97
98public function startNewComponent():void {
99        xmlEditor.clearEditorComponent();
100}
101
102private function publishSpec():void {
103        Alert.show("If your profile/component is ready to be used by other people press ok, otherwise press cancel and save it in your workspace or continue editing.", "Publish", Alert.OK | Alert.CANCEL, null, handlePublishAlert);
104}
105
106private function handlePublishAlert(event:CloseEvent):void {
107        if (event.detail == Alert.OK) {
108                saveSpec(false, UploadService.PUBLISH);
109        }
110}
111
112private function saveSpec(inUserSpace:Boolean, uploadAction:int):void {
113        //      Alert.show(xmlEditor.cmdSpec.toXml());
114        if (xmlEditor.validate()) {
115                var item:ItemDescription = new ItemDescription();
116                item.description = xmlEditor.cmdSpec.headerDescription;
117                item.name = xmlEditor.cmdSpec.headerName;
118                item.isProfile = xmlEditor.cmdSpec.isProfile;
119                item.groupName = xmlEditor.cmdSpec.groupName;
120                item.domainName = xmlEditor.cmdSpec.domainName;
121                item.isInUserSpace = inUserSpace;
122                if (itemDescription && itemDescription.isInUserSpace) {
123                        item.id = xmlEditor.cmdSpec.headerId;
124                }
125               
126                // Private components that are in updated require usage check call. If in use, the user can choose whether or not to save the changes .
127                if(inUserSpace && uploadAction == UploadService.UPDATE && !item.isProfile){
128                        checkUsage(item, inUserSpace);
129                }else{
130                        doUpload(uploadAction,item);
131                }
132        } else {
133                errorMessageField.text = "Validation errors: red colored fields are invalid.";
134        }
135}
136
137private function cancel():void {
138        Alert.show("Are you sure you want to cancel editing?", "Cancel editing", Alert.YES|Alert.NO, null, 
139                function (eventObj:CloseEvent):void{
140                        if(eventObj.detail == Alert.YES){
141                                viewStack.switchToBrowse(itemDescription);                             
142                        }
143                });
144}
145
146/**
147 * Calls usage check for the specified component. If in use, asks user whether to proceed; if positive, initiates update.
148 */
149private function checkUsage(item:ItemDescription, inUserSpace:Boolean = true, uploadAction:int = UploadService.UPDATE):void{
150        var componentUsageService:ComponentUsageService = new ComponentUsageService(item,inUserSpace);
151        componentUsageService.addEventListener(ComponentUsageCheckEvent.COMPONENT_IN_USE, 
152                function (event:ComponentUsageCheckEvent):void{
153                        if(event.isComponentInUse){
154                                var messageBody:String = "The component you are about to save is used by the following component(s) and/or profile(s):\n\n";
155                                for each(var name:String in event.itemUsingComponent){
156                                        messageBody += " - " + name + "\n";
157                                }
158                                messageBody += "\nChanges in this component will affect the above. Do you want to proceed?";
159                                Alert.show(messageBody,"Component is used", Alert.YES|Alert.NO,null,
160                                        function (eventObj:CloseEvent):void{
161                                                if(eventObj.detail == Alert.YES){
162                                                        doUpload(uploadAction, item);
163                                                }
164                                        });
165                        } else {
166                                doUpload(uploadAction,item);
167                        }
168                });
169        componentUsageService.checkUsage();
170}
171
172private function doUpload(uploadAction:int, item:ItemDescription):void{
173        uploadService.upload(uploadAction, item, xmlEditor.cmdSpec.toXml());
174}
175
176private function handleSaveComplete(event:UploadCompleteEvent):void {
177        setDescription(event.itemDescription);
178        parentApplication.viewStack.switchToBrowse(event.itemDescription);
179}
180
181
182private function handleEditorChange(event:Event):void {
183        errorMessageField.text = "";
184        uploadProgress.visible = false;
185        uploadProgress.includeInLayout = false;
186        determineSaveButtonEnabled();
187}
188
189private function initPaletteOverview():void {
190        componentsPaletteOverview.dataGrid.dragEnabled = true;
191        componentsPaletteOverview.dataGrid.allowMultipleSelection = true;
192        componentsPaletteOverview.dataGrid.resizableColumns = true;
193}
194
195public function getType():String {
196        return Config.VIEW_EDIT;
197}
Note: See TracBrowser for help on using the repository browser.