1 | // ActionScript file |
---|
2 | import clarin.cmdi.componentregistry.browser.BrowserColumns; |
---|
3 | import clarin.cmdi.componentregistry.common.ItemDescription; |
---|
4 | import clarin.cmdi.componentregistry.common.components.RegistryViewStack; |
---|
5 | import clarin.cmdi.componentregistry.editor.model.CMDModelFactory; |
---|
6 | import clarin.cmdi.componentregistry.editor.model.CMDSpec; |
---|
7 | import clarin.cmdi.componentregistry.importer.UploadCompleteEvent; |
---|
8 | import clarin.cmdi.componentregistry.services.ComponentInfoService; |
---|
9 | import clarin.cmdi.componentregistry.services.ComponentListService; |
---|
10 | import clarin.cmdi.componentregistry.services.ComponentUsageCheckEvent; |
---|
11 | import clarin.cmdi.componentregistry.services.ComponentUsageService; |
---|
12 | import clarin.cmdi.componentregistry.services.Config; |
---|
13 | import clarin.cmdi.componentregistry.services.ProfileInfoService; |
---|
14 | import clarin.cmdi.componentregistry.services.UploadService; |
---|
15 | |
---|
16 | import flash.events.Event; |
---|
17 | |
---|
18 | import mx.controls.Alert; |
---|
19 | import mx.events.CloseEvent; |
---|
20 | import mx.managers.CursorManager; |
---|
21 | |
---|
22 | |
---|
23 | private var profileSrv:ProfileInfoService = new ProfileInfoService(); |
---|
24 | private var componentSrv:ComponentInfoService = new ComponentInfoService(); |
---|
25 | private var itemDescription:ItemDescription; |
---|
26 | |
---|
27 | [Bindable] |
---|
28 | private var componentsSrv:ComponentListService = ComponentListService.getInstance(Config.instance.userSpace); |
---|
29 | |
---|
30 | [Bindable] |
---|
31 | public var cmdComponent:XML; |
---|
32 | |
---|
33 | [Bindable] |
---|
34 | private var cmdSpec:CMDSpec; |
---|
35 | |
---|
36 | [Bindable] |
---|
37 | private var browserColumns:BrowserColumns = new BrowserColumns(); |
---|
38 | |
---|
39 | [Bindable] |
---|
40 | private var uploadService:UploadService = new UploadService(); |
---|
41 | |
---|
42 | [Bindable] |
---|
43 | private var viewStack:RegistryViewStack; |
---|
44 | |
---|
45 | public 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 | |
---|
55 | private function toggleUserSpace(event:Event):void { |
---|
56 | componentsSrv = ComponentListService.getInstance(Config.instance.userSpace); |
---|
57 | } |
---|
58 | |
---|
59 | private function determineSaveButtonEnabled():void { |
---|
60 | buttonBar.saveBtn.enabled = (itemDescription != null && itemDescription.isInUserSpace && null != itemDescription.id && null != xmlEditor.cmdSpec.headerId); |
---|
61 | } |
---|
62 | |
---|
63 | private 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 | |
---|
71 | private 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 | |
---|
81 | public 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 | |
---|
94 | public function startNewProfile():void { |
---|
95 | xmlEditor.clearEditorProfile(); |
---|
96 | } |
---|
97 | |
---|
98 | public function startNewComponent():void { |
---|
99 | xmlEditor.clearEditorComponent(); |
---|
100 | } |
---|
101 | |
---|
102 | private 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 | |
---|
106 | private function handlePublishAlert(event:CloseEvent):void { |
---|
107 | if (event.detail == Alert.OK) { |
---|
108 | saveSpec(false, UploadService.PUBLISH); |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | private 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 | |
---|
137 | private 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 | */ |
---|
149 | private 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 | |
---|
172 | private function doUpload(uploadAction:int, item:ItemDescription):void{ |
---|
173 | uploadService.upload(uploadAction, item, xmlEditor.cmdSpec.toXml()); |
---|
174 | } |
---|
175 | |
---|
176 | private function handleSaveComplete(event:UploadCompleteEvent):void { |
---|
177 | setDescription(event.itemDescription); |
---|
178 | parentApplication.viewStack.switchToBrowse(event.itemDescription); |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | private function handleEditorChange(event:Event):void { |
---|
183 | errorMessageField.text = ""; |
---|
184 | uploadProgress.visible = false; |
---|
185 | uploadProgress.includeInLayout = false; |
---|
186 | determineSaveButtonEnabled(); |
---|
187 | } |
---|
188 | |
---|
189 | private function initPaletteOverview():void { |
---|
190 | componentsPaletteOverview.dataGrid.dragEnabled = true; |
---|
191 | componentsPaletteOverview.dataGrid.allowMultipleSelection = true; |
---|
192 | componentsPaletteOverview.dataGrid.resizableColumns = true; |
---|
193 | } |
---|
194 | |
---|
195 | public function getType():String { |
---|
196 | return Config.VIEW_EDIT; |
---|
197 | } |
---|