1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* DreemGL is a collaboration between Teeming Society & Samsung Electronics, sponsored by Samsung and others.
   Copyright 2015-2016 Teeming Society. Licensed under the Apache License, Version 2.0 (the "License"); You may not use this file except in compliance with the License.
   You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing,
   software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and limitations under the License.*/

//Pure JS based composition
define.class('$server/composition', function(require, $server$, fileio, dataset, $ui$, screen, view, splitcontainer, treeview, label, $widgets$, docviewer, jsviewer){
// internal, Inline documentation and example code viewer

        define.class(this, 'fileio', function($server$,fileio){
                var path = require('path')
                var fs = require('fs')
                this.name = 'fileio'

                function readRecurDir(base, inname, ignoreset){
                        var local = path.join(base, inname)
                        var dir = fs.readdirSync(local)
                        var out = {name:inname, collapsed:1, children:[]}
                        for(var i = 0; i < dir.length;i++){
                                var name = dir[i]
                                var mypath = path.join(local, name)
                                if(ignoreset){
                                        for(var j = 0; j < ignoreset.length; j++){
                                                var item = ignoreset[j]
                                                if(typeof item === 'string'){
                                                        if(mypath.indexOf(item) !== -1)  break
                                                }
                                                else{
                                                        if(mypath.match(item)) break
                                                }
                                        }
                                        if(j < ignoreset.length) continue
                                }
                                var stat = fs.statSync(mypath)
                                if(stat.isDirectory()){
                                        out.children.push(readRecurDir(local, name, ignoreset))
                                }
                                else{
                                        out.children.push({name:name, size:stat.size})
                                }
                        }
                        return out
                }

                // recursively read all directories starting from a base path
                this.readAllPaths = function(ignoreset                 // files and directories to ignore while recursively expanding

                ){
                        // lets read all paths.
                        // lets read the directory and return it
                        var root = {collapsed:0, children:[]}

                        if(ignoreset) ignoreset = ignoreset.map(function(value){
                                if(value.indexOf('@') == 0) return new RegExp(value.slice(1))
                                return value
                        })

                        for(var key in define.paths){
                                if(ignoreset.indexOf(key) !== -1) continue
                                var ret = readRecurDir(define.expandVariables(define['$'+key]), '', ignoreset)
                                ret.name = key
                                root.children.push(ret)
                        }
                        return root
                }
        })

        this.render = function(){
                return [
                this.fileio(),
                screen({
                        init:function(){
                                // lets load the entire directory structure
                                this.rpc.fileio.readAllPaths(['resources','server.js','resources','cache','@/\\.','.git', '.gitignore']).then(function(result){
                                        var filetree = this.find('filetree')
                                        var tree = result.value
                                        tree.name = 'Documentation'
                                        tree.collapsed = false
                                        // lets make a dataset
                                        this.model = filetree.dataset = dataset(tree)
                                }.bind(this))

                        }},
                        splitcontainer({bgcolor: "black", flex:1}
                                ,view({flexdirection:"column", padding: 0,flex: 0.2}
                                        ,view({alignitems:"center", bgcolor:"#343434", flexdirection:"row" ,padding: 14},
                                                label({text:"DreemGL", fgcolor:"white", bgcolor:NaN, fontsize: 35 })
                                        )
                                        ,treeview({
                                                postLayout:function(){
                                                },
                                                init:function(){
                                                        var dataset = this.screen.model
                                                        if(dataset) this.dataset = dataset
                                                },
                                                name: 'filetree',
                                                flex: 1,
                                                select: function(sel){
                                                        if(sel.type === 'setter') debugger
                                                        // we have to grab the last path set and concatenate a path
                                                        var path = ''
                                                        for(var i = sel.path.length - 1; i >= 1; i--){
                                                                path = sel.path[i].name + (path !== ''? '/' + path: '')
                                                        }
                                                        this.screen.locationhash = {path:'$root/' + path};
                                                }
                                        })
                                )
                                ,docviewer({flex:1,
                                        classconstr: Config({persist:true}),
                                        init:function(){
                                                this.screen.locationhash = function(event){
                                                //        debugger
                                                        if(event.value.path) require.async(event.value.path).then(function(module){
                                                                this.classconstr = module
                                                        }.bind(this))
                                                }.bind(this)
                                                //this.screen.locationhash = this.screen.locationhash
                                        },
                                        minsize:vec2(400,400),
                                        overflow:'scroll'
                                //        overflow:'scroll'
                                })
                        )
                )
        ]}
})