PageRenderTime 60ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/flash/multiupload/multiupload/src/sina/MultiUpload.as

http://chenjin.googlecode.com/
ActionScript | 935 lines | 793 code | 102 blank | 40 comment | 79 complexity | 9df6a6f51b8f79465a54b1c23804da84 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. package sina
  2. {
  3. import flash.display.GradientType;
  4. import flash.display.Graphics;
  5. import flash.display.Sprite;
  6. import flash.events.Event;
  7. import flash.events.MouseEvent;
  8. import flash.external.ExternalInterface;
  9. import flash.media.Video;
  10. import flash.net.FileFilter;
  11. import flash.net.FileReference;
  12. import flash.net.FileReferenceList;
  13. import flash.net.navigateToURL;
  14. import flash.net.URLRequest;
  15. import flash.system.Capabilities;
  16. import flash.text.TextField;
  17. import flash.text.TextFieldAutoSize;
  18. import flash.text.TextFormat;
  19. import flash.utils.*;
  20. import nl.demonsters.debugger.MonsterDebugger;
  21. import sina.external.ExternalCall;
  22. import sina.ui.controls.Button;
  23. import sina.ui.controls.Item;
  24. import sina.ui.events.ItemEvent;
  25. import sina.ui.skin.AllSkins;
  26. import sina.ui.skin.DotLine;
  27. /**
  28. * @author mole<mole1230@gmail.com>
  29. * @version $Id: MultiUpload.as 7 2010-10-19 10:38:30Z mole1230 $
  30. */
  31. public class MultiUpload extends Sprite
  32. {
  33. private const START_X:Number = 0;
  34. private const START_Y_HEAD:Number = 10;
  35. private const START_Y_IMG:Number = 60;
  36. private const START_Y_FOOT_PADDING:Number = 10;
  37. private const ITEM_MARGIN_RIGHT:Number = 10;
  38. private const ITEM_MARGIN_BOTTOM:Number = 10;
  39. private const ITEM_NUM_X:int = 6;
  40. private const IMAGE_GROUP_PADDING_X:Number = 10;
  41. private const IMAGE_GROUP_PADDING_Y:Number = 10;
  42. private const IMAGE_GROUP_MENU_HEIGHT:Number = 22;
  43. private const REMOVE_ONE:String = "remove_one";
  44. private const REMOVE_ALL:String = "remove_all";
  45. private const REMOVE_REMAIN:String = "remove_remain";
  46. private const UPDATE_TYPE_ADD:String = "update_type_add";
  47. private const UPDATE_TYPE_NEW:String = "update_type_new";
  48. private const UPDATE_TYPE_DELETE:String = "update_type_delete";
  49. private const PHASE_NORMAL:String = "phase_normal";
  50. private const PHASE_UPLOADING:String = "phase_uploading";
  51. private const PHASE_PAUSE:String = "phase_pause";
  52. private const PHASE_UPLOADED:String = "phase_uploaded";
  53. private var _uploadUrl:String = "http://www.mole.com/flash/upload.php";
  54. private var _imgFormat:String = "????|*.jpg;*.jpeg;*.gif;*.png;*.bmp";
  55. private var _token:String = "";
  56. private var _uid:int = 0;
  57. private var _maxNum:int = 32;
  58. private var _maxSize:Number = 3;
  59. private var _movieName:String = "Multiupload_0";
  60. private var _numImg:int = 0;
  61. private var _numImgUploadedError:int = 0;
  62. private var _numImgUploaded:int = 0;
  63. private var _fileList:Array = [];
  64. private var _preloadStack:Array = [];
  65. private var _selectedStack:Array = [];
  66. private var _uploadStack:Array = [];
  67. private var _itemUploading:Item;
  68. private var _phase:String = PHASE_NORMAL;
  69. private var _response:Array = [];
  70. private var _fp:int = 10;
  71. private var _isIe:int;
  72. private var _fileFilter:FileFilter;
  73. private var _fileReferenceList:FileReferenceList;
  74. private var _lineWidth:Number;
  75. private var _skins:AllSkins = new AllSkins();
  76. private var _browseBtn:Button;
  77. private var _headGroup:Sprite;
  78. private var _headGroupFlash9Tips:Button;
  79. private var _imgGroup:Sprite;
  80. private var _imgGroupBg:Sprite;
  81. private var _imgGroupTips:TextField;
  82. private var _imgGroupErrorTips:TextField;
  83. private var _imgGroupBrowseBtn:Button;
  84. private var _imgGroupNoImg:TextField;
  85. private var _imgGroupCleanBtn:Button;
  86. private var _imgGroupRemoveRemainBtn:Button;
  87. private var _imgGroupGRACWBtn:Button;
  88. private var _imgGroupGRCWBtn:Button;
  89. private var _imgGroupGDBtn:Button;
  90. private var _progressBar:Sprite;
  91. private var _progressBarBg:Sprite;
  92. private var _filePanel:Sprite;
  93. private var _footGroup:Sprite;
  94. private var _uploadBtn:Button;
  95. private var _pauseBtn:Button;
  96. private var _continueBtn:Button;
  97. private var _setHeightCallback:String;
  98. private var _endUploadCallback:String;
  99. public function MultiUpload()
  100. {
  101. var v:int = int(Capabilities.version.replace(/,/, " ").split(" ")[1]);
  102. if (v) {
  103. _fp = int(v);
  104. }
  105. _lineWidth = IMAGE_GROUP_PADDING_X * 2 + ITEM_NUM_X * Item.WIDTH + (ITEM_NUM_X - 1) * ITEM_MARGIN_RIGHT + 1;
  106. }
  107. public function init():void
  108. {
  109. _setHeightCallback = "MULTIUpload.instances[\"" + _movieName + "\"].setHeight";
  110. _endUploadCallback = "MULTIUpload.instances[\"" + _movieName + "\"].endUpload";
  111. _addHeadGroup();
  112. _addImgGroup();
  113. _addFootGroup();
  114. _setupExternalInterface();
  115. }
  116. private function _setupExternalInterface():void
  117. {
  118. try {
  119. ExternalInterface.addCallback('startUpload', _startUpload);
  120. } catch (e:Error) {
  121. MonsterDebugger.trace('startUpload', e);
  122. }
  123. }
  124. private function _addHeadGroup():void
  125. {
  126. var paddingY:Number = 13;
  127. // head container
  128. _headGroup = new Sprite();
  129. // line
  130. //var headDotLine:Sprite = new DotLine() as Sprite;
  131. //headDotLine.x = 0;
  132. //headDotLine.y = 0;
  133. //headDotLine.width = _lineWidth;
  134. //_headGroup.addChild(headDotLine);
  135. // browse button
  136. _browseBtn = new Button();
  137. _browseBtn.setStyle("skin", _skins.browse);
  138. _browseBtn.x = 0;
  139. _browseBtn.y = 10;
  140. _browseBtn.addEventListener(MouseEvent.CLICK, _browseFile);
  141. _headGroup.addChild(_browseBtn);
  142. // tip string
  143. var headGroupNormalTips:TextField = new TextField();
  144. var tf:TextFormat = new TextFormat("??", 12, 0x666666);
  145. headGroupNormalTips.autoSize = TextFieldAutoSize.LEFT;
  146. headGroupNormalTips.selectable = false;
  147. headGroupNormalTips.defaultTextFormat = tf;
  148. var str:String = "???????" + this._maxNum + "??";
  149. str = str + ("?????" + (this._fp >= 10 ? (this._maxSize) : (3)) + "M");
  150. //if (this._fp >= 10) {
  151. // str = str + "?BMP?GIF???????3M?";
  152. //}
  153. str = str + ("??????" + _imgFormat.split("|")[1].replace(/\*./g, "").replace(/;/g, "?").toUpperCase());
  154. headGroupNormalTips.appendText(str);
  155. headGroupNormalTips.x = 75;
  156. headGroupNormalTips.y = paddingY;
  157. _headGroup.addChild(headGroupNormalTips);
  158. // flash player < 10
  159. _headGroupFlash9Tips = new Button();
  160. _headGroup.addChild(_headGroupFlash9Tips);
  161. _headGroupFlash9Tips.setStyle("skin", _skins["fp10"]);
  162. _headGroupFlash9Tips.x = headGroupNormalTips.x + headGroupNormalTips.width + 10;
  163. _headGroupFlash9Tips.y = paddingY;
  164. _headGroupFlash9Tips.useHandCursor = true;
  165. _headGroupFlash9Tips.visible = _fp < 10;
  166. _headGroupFlash9Tips.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void {
  167. try {
  168. navigateToURL(
  169. new URLRequest("http://fpdownload.adobe.com/get/flashplayer/current/install_flash_player_ax.exe")
  170. );
  171. } catch (e:Error) {}
  172. });
  173. _headGroup.x = START_X;
  174. _headGroup.y = START_Y_HEAD;
  175. addChild(_headGroup);
  176. }
  177. private function _addImgGroup():void
  178. {
  179. var paddingX:Number = 5;
  180. var paddingY:Number = 3;
  181. var btnDistance:Number = 60;
  182. // image group container
  183. _imgGroup = new Sprite();
  184. // background
  185. _imgGroupBg = new Sprite();
  186. _imgGroup.addChild(_imgGroupBg);
  187. _imgGroupBg.x = 0;
  188. _imgGroupBg.y = 0;
  189. _drawImgGroupBg();
  190. _imgGroupBg.addEventListener(MouseEvent.CLICK, _gloalHandleItems);
  191. // tips
  192. var tf:TextFormat = new TextFormat("??", 12, 0x8C8C8C);
  193. tf.bold = true;
  194. _imgGroupTips = new TextField();
  195. _imgGroupTips.defaultTextFormat = tf;
  196. _imgGroupTips.autoSize = TextFieldAutoSize.LEFT;
  197. _imgGroupTips.selectable = false;
  198. _imgGroupTips.text = "0???????";
  199. _imgGroupTips.x = paddingX;
  200. _imgGroupTips.y = paddingY;
  201. _imgGroup.addChild(_imgGroupTips);
  202. // error tips
  203. var etf:TextFormat = new TextFormat("??", 12, 0xFF0000);
  204. etf.bold = true;
  205. _imgGroupErrorTips = new TextField();
  206. _imgGroupErrorTips.defaultTextFormat = etf;
  207. _imgGroupErrorTips.autoSize = TextFieldAutoSize.LEFT;
  208. _imgGroupErrorTips.selectable = false;
  209. _imgGroupErrorTips.text = "????????" + _maxNum + "???";
  210. _imgGroupErrorTips.x = 180;
  211. _imgGroupErrorTips.y = paddingY;
  212. _imgGroupErrorTips.visible = false;
  213. _imgGroup.addChild(_imgGroupErrorTips);
  214. // clean up button
  215. _imgGroupCleanBtn = new Button();
  216. _imgGroupCleanBtn.setStyle("skin", _skins.rmvall);
  217. _imgGroupCleanBtn.x = 120;
  218. _imgGroupCleanBtn.y = paddingY;
  219. _imgGroupCleanBtn.useHandCursor = true;
  220. _imgGroupCleanBtn.visible = false;
  221. _imgGroupCleanBtn.addEventListener(MouseEvent.CLICK, _gloalHandleItems);
  222. _imgGroup.addChild(_imgGroupCleanBtn);
  223. // remove remain button
  224. _imgGroupRemoveRemainBtn = new Button();
  225. _imgGroupRemoveRemainBtn.setStyle("skin", _skins.rmvremain);
  226. _imgGroupRemoveRemainBtn.x = 320;
  227. _imgGroupRemoveRemainBtn.y = paddingY;
  228. _imgGroupRemoveRemainBtn.useHandCursor = true;
  229. _imgGroupRemoveRemainBtn.visible = false;
  230. _imgGroupRemoveRemainBtn.addEventListener(MouseEvent.CLICK, _gloalHandleItems);
  231. _imgGroup.addChild(_imgGroupRemoveRemainBtn);
  232. // progress bar
  233. var gp:Graphics;
  234. _progressBar = new Sprite();
  235. _progressBarBg = new Sprite();
  236. gp = _progressBarBg.graphics;
  237. gp.lineStyle(1, 0xD3D3D3);
  238. gp.beginFill(0xFFFFFF);
  239. gp.drawRect(0, 0, 126, 12);
  240. gp.endFill();
  241. _progressBarBg.x = 180;
  242. _progressBarBg.y = paddingY + 2;
  243. _progressBarBg.visible = false;
  244. _imgGroup.addChild(_progressBarBg);
  245. gp = _progressBar.graphics;
  246. gp.lineStyle();
  247. gp.beginGradientFill(GradientType.LINEAR, [0xE7ECF0, 0x1B68AB], [1, 1], [0, 255]);
  248. gp.drawRect(0, 0, 124, 10);
  249. gp.endFill();
  250. _progressBar.x = 180 + 1.5;
  251. _progressBar.y = paddingY + 3.5;
  252. _progressBar.scaleX = 0;
  253. _progressBar.visible = false;
  254. _imgGroup.addChild(_progressBar);
  255. // acw button
  256. _imgGroupGRACWBtn = new Button();
  257. _imgGroupGRACWBtn.setStyle("skin", _skins.gacw);
  258. _imgGroupGRACWBtn.x = _lineWidth - 180;
  259. _imgGroupGRACWBtn.y = paddingY;
  260. _imgGroupGRACWBtn.useHandCursor = true;
  261. _imgGroupGRACWBtn.enabled = false;
  262. _imgGroupGRACWBtn.addEventListener(MouseEvent.CLICK, _gloalHandleItems);
  263. _imgGroup.addChild(_imgGroupGRACWBtn);
  264. // cw button
  265. _imgGroupGRCWBtn = new Button();
  266. _imgGroupGRCWBtn.setStyle("skin", _skins.gcw);
  267. _imgGroupGRCWBtn.x = _imgGroupGRACWBtn.x + btnDistance;
  268. _imgGroupGRCWBtn.y = paddingY;
  269. _imgGroupGRCWBtn.useHandCursor = true;
  270. _imgGroupGRCWBtn.enabled = false;
  271. _imgGroupGRCWBtn.addEventListener(MouseEvent.CLICK, _gloalHandleItems);
  272. _imgGroup.addChild(_imgGroupGRCWBtn);
  273. // delete button
  274. _imgGroupGDBtn = new Button();
  275. _imgGroupGDBtn.setStyle("skin", _skins.gd);
  276. _imgGroupGDBtn.x = _imgGroupGRCWBtn.x + btnDistance;
  277. _imgGroupGDBtn.y = paddingY;
  278. _imgGroupGDBtn.useHandCursor = true;
  279. _imgGroupGDBtn.enabled = false;
  280. _imgGroupGDBtn.addEventListener(MouseEvent.CLICK, _gloalHandleItems);
  281. _imgGroup.addChild(_imgGroupGDBtn);
  282. // group browse button
  283. _imgGroupBrowseBtn = new Button();
  284. _imgGroupBrowseBtn.setStyle("skin", _skins.browseC);
  285. _imgGroupBrowseBtn.x = (_imgGroupBg.width - 157) / 2;
  286. _imgGroupBrowseBtn.y = (_imgGroupBg.height - 45) / 2;
  287. _imgGroupBrowseBtn.useHandCursor = true;
  288. _imgGroupBrowseBtn.addEventListener(MouseEvent.CLICK, _browseFile);
  289. _imgGroup.addChild(_imgGroupBrowseBtn);
  290. // no image
  291. var tfNoImg:TextFormat = new TextFormat("??", 12, 0x006530);
  292. tfNoImg.bold = true;
  293. _imgGroupNoImg = new TextField();
  294. _imgGroupNoImg.defaultTextFormat = tfNoImg;
  295. _imgGroupNoImg.autoSize = TextFieldAutoSize.LEFT;
  296. _imgGroupNoImg.selectable = false;
  297. _imgGroupNoImg.text = "??????? " + this._maxNum + " ???";
  298. _imgGroupNoImg.x = (_imgGroupBg.width - _imgGroupNoImg.width) / 2;
  299. _imgGroupNoImg.y = (_imgGroupBg.height - _imgGroupNoImg.height) / 2 + 35;
  300. _imgGroup.addChild(_imgGroupNoImg);
  301. // image container
  302. _filePanel = new Sprite();
  303. _filePanel.x = IMAGE_GROUP_PADDING_X;
  304. _filePanel.y = IMAGE_GROUP_PADDING_Y + IMAGE_GROUP_MENU_HEIGHT;
  305. _imgGroup.addChild(_filePanel);
  306. _imgGroup.x = START_X;
  307. _imgGroup.y = START_Y_IMG;
  308. addChild(_imgGroup);
  309. }
  310. private function _addFootGroup():void
  311. {
  312. var paddingY:Number = 10;
  313. // foot container;
  314. _footGroup = new Sprite();
  315. // line
  316. //var headDotLine:Sprite = new DotLine() as Sprite;
  317. //headDotLine.x = 0;
  318. //headDotLine.y = 0;
  319. //headDotLine.width = _lineWidth;
  320. //_footGroup.addChild(headDotLine);
  321. // upload button
  322. _uploadBtn = new Button();
  323. _uploadBtn.setStyle("skin", _skins.start);
  324. _uploadBtn.x = 0;
  325. _uploadBtn.y = paddingY;
  326. _uploadBtn.useHandCursor = true;
  327. _uploadBtn.enabled = false;
  328. _uploadBtn.addEventListener(MouseEvent.CLICK, _startUpload);
  329. _footGroup.addChild(_uploadBtn);
  330. // pause button
  331. _pauseBtn = new Button();
  332. _pauseBtn.setStyle("skin", _skins.pause);
  333. _pauseBtn.x = 0;
  334. _pauseBtn.y = paddingY
  335. _pauseBtn.useHandCursor = true;
  336. _pauseBtn.visible = false;
  337. _pauseBtn.addEventListener(MouseEvent.CLICK, _pauseUpload);
  338. _footGroup.addChild(_pauseBtn);
  339. // continue button
  340. _continueBtn = new Button();
  341. _continueBtn.setStyle("skin", _skins.contn);
  342. _continueBtn.x = 0;
  343. _continueBtn.y = paddingY;
  344. _continueBtn.useHandCursor = true;
  345. _continueBtn.visible = false;
  346. _continueBtn.addEventListener(MouseEvent.CLICK, _continueUpload);
  347. _footGroup.addChild(_continueBtn);
  348. _footGroup.x = START_X;
  349. _footGroup.y = _imgGroup.y + _imgGroup.height + START_Y_FOOT_PADDING;
  350. addChild(_footGroup);
  351. }
  352. private function _continueUpload(e:MouseEvent):void
  353. {
  354. _phase = PHASE_UPLOADING;
  355. _handleUpload();
  356. }
  357. private function _pauseUpload(e:MouseEvent):void
  358. {
  359. _phase = PHASE_PAUSE;
  360. _itemUploading.pauseUpload();
  361. _uploadStack.unshift(_itemUploading);
  362. _updateStatus(false);
  363. }
  364. private function _startUpload(e:MouseEvent = null):void
  365. {
  366. _phase = PHASE_UPLOADING;
  367. _selectedStack = [];
  368. _uploadStack = [];
  369. var i:int = 0;
  370. var length:int = _filePanel.numChildren;
  371. var node:Item;
  372. while (i < length) {
  373. node = _filePanel.getChildAt(i) as Item;
  374. node.uploadStatus = Item.UPLOAD_STATUS_WAITING;
  375. _uploadStack.push(node);
  376. i++;
  377. }
  378. _handleUpload();
  379. }
  380. private function _handleUpload():void
  381. {
  382. _updateStatus(false);
  383. if (_uploadStack.length > 0) {
  384. var node:Item;
  385. node = _uploadStack.shift();
  386. _itemUploading = node;
  387. node.startUpload();
  388. } else {
  389. _endUpload();
  390. }
  391. }
  392. private function _endUpload():void
  393. {
  394. _itemUploading = null;
  395. _phase = PHASE_UPLOADED;
  396. _updateStatus(false);
  397. if (_numImgUploadedError <= 0) {
  398. try {
  399. ExternalCall.endUpload(_endUploadCallback, _response);
  400. } catch (e:Error) {
  401. MonsterDebugger.trace("endUpload", e);
  402. }
  403. }
  404. }
  405. private function _gloalHandleItems(e:MouseEvent):void
  406. {
  407. var node:* = e.target;
  408. var item:Item;
  409. switch (node) {
  410. case _imgGroupBg:
  411. for each (item in _selectedStack) {
  412. item.selected = false;
  413. }
  414. _selectedStack = [];
  415. _updateBtnStatus();
  416. break;
  417. case _imgGroupCleanBtn:
  418. _updateFilePanel(null, UPDATE_TYPE_DELETE, null, REMOVE_ALL);
  419. break;
  420. case _imgGroupGDBtn:
  421. while (_selectedStack.length > 0) {
  422. _updateFilePanel(null, UPDATE_TYPE_DELETE, _selectedStack[0]);
  423. }
  424. break;
  425. case _imgGroupGRCWBtn:
  426. for each (item in _selectedStack) {
  427. item.rotationImg(Item.ROTATE_CW);
  428. }
  429. break;
  430. case _imgGroupGRACWBtn:
  431. for each (item in _selectedStack) {
  432. item.rotationImg(Item.ROTATE_ACW);
  433. }
  434. break;
  435. case _imgGroupRemoveRemainBtn:
  436. _updateFilePanel(null, UPDATE_TYPE_DELETE, null, REMOVE_REMAIN);
  437. break;
  438. default:
  439. break;
  440. }
  441. }
  442. private function _drawImgGroupBg():void
  443. {
  444. var allHeight:Number;
  445. var imgGroupWidth:Number = _lineWidth - 1;
  446. var num:int = _filePanel ? _filePanel.numChildren : 0;
  447. var rows:int = Math.max(Math.ceil(num / ITEM_NUM_X) , 1);
  448. var bgHeight:Number = IMAGE_GROUP_PADDING_Y * 2 + IMAGE_GROUP_MENU_HEIGHT + rows * Item.HEIGHT + (rows - 1) * ITEM_MARGIN_BOTTOM;
  449. if (_imgGroupBg && bgHeight != (_imgGroupBg.height - 1)) {
  450. var gp:Graphics = _imgGroupBg.graphics;
  451. gp.clear();
  452. gp.lineStyle(1, 0xD0D0D0);
  453. gp.beginFill(0xFFFFFF);
  454. gp.drawRect(0, 0, imgGroupWidth, bgHeight);
  455. gp.endFill();
  456. gp.lineStyle();
  457. gp.beginFill(0xEEEEEE);
  458. gp.drawRect(0.5, 0.5, imgGroupWidth - 0.5, IMAGE_GROUP_MENU_HEIGHT);
  459. gp.endFill();
  460. if (_footGroup) {
  461. _footGroup.y = _imgGroup.y + _imgGroup.height + START_Y_FOOT_PADDING;
  462. allHeight = _footGroup.y + _footGroup.height + 10;
  463. try {
  464. ExternalCall.setHeight(_setHeightCallback, allHeight);
  465. } catch (e:Error) {
  466. MonsterDebugger.trace("setHeight", e);
  467. }
  468. }
  469. }
  470. }
  471. private function _browseFile(e:MouseEvent):void
  472. {
  473. _fileReferenceList = new FileReferenceList();
  474. _fileReferenceList.addEventListener(Event.SELECT, _selectFile);
  475. _fileReferenceList.addEventListener(Event.CANCEL, _selectFile);
  476. _fileReferenceList.browse([_fileFilter]);
  477. }
  478. private function _selectFile(e:Event):void
  479. {
  480. var num:int;
  481. var preloadFileList:Array;
  482. switch (e.type) {
  483. case Event.SELECT:
  484. if (!_fileList) {
  485. preloadFileList = _fileReferenceList.fileList;
  486. num = preloadFileList.length;
  487. if (num > _maxNum) {
  488. _fileList = _fileList.splice(0, _maxNum);
  489. _showErrorTips();
  490. }
  491. _fileList = preloadFileList;
  492. _updateFilePanel(preloadFileList);
  493. } else {
  494. preloadFileList = _uniqueFileList(_fileReferenceList.fileList);
  495. num = _maxNum - _numImg;
  496. if (num > 0) {
  497. if (num < preloadFileList.length) {
  498. preloadFileList = preloadFileList.splice(0, num);
  499. _showErrorTips();
  500. }
  501. _fileList = _fileList.concat(preloadFileList);
  502. _updateFilePanel(preloadFileList, UPDATE_TYPE_ADD);
  503. }
  504. }
  505. break;
  506. case Event.CANCEL:
  507. break;
  508. default:
  509. break;
  510. }
  511. }
  512. private function _showErrorTips(str:String = ""):void
  513. {
  514. if (str) {
  515. _imgGroupErrorTips.text = str;
  516. }
  517. _imgGroupErrorTips.visible = true;
  518. setTimeout(function():void {
  519. _imgGroupErrorTips.visible = false;
  520. }, 5000);
  521. }
  522. private function _updateFilePanel(fileList:Array, type:String = UPDATE_TYPE_NEW, node:Item = null, rmType:String = REMOVE_ONE):void
  523. {
  524. switch (type) {
  525. case UPDATE_TYPE_NEW:
  526. _pushPreloadStack(fileList, UPDATE_TYPE_NEW);
  527. _numImg = fileList.length;
  528. _startPreload();
  529. break;
  530. case UPDATE_TYPE_ADD:
  531. _pushPreloadStack(fileList, UPDATE_TYPE_ADD);
  532. _numImg += fileList.length;
  533. _startPreload();
  534. break;
  535. case UPDATE_TYPE_DELETE:
  536. switch (rmType) {
  537. case REMOVE_ALL:
  538. while (_filePanel.numChildren) {
  539. _filePanel.removeChildAt(0);
  540. }
  541. _resetVariables();
  542. break;
  543. case REMOVE_REMAIN:
  544. var node:Item;
  545. while (_uploadStack.length) {
  546. node = _uploadStack[_uploadStack.length - 1] as Item;
  547. _filePanel.removeChild(node);
  548. _spliceVariables(node);
  549. }
  550. break;
  551. default:
  552. _filePanel.removeChild(node);
  553. _spliceVariables(node);
  554. break;
  555. }
  556. _rearrangeFilePanelChildren();
  557. break;
  558. default:
  559. break;
  560. }
  561. _updateStatus(true);
  562. }
  563. private function _spliceVariables(node:Item):void
  564. {
  565. var pos:int;
  566. pos = _selectedStack.indexOf(node);
  567. (pos > -1) && _selectedStack.splice(pos, 1);
  568. pos = _fileList.indexOf(node.fileReference);
  569. (pos > -1) && _fileList.splice(pos, 1);
  570. pos = _uploadStack.indexOf(node);
  571. (pos > -1) && _uploadStack.splice(pos, 1);
  572. _numImg--;
  573. }
  574. private function _rearrangeFilePanelChildren():void
  575. {
  576. var length:int = _filePanel.numChildren;
  577. var i:int = 0;
  578. var node:Item;
  579. while (i < length) {
  580. node = _filePanel.getChildAt(i) as Item;
  581. if (node) {
  582. node.index = i;
  583. node.x = i % ITEM_NUM_X * (Item.WIDTH + ITEM_MARGIN_RIGHT);
  584. node.y = Math.floor(i / ITEM_NUM_X) * (Item.HEIGHT + ITEM_MARGIN_BOTTOM);
  585. }
  586. i++;
  587. }
  588. }
  589. private function _updateStatus(isDrawBg:Boolean = true):void
  590. {
  591. _updateTips();
  592. _updateBtnStatus();
  593. if (isDrawBg) {
  594. _drawImgGroupBg()
  595. }
  596. }
  597. private function _updateTips():void
  598. {
  599. var str:String;
  600. switch (_phase) {
  601. case PHASE_NORMAL:
  602. str = _numImg + "???????";
  603. break;
  604. default:
  605. str = "?" + _numImg + "????????" + _numImgUploaded + "?";
  606. break;
  607. }
  608. _imgGroupTips.text = str;
  609. }
  610. private function _updateBtnStatus():void
  611. {
  612. _numImg = (_numImg < 0) ? 0 : _numImg;
  613. _numImgUploaded = (_numImgUploaded < 0) ? 0 : _numImgUploaded;
  614. _imgGroupGDBtn.enabled =
  615. _imgGroupGRCWBtn.enabled =
  616. _imgGroupGRACWBtn.enabled = Boolean(_selectedStack.length);
  617. _imgGroupNoImg.visible =
  618. _imgGroupBrowseBtn.visible = (_numImg == 0);
  619. if (_phase == PHASE_NORMAL) {
  620. _imgGroupCleanBtn.visible = Boolean(_numImg);
  621. if (_numImg < _maxNum) {
  622. _browseBtn.enabled = (_preloadStack.length == 0);
  623. }
  624. if (_numImg > 0) {
  625. _uploadBtn.enabled = (_preloadStack.length == 0);
  626. } else {
  627. _uploadBtn.enabled = false
  628. }
  629. } else {
  630. _progressBarBg.visible = true;
  631. _progressBar.visible = true;
  632. _progressBar.scaleX = _numImgUploaded / _numImg;
  633. _browseBtn.enabled = false;
  634. _imgGroupCleanBtn.visible = false;
  635. _imgGroupRemoveRemainBtn.visible = !(_filePanel.numChildren == _numImgUploaded);
  636. _uploadBtn.visible = false;
  637. if (_phase == PHASE_UPLOADING) {
  638. _pauseBtn.visible = true;
  639. _continueBtn.visible = false;
  640. } else if (_phase == PHASE_PAUSE) {
  641. _pauseBtn.visible = false;
  642. _continueBtn.visible = true;
  643. } else if (_phase == PHASE_UPLOADED) {
  644. _uploadBtn.visible = false;
  645. _pauseBtn.visible = false;
  646. _continueBtn.visible = false;
  647. }
  648. }
  649. }
  650. private function _resetVariables():void
  651. {
  652. _numImg = 0;
  653. _numImgUploadedError = 0;
  654. _numImgUploaded = 0;
  655. _fileList = [];
  656. _preloadStack = [];
  657. _selectedStack = [];
  658. _uploadStack = [];
  659. _phase = PHASE_NORMAL;
  660. }
  661. private function _pushPreloadStack(fileList:Array, type:String = UPDATE_TYPE_NEW):void
  662. {
  663. var file:FileReference;
  664. var node:Item;
  665. var i:*;
  666. for (i in fileList) {
  667. i = int(i);
  668. file = fileList[i];
  669. node = new Item();
  670. node.fp = _fp;
  671. node.index = _numImg + i;
  672. node.uploadUrl = _uploadUrl;
  673. node.delay = i;
  674. node.maxSize = _maxSize;
  675. node.fileReference = file;
  676. node.setRequestVariables("token", _token);
  677. node.setRequestVariables("uid", _uid);
  678. node.x = (_numImg + i) % ITEM_NUM_X * (Item.WIDTH + ITEM_MARGIN_RIGHT);
  679. node.y = Math.floor((_numImg + i) / ITEM_NUM_X) * (Item.HEIGHT + ITEM_MARGIN_BOTTOM);
  680. node.addEventListener(ItemEvent.DOWN, _itemEvent);
  681. node.addEventListener(ItemEvent.DELETE, _itemEvent);
  682. node.addEventListener(ItemEvent.SELECT_CHANGED, _itemEvent);
  683. node.addEventListener(ItemEvent.UNSELECT_OTHERS, _itemEvent);
  684. node.addEventListener(ItemEvent.PRELOAD_COMPLETE, _itemEvent);
  685. node.addEventListener(ItemEvent.UPLOAD_SIZE_ERROR, _itemEvent);
  686. node.addEventListener(ItemEvent.UPLOAD_PRELOAD_ERROR, _itemEvent);
  687. node.addEventListener(ItemEvent.UPLOAD_START, _itemEvent);
  688. node.addEventListener(ItemEvent.UPLOAD_SUCCESS, _itemEvent);
  689. node.addEventListener(ItemEvent.UPLOAD_FAILURE, _itemEvent);
  690. node.addEventListener(ItemEvent.UPLOAD_COMPLETE, _itemEvent);
  691. _filePanel.addChild(node);
  692. _preloadStack.push(node);
  693. }
  694. }
  695. private function _itemEvent(e:Event):void
  696. {
  697. var node:Item = e.target as Item;
  698. switch (e.type) {
  699. case ItemEvent.DELETE:
  700. _updateFilePanel(null, UPDATE_TYPE_DELETE, node);
  701. break;
  702. case ItemEvent.UNSELECT_OTHERS:
  703. var tmp:Item;
  704. for each (tmp in _selectedStack) {
  705. if (tmp != node) {
  706. tmp.selected = false;
  707. }
  708. }
  709. _selectedStack = [node];
  710. _updateBtnStatus();
  711. break;
  712. case ItemEvent.SELECT_CHANGED:
  713. if (node.selected) {
  714. _selectedStack.push(node);
  715. } else {
  716. _selectedStack.splice(_selectedStack.indexOf(node), 1);
  717. }
  718. _updateBtnStatus();
  719. break;
  720. case ItemEvent.PRELOAD_COMPLETE:
  721. _handlePreload();
  722. break;
  723. case ItemEvent.UPLOAD_START:
  724. node.selected = false;
  725. var pos:int = _selectedStack.indexOf(node);
  726. (pos > -1) && _selectedStack.splice(pos, 1);
  727. break;
  728. case ItemEvent.UPLOAD_SUCCESS:
  729. _response.push(e.target.json);
  730. break;
  731. case ItemEvent.UPLOAD_SIZE_ERROR:
  732. case ItemEvent.UPLOAD_FAILURE:
  733. _numImgUploadedError++;
  734. break;
  735. case ItemEvent.UPLOAD_COMPLETE:
  736. _numImgUploaded++;
  737. _handleUpload();
  738. break;
  739. default:
  740. break;
  741. }
  742. }
  743. private function _startPreload():void
  744. {
  745. _browseBtn.enabled = false;
  746. _uploadBtn.enabled = false;
  747. _handlePreload();
  748. }
  749. private function _handlePreload():void
  750. {
  751. if (_preloadStack.length > 0) {
  752. var node:Item;
  753. node = _preloadStack.shift();
  754. node.starPreload();
  755. } else {
  756. _endPreload();
  757. }
  758. }
  759. private function _endPreload():void
  760. {
  761. _updateBtnStatus();
  762. }
  763. private function _uniqueFileList(fileList:Array):Array
  764. {
  765. var collectList:Array = [];
  766. var item:*;
  767. var sItem:*;
  768. for each (item in fileList) {
  769. var isNew:Boolean = true;
  770. for each (sItem in _fileList) {
  771. if (item.type == sItem.type
  772. && item.size == sItem.size
  773. && item.modificationDate.time == sItem.modificationDate.time
  774. && item.creationDate.time == sItem.creationDate.time) {
  775. isNew = false;
  776. break;
  777. }
  778. }
  779. if (isNew) {
  780. collectList.push(item);
  781. }
  782. }
  783. return collectList;
  784. }
  785. public function set uploadUrl(value:String):void
  786. {
  787. if (value) {
  788. _uploadUrl = value;
  789. }
  790. }
  791. public function set imgFormat(value:String):void
  792. {
  793. var arr:Array;
  794. if (value) {
  795. _imgFormat = value;
  796. }
  797. arr = _imgFormat.split("|");
  798. if (arr.length > 1) {
  799. _fileFilter = new FileFilter(arr[0], arr[1]);
  800. }
  801. }
  802. public function set token(value:String):void
  803. {
  804. if (value) {
  805. _token = value;
  806. }
  807. }
  808. public function set uid(value:int):void
  809. {
  810. if (value) {
  811. _uid = value;
  812. }
  813. }
  814. public function set maxNum(value:int):void
  815. {
  816. if (value) {
  817. _maxNum = value;
  818. }
  819. }
  820. public function set maxSize(value:Number):void
  821. {
  822. if (value) {
  823. _maxSize = value;
  824. }
  825. }
  826. public function set movieName(value:String):void
  827. {
  828. if (value) {
  829. _movieName = value;
  830. }
  831. }
  832. }
  833. }