DirectoryReader.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. *
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. *
  20. */
  21. var exec = require('cordova/exec'),
  22. FileError = require('./FileError') ;
  23. /**
  24. * An interface that lists the files and directories in a directory.
  25. */
  26. function DirectoryReader(localURL) {
  27. this.localURL = localURL || null;
  28. this.hasReadEntries = false;
  29. }
  30. /**
  31. * Returns a list of entries from a directory.
  32. *
  33. * @param {Function} successCallback is called with a list of entries
  34. * @param {Function} errorCallback is called with a FileError
  35. */
  36. DirectoryReader.prototype.readEntries = function(successCallback, errorCallback) {
  37. // If we've already read and passed on this directory's entries, return an empty list.
  38. if (this.hasReadEntries) {
  39. successCallback([]);
  40. return;
  41. }
  42. var reader = this;
  43. var win = typeof successCallback !== 'function' ? null : function(result) {
  44. var retVal = [];
  45. for (var i=0; i<result.length; i++) {
  46. var entry = null;
  47. if (result[i].isDirectory) {
  48. entry = new (require('./DirectoryEntry'))();
  49. }
  50. else if (result[i].isFile) {
  51. entry = new (require('./FileEntry'))();
  52. }
  53. entry.isDirectory = result[i].isDirectory;
  54. entry.isFile = result[i].isFile;
  55. entry.name = result[i].name;
  56. entry.fullPath = result[i].fullPath;
  57. entry.filesystem = new (require('./FileSystem'))(result[i].filesystemName);
  58. entry.nativeURL = result[i].nativeURL;
  59. retVal.push(entry);
  60. }
  61. reader.hasReadEntries = true;
  62. successCallback(retVal);
  63. };
  64. var fail = typeof errorCallback !== 'function' ? null : function(code) {
  65. errorCallback(new FileError(code));
  66. };
  67. exec(win, fail, "File", "readEntries", [this.localURL]);
  68. };
  69. module.exports = DirectoryReader;