file.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta charset="utf-8"/>
  5. <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
  6. <meta name="HandheldFriendly" content="true"/>
  7. <meta name="MobileOptimized" content="320"/>
  8. <title>Hello H5+</title>
  9. <script type="text/javascript" src="../js/common.js"></script>
  10. <script type="text/javascript">
  11. var dir="",root=[],current=null,parent=null,pitem=null,list=null;
  12. var htmlItem='<img class="ficon"></img><div><span class="fname"></span><br/><span class="finf">...</span></div>';
  13. document.addEventListener("plusready", function(){
  14. pitem = document.getElementById("pdir");
  15. list = document.getElementById("dcontent");
  16. // Get root item information
  17. var items = list.querySelectorAll(".fitem");
  18. for ( var i = 1; i < items.length; i++ ){
  19. updateRootItem(items[i]);
  20. }
  21. }, false);
  22. // Update root information with item(HTMLUIElement)
  23. function updateRootItem( item ) {
  24. plus.io.resolveLocalFileSystemURL( dir+item.id, function ( entry ) {
  25. root.push( entry );
  26. item.entry = entry;
  27. updateInf( item, entry );
  28. }, function ( e ) {
  29. outLine( "Update "+item.id+" information failed: "+e.message );
  30. } );
  31. }
  32. // Update HTMLUIElement information with entry object
  33. function updateInf( item, entry ) {
  34. entry.getMetadata( function ( metadata ) {
  35. var inf = item.querySelector(".finf");
  36. if ( entry.isDirectory ) {
  37. inf.innerText = "文件夹:"+metadata.directoryCount+"项,文件:"+metadata.fileCount+"项";
  38. } else {
  39. inf.innerText = dateToStr(metadata.modificationTime);
  40. }
  41. }, function ( e ) {
  42. outLine( "Get metadata "+entry.name+" failed: "+e.message );
  43. }, false );
  44. }
  45. // Update ui with entries
  46. function updateList( entries ) {
  47. var i,items = [].slice.apply(list.querySelectorAll(".fitem"));
  48. items.shift();
  49. // sort the entries
  50. entries.sort( sortCompareEntry )
  51. // Update item to ui
  52. for ( i = 0; i < entries.length; i++ ) {
  53. var di = null;
  54. if ( i < items.length ) {
  55. di=items[i];
  56. di.style.display = "block";
  57. } else {
  58. di = document.createElement("div");
  59. di.className = "fitem";
  60. di.setAttribute( "onclick", "openDir(this);" );
  61. di.innerHTML = htmlItem;
  62. list.appendChild( di );
  63. }
  64. di.entry = entries[i];
  65. di.id = di.entry.name;
  66. di.querySelector(".fname").innerText = di.id;
  67. di.querySelector(".finf").innerText = "";
  68. if ( entries === root ) {
  69. di.querySelector(".ficon").src = "../img/fdisk.png";
  70. } else {
  71. di.querySelector(".ficon").src = di.entry.isDirectory?"../img/fdir.png":"../img/ffile.png";
  72. }
  73. updateInf( di, di.entry );
  74. }
  75. // Hide other items
  76. for ( ; i < items.length; i++ ) {
  77. items[i].style.display = "none";
  78. items[i].entry = null;
  79. }
  80. // Reset scroll offset
  81. list.scrollTop = 0;
  82. }
  83. // Open directory with item(HTMLUIElement)
  84. function openDir( item ) {
  85. var entry = item.entry;
  86. if ( !entry ) {
  87. outSet( "Open directory \""+item.id+"\" with null!" );
  88. return;
  89. }
  90. if ( entry.isDirectory ) {
  91. outSet( "Open directory: \""+dir+item.id+"\"" );
  92. var dirReader = entry.createReader();
  93. dirReader.readEntries( function( entries ) {
  94. parent = current;
  95. current = item.entry;
  96. dir = entry.toURL()+"/";
  97. // Dispaly up to parent item
  98. pitem.style.display = "block";
  99. // Update ui
  100. updateList( entries );
  101. }, function ( e ) {
  102. outLine( "Read directory "+item.id+" failed: "+e.message );
  103. } );
  104. } else {
  105. outSet( "Open file: \""+dir+item.id+"\"" );
  106. plus.runtime.openFile( dir+item.id, {}, function ( e ) {
  107. plus.nativeUI.alert( "无法打开此文件:"+e.message );
  108. } );
  109. }
  110. }
  111. // Back to parent directory
  112. function parentDir() {
  113. outSet( "Go to previous directory: \""+dir+"\"");
  114. var p = dir.lastIndexOf( "/", dir.length-2 );
  115. if ( p < 0 || !parent ) { // Up to root
  116. dir = "";
  117. current = parent = null;
  118. // hide up to parent item
  119. pitem.style.display = "none";
  120. // Update ui
  121. updateList( root );
  122. } else {
  123. var dirReader = parent.createReader();
  124. dirReader.readEntries( function( entries ) {
  125. dir = dir.substr( 0, p+1 );
  126. outLine( "Current directory: \""+dir+"\"" );
  127. current = parent;
  128. current.getParent( function ( entry ) {
  129. parent = entry;
  130. }, function ( e ) {
  131. outLine( "Get \""+current.name+"\" parent directory failed: "+e.message );
  132. } );
  133. parent = null;
  134. // Update ui
  135. updateList( entries );
  136. }, function ( e ) {
  137. outLine( "Read directory "+item.id+" failed: "+e.message );
  138. } );
  139. }
  140. }
  141. // Entry sort compare
  142. function sortCompareEntry( a, b ) {
  143. if ( a.isDirectory && b.isFile ) {
  144. return -1;
  145. } else if ( a.isFile && b.isDirectory ) {
  146. return 1;
  147. } else {
  148. return a.name - b.name;
  149. }
  150. }
  151. // Format data to string
  152. function dateToStr( datetime ) {
  153. var year = datetime.getFullYear(),
  154. month = datetime.getMonth()+1,
  155. date = datetime.getDate(),
  156. hour = datetime.getHours(),
  157. minutes = datetime.getMinutes(),
  158. second = datetime.getSeconds();
  159. if ( month < 10 ) {
  160. month = "0" + month;
  161. }
  162. if ( date < 10 ) {
  163. date = "0" + date;
  164. }
  165. if ( hour < 10 ) {
  166. hour = "0" + hour;
  167. }
  168. if ( minutes < 10 ) {
  169. minutes = "0" + minutes;
  170. }
  171. if ( second < 10 ) {
  172. second = "0" + second;
  173. }
  174. return (year+"-"+month+"-"+date+" "+hour+":"+minutes+":"+second);
  175. }
  176. </script>
  177. <link rel="stylesheet" href="../css/common.css" type="text/css" charset="utf-8"/>
  178. <style type="text/css">
  179. .fitem {
  180. width: 96%;
  181. overflow: hidden;
  182. padding: 10px 2%;
  183. border-bottom: 1px solid #c6c6c6;
  184. text-align: left;
  185. }
  186. .fitem:active {
  187. background: #f4f4f4;
  188. }
  189. .ficon {
  190. height: 40px;
  191. float: left;
  192. margin-right: 8px;
  193. }
  194. .fup {
  195. line-height:40px;
  196. }
  197. .fname {
  198. font-weight: bolder;
  199. height: 22px;
  200. font-size: 16px;
  201. }
  202. .finf {
  203. height: 18px;
  204. font-size: 12px;
  205. }
  206. </style>
  207. </head>
  208. <body>
  209. <div id="dcontent">
  210. <div id="pdir" class="fitem" style="display:none" onclick="parentDir()">
  211. <img class="ficon" src="../img/fup.png"/>
  212. <div class="fup"><span class="fname">返回上一级</span></div>
  213. </div>
  214. <div id="_www" class="fitem" onclick="openDir(this)">
  215. <img class="ficon" src="../img/fdisk.png"/>
  216. <div>
  217. <span class="fname">www</span><br/>
  218. <span class="finf">...</span>
  219. </div>
  220. </div>
  221. <div id="_doc" class="fitem" onclick="openDir(this)">
  222. <img class="ficon" src="../img/fdisk.png"/>
  223. <div>
  224. <span class="fname">doc</span><br/>
  225. <span class="finf">...</span>
  226. </div>
  227. </div>
  228. <div id="_documents" class="fitem" onclick="openDir(this)">
  229. <img class="ficon" src="../img/fdisk.png"/>
  230. <div>
  231. <span class="fname">documents</span><br/>
  232. <span class="finf">...</span>
  233. </div>
  234. </div>
  235. <div id="_downloads" class="fitem" onclick="openDir(this)">
  236. <img class="ficon" src="../img/fdisk.png"/>
  237. <div>
  238. <span class="fname">downloads</span><br/>
  239. <span class="finf">...</span>
  240. </div>
  241. </div>
  242. </div>
  243. <div id="outpos"/>
  244. <div id="output">
  245. File System模块管理本地文件系统,用于文件系统目录的浏览、文件的写入、文件的读取等操作。
  246. </div>
  247. </body>
  248. </html>