/* Copyright 2013 Florent Galland & Luc Verdier

This file is part of glark.io.

glark.io is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
at your option) any later version.

glark.io is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with glark.io.  If not, see <http://www.gnu.org/licenses/>. */
'use strict';

angular.module('glark.services')

    .factory('FileTree', function () {
        
        
        var DirectoryNode = function(name) {
            this.isDirectory = true;
            this.isFile = false;
            
            this.name = name;
            this.children = [];
        };
        
        var FileNode = function(file) {
            this.isDirectory = false;
            this.isFile = true;
            
            this.name = false.name;
            this.file = file;
        };
        
        var FileTree = function() {
            this.rootNode = new DirectoryNode('root');
        }
        
        FileTree.prototype.addFile = function(file) {
            var directories = file.basename.split('/');
            var activeNode = this.rootNode;
            angular.forEach(directories, function(directory) {
                if(!(directory in activeNode.children)) {
                    activeNode.children.push(new DirectoryNode(directory));
                }
                activeNode = activeNode[directory];
            });
            activeNode.children.push(new FileNode(file));
        };
        
        return FileTree;
    }
    
    .factory('workspace', function (editor, FileTree) {
        /* Main model of the glark.io application. Contains among other all the
         * data describing the files of the workspace, the open ones and the active
         * one. */
        var workspace = {};

        var activeFile = null;

        /* Files of the workspace. A collection of glark.services.File object. */
        workspace.files = [];

        /* A file tree representation of the files collection. */
        workspace.tree = new FileTree();
        
        /* @param file is a glark.services.File object. */
        workspace.addFile = function (file) {
            if (this.files.indexOf(file) == -1) {
                this.files.push(file);
                this.tree.addFile(file);
            }
        };
        
        /* @param file is a glark.services.File object. */
        workspace.removeFile = function (file) {
            var idx = this.files.indexOf(file);
            if (idx != -1) {
                this.files.splice(idx, 1);
                file.session.setValue('', -1);
                //this.tree.removeFile(file); /*TODO*/
                return true;
            }
            return false;
        };

        /* @return the number of files. */
        workspace.getFileCount = function () {
            return this.files.length;
        };

        /* @return the active glark.services.File file. */
        workspace.getActiveFile = function () {
            return activeFile;
        };

        /* @param file is a glark.services.File object. */
        workspace.setActiveFile = function (file) {
            this.addFile(file);
            activeFile = file;
            editor.setSession(file.session);
        };

        /* @param file is a glark.services.File object. */
        workspace.isActiveFile = function (file) {
            return file == activeFile;
        };

        return workspace;
    });

@@ -750,1145 +750,8 @@
 ')%0A%0A
-    .factory('FileTree', function () %7B%0A        %0A        %0A        var DirectoryNode = function(name) %7B%0A            this.isDirectory = true;%0A            this.isFile = false;%0A            %0A            this.name = name;%0A            this.children = %5B%5D;%0A        %7D;%0A        %0A        var FileNode = function(file) %7B%0A            this.isDirectory = false;%0A            this.isFile = true;%0A            %0A            this.name = false.name;%0A            this.file = file;%0A        %7D;%0A        %0A        var FileTree = function() %7B%0A            this.rootNode = new DirectoryNode('root');%0A        %7D%0A        %0A        FileTree.prototype.addFile = function(file) %7B%0A            var directories = file.basename.split('/');%0A            var activeNode = this.rootNode;%0A            angular.forEach(directories, function(directory) %7B%0A                if(!(directory in activeNode.children)) %7B%0A                    activeNode.children.push(new DirectoryNode(directory));%0A                %7D%0A                activeNode = activeNode%5Bdirectory%5D;%0A            %7D);%0A            activeNode.children.push(new FileNode(file));%0A        %7D;%0A        %0A        return FileTree;%0A    %7D%0A    %0A
     

Fix merge issue