PageRenderTime 56ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/client/src/list/ItemPanel.hx

http://na-agent.googlecode.com/
Haxe | 612 lines | 478 code | 93 blank | 41 comment | 69 complexity | 556bc3995c823875dcff4a1a573002ff MD5 | raw file
Possible License(s): GPL-2.0
  1. /**
  2. * ...
  3. * @author Innokentiy Shuvalov
  4. * @author Alex
  5. */
  6. package list;
  7. import flash.net.URLRequest;
  8. import loader.Loader;
  9. import list.DisplayMode;
  10. import client.INews;
  11. import client.Item;
  12. import flash.geom.Rectangle;
  13. import gui.hug.HUGSprite;
  14. import gui.hug.HUGColors;
  15. import flash.text.TextFormat;
  16. import gui.hug.HUGTextArea;
  17. import gui.hug.Utils;
  18. import gui.hug.HUGLabel;
  19. import flash.events.MouseEvent;
  20. import flash.events.Event;
  21. import flash.events.KeyboardEvent;
  22. import flash.display.Sprite;
  23. import flash.ui.Keyboard;
  24. import haxe.Timer;
  25. import client.Channel;
  26. import tab.FavoritesTab;
  27. class ItemPanel extends HUGSprite {
  28. static private inline var SELECTED_BACKGROUND_COLOR : UInt = Utils.SEL_BG_COL;
  29. static private inline var SELECTED_BORDER_COLOR : UInt = Utils.SEL_BG_COL - 0x222222;
  30. static private inline var SELECTED_TEXT_COLOR : UInt = HUGColors.WHITE;
  31. static private inline var SELECTED_LINK_COLOR : UInt = HUGColors.VK_BUTTON - 0x222222;
  32. static private inline var HIGHLIGHTED_BACKGROUND_COLOR : UInt = HUGColors.VK_BORDER_INNER - 0x222211;
  33. static private inline var HIGHLIGHTED_BORDER_COLOR : UInt = Utils.SEL_BG_COL + 0x222222;
  34. static private inline var HIGHLIGHTED_TEXT_COLOR : UInt = 0x002266;
  35. static private inline var HIGHLIGHTED_LINK_COLOR : UInt = 0x004499;
  36. static private inline var DEFAULT_BACKGROUND_COLOR : UInt = HUGColors.VK_BORDER_INNER;
  37. static private inline var DEFAULT_BORDER_COLOR : UInt = HUGColors.WHITE;
  38. static private inline var DEFAULT_TEXT_COLOR : UInt = HUGColors.VK_MENU_TEXT;
  39. static private inline var TITLE_SIZE : UInt = 13;
  40. static private inline var TEXT_SIZE : UInt = 11;
  41. static private inline var TEXT_ADD_TO_FAVOURITES : String = "????????? ? ?????????";
  42. static private inline var str: String = "???????? ? ?????????";
  43. static private var shortTitleFormat = new TextFormat("Tahoma", TITLE_SIZE, DEFAULT_TEXT_COLOR, true, false, true);
  44. static private var shortTextFormat = new TextFormat("Tahoma", TEXT_SIZE, DEFAULT_TEXT_COLOR, false, false, false);
  45. static private var selectedTitleFormat = new TextFormat("Tahoma", TITLE_SIZE, SELECTED_TEXT_COLOR, true, false, true);
  46. static private var selectedTextFormat = new TextFormat("Tahoma", TEXT_SIZE, SELECTED_TEXT_COLOR, false, false, false);
  47. static private var highlightedTitleFormat = new TextFormat("Tahoma", TITLE_SIZE, HIGHLIGHTED_TEXT_COLOR, true, false, true);
  48. static private var highlightedTextFormat = new TextFormat("Tahoma", TEXT_SIZE, HIGHLIGHTED_TEXT_COLOR, false, false, false);
  49. static private inline var IMAGE_HEIGHT : UInt = 50;
  50. static private inline var IMAGE_WIDTH : UInt = 50;
  51. private static inline var LEFT : Float = 6 + IMAGE_WIDTH;
  52. static private inline var SPACE_FROM_TOP : UInt = 3; // space before title
  53. static private inline var SPACE_AFTER_TITLE : UInt = 10;
  54. static private inline var SPACE_AFTER_DESCRIPTION : UInt = 8;
  55. static private inline var SPACE_AFTER_DATE : UInt = 8;
  56. private var w : UInt; //width
  57. private var addition : Float;
  58. private var shortNameHeight : Float;
  59. private var fullNameHeight : Float;
  60. private var fullDescriptionHeight : Float;
  61. private var shortDescriptionHeight : Float;
  62. // content
  63. private var content : INews;
  64. private var mode : DisplayMode;
  65. private var id : UInt;
  66. private var stateChanged : Bool;
  67. // graphical parts
  68. private var image :ImageLoader;
  69. private var fullName : HUGLabel;
  70. private var shortName : HUGLabel;
  71. private var fullDescription : HUGLabel;
  72. private var shortDescription : HUGLabel;
  73. private var date : HUGLabel;
  74. private var link : MyLinkButton;
  75. private var open : MyLinkButton;
  76. private var hideButton : MyLinkButton;
  77. private var toFriend : MyLinkButton;
  78. private var Add: MyLinkButton;
  79. public var Favorites: Array<INews>;
  80. private var loader : loader.Loader;
  81. private var dataId : Array<Int> ;
  82. public function new( panelWidth : UInt, item : INews, index : UInt, type:Int , ?disMode : DisplayMode ) {
  83. super();
  84. Favorites = new Array<INews>();
  85. Loader.createInstance();
  86. w = panelWidth;
  87. content = item;
  88. mode = disMode == null ? DisplayMode.short : disMode;
  89. id = index;
  90. addition = Std.is(content, Item) ? 0 : 150;
  91. init(type);
  92. generatePanel();
  93. addEventListener( MouseEvent.CLICK, onClick );
  94. addEventListener( MouseEvent.MOUSE_OVER, onOver );
  95. addEventListener( MouseEvent.MOUSE_OUT, onOut );
  96. addEventListener( KeyboardEvent.KEY_DOWN, onKeyDown );
  97. }
  98. private function init(type:Int) {
  99. stateChanged = true;
  100. initImage();
  101. initName();
  102. initDescription();
  103. initDate();
  104. initLink();
  105. initOpen(type);
  106. initHide(type);
  107. setLinkColor();
  108. initFavYesORNo(type);
  109. if (type == 1) initToFriend();
  110. }
  111. private function initFavYesORNo(type:Int) {
  112. loader = Loader.getInstance();
  113. if (type==0) loader.getIDFavorites(onLoadFav);
  114. initAddFavorites(str, type);
  115. }
  116. private function onLoadFav(data : Array < Int > ) :Void {
  117. for (channel in data)
  118. if (content.getChanId() == channel)
  119. initAddFavorites(TEXT_ADD_TO_FAVOURITES, 0);
  120. }
  121. private function initImage() {
  122. image = new ImageLoader(Std.string(content.getLinkImg()), IMAGE_WIDTH, IMAGE_HEIGHT); // Loader.wrapImageLink()
  123. image.visible = true;
  124. image.x = 0;
  125. image.y = 0;
  126. addChild(image);
  127. }
  128. private function initName() {
  129. fullName = new HUGLabel();
  130. fullName.setTextFormat( shortTitleFormat );
  131. fullName.setLocation(LEFT , 0);
  132. fillFullName( content.getName() );
  133. fullName.visible = false;
  134. addChild(fullName);
  135. fullNameHeight = fullName.getBounds(fullName).height;
  136. shortName = new HUGLabel();
  137. shortName.setTextFormat( shortTitleFormat );
  138. shortName.setLocation(LEFT , 0);
  139. fillShortName( content.getName() );
  140. addChild(shortName);
  141. shortNameHeight = shortName.getBounds(shortName).height;
  142. }
  143. private function initDescription() {
  144. fullDescription = new HUGLabel();
  145. fullDescription.setLocation(LEFT , fullNameHeight + SPACE_AFTER_TITLE);
  146. fullDescription.setTextFormat( shortTextFormat );
  147. fullDescription.visible = false;
  148. fullDescription.multiline = true;
  149. fullDescription.text = "";
  150. fillFullDescription(content.getDescr() );
  151. addChild(fullDescription);
  152. fullDescriptionHeight = fullDescription.getBounds(fullDescription).height * 0.88;
  153. shortDescription = new HUGLabel();
  154. shortDescription.setLocation(LEFT , shortNameHeight + SPACE_AFTER_TITLE);
  155. shortDescription.setTextFormat( shortTextFormat );
  156. shortDescription.multiline = true;
  157. fillShortDescription( content.getDescr() );
  158. addChild(shortDescription);
  159. shortDescriptionHeight = 18 ; //shortDescription.getBounds(shortDescription).height ;
  160. }
  161. private function initDate() {
  162. date = new HUGLabel();
  163. date.setLocation(LEFT, fullNameHeight +
  164. SPACE_AFTER_TITLE +
  165. fullDescriptionHeight +
  166. SPACE_AFTER_DESCRIPTION );
  167. date.visible = false;
  168. date.text = content.getDate();
  169. date.setTextFormat( shortTextFormat );
  170. addChild(date);
  171. }
  172. private function initLink() {
  173. link = new MyLinkButton();
  174. link.setNewText("??????? ? ????????");
  175. link.addEventListener( MouseEvent.CLICK, openLink );
  176. link.setLocation(LEFT, fullNameHeight +
  177. SPACE_AFTER_TITLE +
  178. fullDescriptionHeight +
  179. SPACE_AFTER_DESCRIPTION +
  180. date.getBounds(date).height +
  181. SPACE_AFTER_DATE );
  182. link.visible = false;
  183. addChild(link);
  184. }
  185. private function initToFriend() {
  186. toFriend = new MyLinkButton();
  187. toFriend.setNewText("????????? ?????");
  188. toFriend.addEventListener(MouseEvent.CLICK, onSend);
  189. toFriend.setLocation(LEFT + link.width + 10 + open.width + 10, fullNameHeight + SPACE_AFTER_TITLE + fullDescriptionHeight + SPACE_AFTER_DESCRIPTION +
  190. date.getBounds(date).height + SPACE_AFTER_DATE );
  191. toFriend.visible = false;
  192. addChild(toFriend);
  193. }
  194. private function initOpen(type:Int) {
  195. open = new MyLinkButton();
  196. open.setNewText("???????");
  197. open.addEventListener( MouseEvent.CLICK, openTab );
  198. open.setLocation(LEFT + link.width + 10, fullNameHeight +
  199. SPACE_AFTER_TITLE +
  200. fullDescriptionHeight +
  201. SPACE_AFTER_DESCRIPTION +
  202. date.getBounds(date).height +
  203. SPACE_AFTER_DATE );
  204. open.visible = !Std.is(content, Item);
  205. if(type == 0)
  206. addChild(open);
  207. }
  208. private function initHide(type:Int) {
  209. hideButton = new MyLinkButton();
  210. hideButton.setNewText("??????");
  211. hideButton.addEventListener( MouseEvent.CLICK, onHide );
  212. if(type==0 )
  213. hideButton.setLocation(LEFT + link.width + 10 + open.width + 10, fullNameHeight +
  214. SPACE_AFTER_TITLE +
  215. fullDescriptionHeight +
  216. SPACE_AFTER_DESCRIPTION +
  217. date.getBounds(date).height +
  218. SPACE_AFTER_DATE );
  219. else
  220. hideButton.setLocation(link.x+link.width+10,link.y );
  221. hideButton.visible = false;
  222. addChild(hideButton);
  223. }
  224. public function initAddFavorites(str:String, type:Int) {
  225. Add = new MyLinkButton();
  226. Add.setNewText(str);
  227. if (str!=TEXT_ADD_TO_FAVOURITES)
  228. {Add.addEventListener( MouseEvent.CLICK, AddChan); }
  229. Add.setLocation(hideButton.x+hideButton.width+10,hideButton.y );
  230. Add.visible = !Std.is(content, Item);
  231. if(type==0)
  232. addChild(Add);
  233. }
  234. // USER METHODS /////////////////////////////////////////////////////////////////////////
  235. public function getContent() : INews {
  236. return content;
  237. }
  238. public function reduceId() {
  239. if (id > 0) {
  240. id -- ;
  241. }
  242. }
  243. public function getHeight() : Float {
  244. return switch(mode) {
  245. case(DisplayMode.hidden) : 0 ;
  246. case(DisplayMode.short) :
  247. shortNameHeight +
  248. SPACE_AFTER_TITLE +
  249. shortDescriptionHeight +
  250. 5 ;
  251. case(DisplayMode.selected) :
  252. fullNameHeight +
  253. SPACE_AFTER_TITLE +
  254. fullDescriptionHeight +
  255. SPACE_AFTER_DESCRIPTION +
  256. date.getBounds(date).height +
  257. SPACE_AFTER_DATE +
  258. hideButton.getBounds(hideButton).height +
  259. 10 ;
  260. case(DisplayMode.highlighted) :
  261. shortNameHeight +
  262. SPACE_AFTER_TITLE +
  263. shortDescriptionHeight +
  264. 5 ;
  265. }
  266. }
  267. /**
  268. * @param hidden in case it is 'true' the item becomes hidden
  269. * and only a one more execution of this method with 'false' parameter
  270. * can make it visible again!
  271. */
  272. public function hide( hidden : Bool = true ) {
  273. if (hidden) setDisplayMode(DisplayMode.hidden);
  274. else setDisplayMode(DisplayMode.short);
  275. }
  276. // PRIVATE METHODS //////////////////////////////////////////////////////////////////
  277. private function generatePanel() {
  278. if (stateChanged) {
  279. drawBackground();
  280. //setLinkColor();
  281. setVisibleElements();
  282. setLabelFormats();
  283. stateChanged = false;
  284. }
  285. }
  286. private function setLabelFormats() {
  287. if (stateChanged) {
  288. switch(mode) {
  289. case DisplayMode.short :
  290. shortName.setTextFormat(shortTitleFormat);
  291. shortDescription.setTextFormat(shortTextFormat);
  292. date.setTextFormat(shortTextFormat);
  293. case DisplayMode.selected :
  294. fullName.setTextFormat(selectedTitleFormat);
  295. fullDescription.setTextFormat(selectedTextFormat);
  296. date.setTextFormat(selectedTextFormat);
  297. case DisplayMode.highlighted :
  298. shortName.setTextFormat(highlightedTitleFormat);
  299. fullDescription.setTextFormat(highlightedTextFormat);
  300. shortDescription.setTextFormat(highlightedTextFormat);
  301. date.setTextFormat(highlightedTextFormat);
  302. }
  303. }
  304. }
  305. private function setVisibleElements() {
  306. if (stateChanged) {
  307. var visibility = mode == DisplayMode.selected;
  308. date.visible = visibility;
  309. open.visible = visibility;
  310. link.visible = visibility;
  311. hideButton.visible = visibility;
  312. fullDescription.visible = visibility;
  313. shortDescription.visible = !visibility;
  314. fullName.visible = visibility;
  315. shortName.visible = !visibility;
  316. Add.visible = visibility;
  317. if (toFriend != null) toFriend.visible = visibility;
  318. }
  319. }
  320. private function drawBackground() {
  321. if (stateChanged) {
  322. var backgroundColor = DEFAULT_BACKGROUND_COLOR;
  323. var borderColor = DEFAULT_BORDER_COLOR;
  324. switch(mode) {
  325. case DisplayMode.selected :
  326. backgroundColor = SELECTED_BACKGROUND_COLOR;
  327. borderColor = SELECTED_BORDER_COLOR;
  328. case DisplayMode.highlighted :
  329. backgroundColor = HIGHLIGHTED_BACKGROUND_COLOR;
  330. borderColor = HIGHLIGHTED_BORDER_COLOR;
  331. }
  332. Utils.rect( this, // owner
  333. 0, // left
  334. 0, // top
  335. w,
  336. getHeight(),
  337. backgroundColor,
  338. borderColor );
  339. }
  340. }
  341. private function setLinkColor() {
  342. if (stateChanged) switch(mode) {
  343. case DisplayMode.selected :
  344. link.setOverStateColor(SELECTED_LINK_COLOR);
  345. open.setOverStateColor(SELECTED_LINK_COLOR);
  346. hideButton.setOverStateColor(SELECTED_LINK_COLOR);
  347. Add.setOverStateColor(SELECTED_LINK_COLOR);
  348. case DisplayMode.highlighted :
  349. link.setOverStateColor(HIGHLIGHTED_LINK_COLOR);
  350. open.setOverStateColor(HIGHLIGHTED_LINK_COLOR);
  351. hideButton.setOverStateColor(HIGHLIGHTED_LINK_COLOR);
  352. Add.setOverStateColor(HIGHLIGHTED_LINK_COLOR);
  353. }
  354. }
  355. private function setDisplayMode ( m : DisplayMode ) {
  356. if ( mode != m ) {
  357. stateChanged = true;
  358. mode = m;
  359. }
  360. generatePanel();
  361. }
  362. /**
  363. * we need to edit the description
  364. * because it might be too long
  365. * and so any parts will be missing.
  366. */
  367. private function fillFullDescription(textDescription : String) {
  368. var text : String = StringTools.htmlUnescape(deleteTags(StringTools.htmlUnescape(textDescription)));
  369. var rememberText = fullDescription.text;
  370. fullDescription.text = "";
  371. for (i in 0 ... text.length) {
  372. if (text.charAt(i) != "\n")
  373. fullDescription.appendText(text.charAt(i) );
  374. if (fullDescription.getBounds(fullDescription).width * 1.3 + fullDescription.x > w ||
  375. text.charAt(i) == "\n" ) {
  376. fullDescription.text = rememberText + fullDescription.text + "\n";
  377. if( i != text.length - 1) fillFullDescription(text.substr(i + 1));
  378. return;
  379. }
  380. }
  381. fullDescription.text = rememberText + fullDescription.text + "\n";
  382. }
  383. /**
  384. * we need to edit the description
  385. * because it might be too long
  386. * and so it won't be convenient
  387. * to display the whole one.
  388. */
  389. private function fillShortDescription(textDescription : String) {
  390. var text : String = StringTools.htmlUnescape(deleteTags(StringTools.htmlUnescape(textDescription)));
  391. shortDescription.text = "";
  392. for (i in 0 ... text.length) {
  393. if (text.charAt(i) != "\n")
  394. shortDescription.appendText(text.charAt(i) );
  395. if (shortDescription.getBounds(shortDescription).width * 1.14 + shortDescription.x > w ||
  396. text.charAt(i) == "\n") {
  397. if( i != text.length - 1) shortDescription.appendText("...");
  398. return;
  399. }
  400. }
  401. }
  402. /**
  403. * we need to edit the name
  404. * because it might be too long
  405. * and so any parts will be missing.
  406. */
  407. private function fillFullName(text : String) {
  408. var rememberText = fullName.text;
  409. fullName.text = "";
  410. for (i in 0 ... text.length) {
  411. if (text.charAt(i) != "\n")
  412. fullName.appendText(text.charAt(i) );
  413. if (fullName.getBounds(fullName).width * 1.5 + fullName.x > w ||
  414. text.charAt(i) == "\n" ) {
  415. fullName.text = rememberText + fullName.text + "\n";
  416. if( i != text.length - 1) fillFullName(text.substr(i + 1));
  417. return;
  418. }
  419. }
  420. fullName.text = rememberText + fullName.text + "\n";
  421. }
  422. /**
  423. * we need to edit the name
  424. * because it might be too long
  425. * and so it won't be convenient
  426. * to display the whole one.
  427. */
  428. private function fillShortName(text : String) {
  429. shortName.text = "";
  430. for (i in 0 ... text.length) {
  431. if (text.charAt(i) != "\n")
  432. shortName.appendText(text.charAt(i) );
  433. if (shortName.getBounds(shortName).width * 1.5 + shortName.x > w ||
  434. text.charAt(i) == "\n") {
  435. if( i != text.length - 1) shortName.appendText("...");
  436. return;
  437. }
  438. }
  439. }
  440. private function deleteTags(text : String):String {
  441. var re : EReg = ~/<\/?[A-Za-z]+\/?>/g ;
  442. return re.replace(text, "");
  443. }
  444. // EVENT HANDLERS ///////////////////////////////////////////////////
  445. private function onClick(_) {
  446. if (mode != DisplayMode.hidden) {
  447. if ( mode != DisplayMode.selected ) setDisplayMode(DisplayMode.selected);
  448. else setDisplayMode(DisplayMode.highlighted);
  449. cast(parent, NewList).scrollTo(id);
  450. cast(parent, NewList).draw();
  451. }
  452. }
  453. private function onOver(_) {
  454. if (mode != DisplayMode.hidden) {
  455. if ( mode != DisplayMode.selected ) setDisplayMode(DisplayMode.highlighted);
  456. cast(parent, NewList).scrollTo(id);
  457. cast(parent, NewList).draw();
  458. }
  459. }
  460. private function onOut(_) {
  461. if (mode != DisplayMode.hidden) {
  462. if ( mode != DisplayMode.selected ) setDisplayMode(DisplayMode.short);
  463. cast(parent, NewList).draw();
  464. }
  465. }
  466. private function openLink(e : MouseEvent) {
  467. flash.Lib.getURL(new URLRequest(content.getLink()));
  468. e.stopPropagation();
  469. }
  470. private function openTab(e : MouseEvent) {
  471. cast(parent, NewList).dispatchEvent(new OpenTabEvent(content));
  472. e.stopPropagation();
  473. }
  474. private function onHide(e : MouseEvent) {
  475. hide();
  476. cast(parent, NewList).draw();
  477. e.stopPropagation();
  478. }
  479. private function onSend(e : MouseEvent) {
  480. var friendList : FriendList = new FriendList();
  481. var names : Array < String > = new Array();
  482. var ids : Array < Int > = new Array();
  483. var me = this;
  484. Loader.apiRequest.getAppFriends(function(e : Array < Int > ) { ids = e; trace("ok");
  485. Loader.apiRequest.getProfiles(ids, function(t : Array < Dynamic > ) {
  486. for (i in 0...ids.length) {
  487. names[i] = "" + t[i].firstName +" " + t[i].lastName;
  488. trace(ids[i]);
  489. }
  490. for (i in 0...names.length) {
  491. friendList.addFriend(names[i], ids[i], cast(cast(me.getContent(), Item).getItemID()));
  492. }
  493. var S : Sprite = new Sprite();
  494. S.addChild(friendList);
  495. me.parent.parent.addChild(S);
  496. S.visible = true;
  497. }, function(f : String) { trace(f); } );
  498. }, function(s : String) { trace(s); } );
  499. //????? ????? ?????? id ? ???? ?????? ?????, ???????????? ??????????
  500. //names.push("name1 surname1"); names.push("name2 surname2");
  501. //ids.push(1); ids.push(2);
  502. }
  503. private function AddChan(e: MouseEvent) {
  504. Add.setNewText("????????? ? ?????????");
  505. Favorites.push(this.getContent());
  506. loader.AddToFavorites(content.getChanId());
  507. e.stopPropagation();
  508. }
  509. private function onKeyDown ( e : KeyboardEvent ) {
  510. switch ( e.keyCode ) {
  511. case Keyboard.UP : cast(parent, NewList).dispatchEvent(new ScrollEvent(ScrollEvent.UP));
  512. case Keyboard.DOWN : cast(parent, NewList).dispatchEvent(new ScrollEvent(ScrollEvent.DOWN));
  513. case Keyboard.PAGE_DOWN : cast(parent, NewList).dispatchEvent(new ScrollEvent(ScrollEvent.PAGE_DOWN));
  514. case Keyboard.PAGE_UP : cast(parent, NewList).dispatchEvent(new ScrollEvent(ScrollEvent.PAGE_UP));
  515. case Keyboard.END : cast(parent, NewList).dispatchEvent(new ScrollEvent(ScrollEvent.END));
  516. case Keyboard.HOME : cast(parent, NewList).dispatchEvent(new ScrollEvent(ScrollEvent.HOME));
  517. }
  518. }
  519. }