readEntries.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. /*
  22. * readEntries
  23. *
  24. * IN:
  25. * args
  26. * 0 - URL of directory to list
  27. * OUT:
  28. * success - Array of Entry objects
  29. * fail - FileError
  30. */
  31. var resolve = cordova.require('cordova-plugin-file.resolveLocalFileSystemURIProxy'),
  32. requestAnimationFrame = cordova.require('cordova-plugin-file.bb10RequestAnimationFrame'),
  33. createEntryFromNative = cordova.require('cordova-plugin-file.bb10CreateEntryFromNative');
  34. module.exports = function (success, fail, args) {
  35. var uri = args[0],
  36. onSuccess = function (data) {
  37. if (typeof success === 'function') {
  38. success(data);
  39. }
  40. },
  41. onFail = function (error) {
  42. if (typeof fail === 'function') {
  43. if (error.code) {
  44. fail(error.code);
  45. } else {
  46. fail(error);
  47. }
  48. }
  49. };
  50. resolve(function (fs) {
  51. requestAnimationFrame(function () {
  52. var reader = fs.nativeEntry.createReader(),
  53. entries = [],
  54. readEntries = function() {
  55. reader.readEntries(function (results) {
  56. if (!results.length) {
  57. onSuccess(entries.sort().map(createEntryFromNative));
  58. } else {
  59. entries = entries.concat(Array.prototype.slice.call(results || [], 0));
  60. readEntries();
  61. }
  62. }, onFail);
  63. };
  64. readEntries();
  65. });
  66. }, fail, [uri]);
  67. };