1 | package clarin.cmdi.componentregistry.browser |
---|
2 | { |
---|
3 | import clarin.cmdi.componentregistry.common.Comment; |
---|
4 | import clarin.cmdi.componentregistry.common.Credentials; |
---|
5 | import clarin.cmdi.componentregistry.common.ItemDescription; |
---|
6 | import clarin.cmdi.componentregistry.common.StyleConstants; |
---|
7 | import clarin.cmdi.componentregistry.common.components.RssCommentsContextMenu; |
---|
8 | import clarin.cmdi.componentregistry.common.components.RssLinkButton; |
---|
9 | import clarin.cmdi.componentregistry.services.CommentListService; |
---|
10 | import clarin.cmdi.componentregistry.services.CommentPostService; |
---|
11 | import clarin.cmdi.componentregistry.services.Config; |
---|
12 | import clarin.cmdi.componentregistry.services.DeleteService; |
---|
13 | |
---|
14 | import flash.events.Event; |
---|
15 | import flash.events.MouseEvent; |
---|
16 | import flash.net.URLRequest; |
---|
17 | import flash.net.navigateToURL; |
---|
18 | |
---|
19 | import mx.containers.Canvas; |
---|
20 | import mx.containers.HBox; |
---|
21 | import mx.containers.VBox; |
---|
22 | import mx.controls.HRule; |
---|
23 | import mx.controls.Image; |
---|
24 | import mx.controls.Label; |
---|
25 | import mx.controls.Spacer; |
---|
26 | import mx.controls.scrollClasses.ScrollBar; |
---|
27 | |
---|
28 | |
---|
29 | |
---|
30 | [Event(name="commentsLoaded",type="flash.events.Event")] |
---|
31 | public class CommentsPanel extends HBox |
---|
32 | { |
---|
33 | public static const COMMENTS_LOADED:String = "commentsLoaded"; |
---|
34 | |
---|
35 | [Bindable] |
---|
36 | private var _itemDescription:ItemDescription; |
---|
37 | private var service:CommentListService; |
---|
38 | |
---|
39 | private var commentsBox:VBox; |
---|
40 | |
---|
41 | private const hPadding:int = 5; |
---|
42 | private const vPadding:int = 5; |
---|
43 | |
---|
44 | public function get commentListService():CommentListService { |
---|
45 | return service; |
---|
46 | } |
---|
47 | |
---|
48 | public function set itemDescription(itemDescription:ItemDescription):void { |
---|
49 | _itemDescription = itemDescription; |
---|
50 | } |
---|
51 | |
---|
52 | |
---|
53 | public function CommentsPanel() |
---|
54 | { |
---|
55 | this.setStyle("paddingLeft", 5); |
---|
56 | this.setStyle("paddingTop", 5); |
---|
57 | this.setStyle("paddingRight", 5); |
---|
58 | |
---|
59 | // this is for responding to the deletion of comments. At this point there is no way to distinghuish between item and component deletion |
---|
60 | // and that probably is fine since they mostly require the same response. It does mean that this component will also reload when a |
---|
61 | // component gets deleted, which is a bit superfluous. |
---|
62 | DeleteService.instance.addEventListener(DeleteService.ITEM_DELETED, commentDeletedHandler); |
---|
63 | } |
---|
64 | |
---|
65 | |
---|
66 | |
---|
67 | private function makeRssLinkButton():RssLinkButton{ |
---|
68 | |
---|
69 | var rssButtonTmp:RssLinkButton = new RssLinkButton(); |
---|
70 | rssButtonTmp.contextMenu = (new RssCommentsContextMenu(_itemDescription)).cm; |
---|
71 | rssButtonTmp.addEventListener(MouseEvent.CLICK, goToFeed); |
---|
72 | return rssButtonTmp; |
---|
73 | } |
---|
74 | |
---|
75 | private function goToFeed(event:MouseEvent):void{ |
---|
76 | navigateToURL(new URLRequest(Config.getRssUriComments(_itemDescription)), "_blank"); |
---|
77 | } |
---|
78 | |
---|
79 | public function load():void{ |
---|
80 | removeAllChildren(); |
---|
81 | |
---|
82 | |
---|
83 | if(_itemDescription != null) { |
---|
84 | |
---|
85 | // A box for the comments (will be loaded in callback but should be shown first) |
---|
86 | commentsBox = new VBox(); |
---|
87 | addChild(commentsBox); |
---|
88 | |
---|
89 | // Do actual loading |
---|
90 | service = new CommentListService(_itemDescription, _itemDescription.isInUserSpace); |
---|
91 | service.addEventListener(CommentListService.COMMENTS_LOADED, commentsLoaded); |
---|
92 | service.load(); |
---|
93 | |
---|
94 | |
---|
95 | |
---|
96 | // Rss feed "button" |
---|
97 | if (! _itemDescription.isInUserSpace){ |
---|
98 | var spacer:Spacer = new Spacer(); |
---|
99 | spacer.percentWidth=100; |
---|
100 | addChild(spacer); |
---|
101 | var rssButton:RssLinkButton = makeRssLinkButton(); |
---|
102 | addChild(rssButton); |
---|
103 | } |
---|
104 | |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | private function addPostPanel():void{ |
---|
109 | if(Credentials.instance.isLoggedIn()){ |
---|
110 | var postPanel:commentPostPanel = new commentPostPanel(); |
---|
111 | postPanel.itemDescription = _itemDescription; |
---|
112 | postPanel.commentPostService.addEventListener(CommentPostService.POST_COMPLETE, postCompleteHandler); |
---|
113 | commentsBox.addChild(postPanel); |
---|
114 | } else{ |
---|
115 | var loginToPostLabel:Label = new Label(); |
---|
116 | loginToPostLabel.setStyle("fontWeight","bold"); |
---|
117 | loginToPostLabel.text = "Login to leave a comment!"; |
---|
118 | commentsBox.addChild(loginToPostLabel); |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | private function commentsLoaded(event:Event):void{ |
---|
123 | if(service) { |
---|
124 | var commentsCount:int = service.comments.length; |
---|
125 | _itemDescription.commentsCount = commentsCount; |
---|
126 | |
---|
127 | if(commentsCount > 0) { |
---|
128 | for each(var comment:Comment in service.comments) { |
---|
129 | var panel:CommentPanel = new CommentPanel(comment); |
---|
130 | commentsBox.addChild(panel); |
---|
131 | } |
---|
132 | } else { |
---|
133 | var noCommentsPostedLabel:Label = new Label(); |
---|
134 | noCommentsPostedLabel.text = "No comments have been posted thus far."; |
---|
135 | commentsBox.addChild(noCommentsPostedLabel); |
---|
136 | |
---|
137 | var rule:HRule = new HRule(); |
---|
138 | rule.percentWidth = 100; |
---|
139 | commentsBox.addChild(rule); |
---|
140 | } |
---|
141 | } |
---|
142 | dispatchEvent(new Event(COMMENTS_LOADED)); |
---|
143 | |
---|
144 | // A panel for posting a comment (or a message 'login to post'); |
---|
145 | addPostPanel(); |
---|
146 | |
---|
147 | |
---|
148 | } |
---|
149 | |
---|
150 | private function postCompleteHandler(event:Event):void{ |
---|
151 | load(); |
---|
152 | } |
---|
153 | |
---|
154 | private function commentDeletedHandler(event:Event):void { |
---|
155 | load(); |
---|
156 | } |
---|
157 | } |
---|
158 | } |
---|