/root/projects/slingshot/source/as/webpreviewer/src/org/alfresco/previewer/Document.as

https://github.com/deas/alfresco · ActionScript · 318 lines · 149 code · 40 blank · 129 comment · 11 complexity · 0b5e99da8068e262a99eea80a69ff270 MD5 · raw file

  1. /**
  2. * Copyright (C) 2005-2013 Alfresco Software Limited.
  3. *
  4. * This file is part of Alfresco
  5. *
  6. * Alfresco is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Alfresco is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package org.alfresco.previewer
  20. {
  21. import flash.display.DisplayObject;
  22. import flash.display.Sprite;
  23. import flash.events.MouseEvent;
  24. /**
  25. * A sprite object that displays its children as pages in a document.
  26. *
  27. * @author Erik Winlof
  28. */
  29. public class Document extends Sprite
  30. {
  31. /**
  32. * The outer padding of the whpole document (top, left, right, bottom)
  33. */
  34. private var _padding:Number = 0;
  35. /**
  36. * The vertical gap/padding betwwen the children/pages.
  37. */
  38. private var _gap:Number = 0;
  39. /**
  40. * The width of the widest page/child.
  41. */
  42. private var _maximumPageWidth:Number = 0;
  43. /**
  44. * The height if the highest page/child.
  45. */
  46. private var _maximumPageHeight:Number = 0;
  47. /**
  48. * "start"-y-position for all the pages/children.
  49. */
  50. private var _pageStarts:Array = new Array();
  51. /**
  52. * "end"-y-position for all the pages/children.
  53. */
  54. private var _pageEnds:Array = new Array();
  55. /**
  56. * Constructor
  57. */
  58. public function Document()
  59. {
  60. super();
  61. }
  62. /**
  63. * Positions the children/pages based on the order they were added and the gap and padding.
  64. * Also updates the _maximumPageWidth, _maximumPageHeight, _pageStarts and _pageEnds properties.
  65. */
  66. private function redrawChildren():void
  67. {
  68. graphics.clear();
  69. _pageEnds = new Array();
  70. _pageStarts = new Array();
  71. var obj:DisplayObject;
  72. var top:Number = 0, w:Number = 0;
  73. top += _padding; // padding top
  74. w += _padding; // padding left
  75. for (var i:Number = 0; i < numChildren; i++)
  76. {
  77. // Position each child/page.
  78. obj = getChildAt(i);
  79. obj.x = _padding + ((_maximumPageWidth / 2) - (obj.width / 2));
  80. obj.y = top;
  81. _pageStarts.push(top);
  82. top += obj.height;
  83. _pageEnds.push(top);
  84. // Add gap as long as there are more pages after the current.
  85. if (i < numChildren - 1)
  86. {
  87. top += _gap;
  88. }
  89. }
  90. top += _padding; // padding bottom
  91. w += _maximumPageWidth; // widest page
  92. w += _padding; // padding right
  93. graphics.beginFill(0xFFCC00, 0); // alpha set to 0 so the yellow "padding" isn't "visible"
  94. graphics.drawRect(0, 0, w, top);
  95. graphics.endFill();
  96. }
  97. /**
  98. * Adds a Page as a child to the display list and treats it like a page in a document.
  99. *
  100. * @param child A page in the document.
  101. */
  102. override public function addChild(child:DisplayObject):DisplayObject
  103. {
  104. if (child is Page)
  105. {
  106. // Call super class addChild.
  107. super.addChild(child);
  108. // Set variables for access later
  109. _maximumPageWidth = child.width;
  110. _maximumPageHeight = child.height;
  111. // Return the child/page.
  112. return child;
  113. }
  114. else
  115. {
  116. throw Error("A child to a Document must be of type Child.");
  117. }
  118. }
  119. public function set pages(pages:Array):void
  120. {
  121. var p:Page;
  122. var widest:Number = 0;
  123. var highest:Number = 0;
  124. for (var i:int = 0; i < pages.length; i++)
  125. {
  126. p = pages[i];
  127. if (p is Page)
  128. {
  129. // Call super class addChild.
  130. super.addChild(p);
  131. // Save the width/height of the widest/highest page/child.
  132. widest = Math.max(p.width, widest);
  133. highest = Math.max(p.height, highest);
  134. // Add event listener for page clicks
  135. p.addEventListener(MouseEvent.CLICK, onPageClick);
  136. }
  137. else
  138. {
  139. throw Error("A child to a Document must be of type Child.");
  140. }
  141. }
  142. // Set variables for access later
  143. _maximumPageWidth = widest;
  144. _maximumPageHeight = highest;
  145. // Layout all the pages in the document.
  146. redrawChildren();
  147. }
  148. /**
  149. * Returns the padding used used for top, left, right and bottom.
  150. *
  151. * @return the padding used used for top, left, right and bottom
  152. */
  153. public function get padding():Number
  154. {
  155. return _padding;
  156. }
  157. /**
  158. * Sets the padding to be used for top, left, right and bottom.
  159. *
  160. * @param the padding to be used for top, left, right and bottom.
  161. */
  162. public function set padding(padding:Number):void
  163. {
  164. if (_padding != padding)
  165. {
  166. _padding = padding;
  167. // Make sure we layout the pages according to the new padding.
  168. redrawChildren();
  169. }
  170. }
  171. /**
  172. * Returns the gap between the children/pages in the document.
  173. *
  174. * @return the gap between the children/pages in the document.
  175. */
  176. public function get gap():Number
  177. {
  178. return _gap;
  179. }
  180. /**
  181. * Sets the gap to be used between the children/pages in the document.
  182. *
  183. * @param the gap to be used between the children/pages in the document.
  184. */
  185. public function set gap(gap:Number):void
  186. {
  187. if (_gap != gap)
  188. {
  189. _gap = gap;
  190. // Make sure we layout the pages according to the new gap.
  191. redrawChildren();
  192. }
  193. }
  194. /**
  195. * Returns the width of the document.
  196. *
  197. * @return the width of the document.
  198. */
  199. public function getDocWidth():Number
  200. {
  201. return width;
  202. }
  203. /**
  204. * Returns the height of the document.
  205. *
  206. * @return the height of the document.
  207. */
  208. public function getDocHeight():Number
  209. {
  210. return height; 
  211. }
  212. /**
  213. * Returns the width of the widest page/child.
  214. *
  215. * @return the width of the widest page/child.
  216. */
  217. public function getMaximumPageWidth():Number
  218. {
  219. return _maximumPageWidth;
  220. }
  221. /**
  222. * Returns the height of the widest page/child.
  223. *
  224. * @return the height of the widest page/child.
  225. */
  226. public function getMaximumPageHeight():Number
  227. {
  228. return _maximumPageHeight;
  229. }
  230. /**
  231. * Get the "start"-y-position for the page defined by pageIndex.
  232. *
  233. * @param pageIndex The index of the page (first page can be found on index 0)
  234. *
  235. * @return the "start"-y-position for the page
  236. */
  237. public function getPageStart(pageIndex:int):Number
  238. {
  239. return _pageStarts[pageIndex];
  240. }
  241. /**
  242. * Get the "end"-y-position for the page defined by pageIndex.
  243. *
  244. * @param pageIndex The index of the page (first page can be found on index 0)
  245. *
  246. * @return the "end"-y-position for the page
  247. */
  248. public function getPageEnd(pageIndex:int):Number
  249. {
  250. return _pageEnds[pageIndex];
  251. }
  252. /**
  253. * Returns the number of pages/children in the document.
  254. *
  255. * @return the number of pages/children in the document.
  256. */
  257. public function getNoOfPages():Number
  258. {
  259. return numChildren;
  260. }
  261. /**
  262. * Called when one of the pages is clicked.
  263. *
  264. * @param event Describes the click event on the page.
  265. */
  266. private function onPageClick(event:MouseEvent):void
  267. {
  268. var pageIndex:int = getChildIndex(event.currentTarget as DisplayObject);
  269. if (pageIndex != -1)
  270. {
  271. var de:DocumentEvent = new DocumentEvent(DocumentEvent.DOCUMENT_PAGE_CLICK);
  272. de.page = event.currentTarget as Page;
  273. de.pageIndex = pageIndex;
  274. dispatchEvent(de);
  275. }
  276. }
  277. }
  278. }