angular and chrome.fileSystem

118 views
Skip to first unread message

Alexandre LESAGE

unread,
Mar 20, 2015, 5:44:34 PM3/20/15
to ang...@googlegroups.com
Hello,

I would like to display locals directories and files with chrome.System and angular.

So I have a controller :
angular.module('DirectoryBrowser', [])
  .controller('DirectoryBrowserCtrl', ['$scope', '$http',
    function ($scope, $http) {
      $scope.openModalChooseDirectory = function($scope) {
        chrome.fileSystem.chooseEntry({type: 'openDirectory'}, function(theEntry) {
          if (!theEntry) {
            output.textContent = 'No Directory selected.';
            return;
          }
          // use local storage to retain access to this file
          chrome.storage.local.set({'chosenFile': chrome.fileSystem.retainEntry(theEntry)});
          loadDirEntry(theEntry);
        });
      };
  }])
;


and this html : 
<a ng-click="openModalChooseDirectory()">

Si when a click on the link, a modal appear and let the user choose a local directory.

The function loadDirEntry is like that :
// for directories, read the contents of the top-level directory (ignore sub-dirs)
// and put the results into the textarea, then disable the Save As button
function loadDirEntry(_chosenEntry) {
  chosenEntry = _chosenEntry;
  if (chosenEntry.isDirectory) {
    var dirReader = chosenEntry.createReader();
    var entries = [];

    // Call the reader.readEntries() until no more results are returned.
    var readEntries = function() {
        dirReader.readEntries (function(results) {
        if (!results.length) {
          textarea.value = entries.join("\n");
          saveFileButton.disabled = true; // don't allow saving of the list
          displayEntryData(chosenEntry);
        }
        else {
          results.forEach(function(item) {
            entries = entries.concat(item.fullPath);
          });
          console.log(entries);
          readEntries();
        }
      }, errorHandler);
    };

    readEntries(); // Start reading dirs.
  }
}

the console.log contains all absolute path of directories in directory selected. So what I want...

First, I would like to populate $scope.directory with entries in order to display directories in my html. Have you any idea about how can I put entries in $scope.directory ?

The code I took about chrome.fileSystem provide from this project : https://github.com/GoogleChrome/chrome-app-samples/blob/master/samples/filesystem-access/js/app.js

Thanks,
AlexL

Sander Elias

unread,
Mar 21, 2015, 3:21:03 AM3/21/15
to ang...@googlegroups.com

Hi AlexL,

Return the entries var, and assign it to a scope var.
Something like: scope.entries = loadDirEntry(theEntry).

Does that help a bit. Building a plunk is a bit moot for this, as it only works in chrome apps ;)

Regards
Sander

Alexandre LESAGE

unread,
Mar 21, 2015, 6:56:44 AM3/21/15
to ang...@googlegroups.com
Hi Sander Elias,

Sorry I have not thought about making plunker.
I would have like to return entries of loadDirEntry(theEntry) but in openModalChooseDirectory $scope is undefined. How can I pass $scope into function ?

Thank for your help.
AlexL

--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/CPGWL1E35IY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Alexandre LESAGE

unread,
Mar 21, 2015, 11:13:00 AM3/21/15
to ang...@googlegroups.com
After some test, it works good : 
angular.module('DirectoryBrowser', [])
  .controller('DirectoryBrowserCtrl', ['$scope', '$http',
    function ($scope, $http) {
      $scope.openModalChooseDirectory = function() {
        chrome.fileSystem.chooseEntry({type: 'openDirectory'}, function(theEntry) {
          if (!theEntry) {
            console.log('No Directory selected.');
            return;
          }
          $scope.entryChoosen = theEntry;
          $scope.$apply();
          // use local storage to retain access to this file
          loadDirEntry(theEntry, $scope);
        });
      };
    }
  ])
;


// for directories, read the contents of the top-level directory (ignore sub-dirs)
// and put the results into the textarea, then disable the Save As button
function loadDirEntry(_chosenEntry, $scope) {
  chosenEntry = _chosenEntry;
  if (chosenEntry.isDirectory) {
    var dirReader = chosenEntry.createReader();
    var entries = [];
    // Call the reader.readEntries() until no more results are returned.
    var readEntries = function($scope) {
      console.log($scope);
        dirReader.readEntries (function(results) {
        if (!results.length) {
          textarea.value = entries.join("\n");
          saveFileButton.disabled = true; // don't allow saving of the list
          displayEntryData(chosenEntry);
        }
        else {
          results.forEach(function(item) {
            entries.push(item);
          });
          $scope.entries = entries;
          $scope.$apply();
          readEntries($scope);
          
        }
      }, errorHandler);
    };
    readEntries($scope); // Start reading dirs.
  }
}


Thanks

On Saturday, March 21, 2015 at 11:56:44 AM UTC+1, Alexandre LESAGE wrote:
Hi Sander Elias,

Sorry I have not thought about making plunker.
I would have like to return entries of loadDirEntry(theEntry) but in openModalChooseDirectory $scope is undefined. How can I pass $scope into function ?

Thank for your help.
AlexL
On Sat, Mar 21, 2015 at 8:21 AM Sander Elias <sande...@gmail.com> wrote:

Hi AlexL,

Return the entries var, and assign it to a scope var.
Something like: scope.entries = loadDirEntry(theEntry).

Does that help a bit. Building a plunk is a bit moot for this, as it only works in chrome apps ;)

Regards
Sander

--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/CPGWL1E35IY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages