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