-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilemanager.js
194 lines (179 loc) · 5.25 KB
/
filemanager.js
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"use strict";
const path_prefix = "translations/texts"
function FileManager(holder, navigator, account, on_file)
{
this.holder = holder
this.navigator = navigator
this.table = document.createElement('table')
this.table.className = "table table-responsive table-hover"
this.table.id = holder.id + "-fmtree"
this.table.createTBody()
this.holder.appendChild(this.table)
this.account = account
this.on_file = on_file
let fm = this
if(navigator)
$(window).bind("popstate", function (wevent) {
let event = wevent.originalEvent
if(event.state.path && event.state.json) {
$(fm.table).find('tbody').html('')
fm.updateTree(event.state.json, event.state.path)
if(event.state.file_json)
fm.on_file(event.state.file_json)
}
})
}
FileManager.prototype.addFile = function(name, type, on_click)
{
let row = this.table.insertRow()
let img = row.insertCell()
let glyph = {
"dir": "folder-open",
"file": "list-alt",
"up": "level-up"
}
img.innerHTML = "<span class='glyphicon glyphicon-" + glyph[type] + "'></span>"
img.className = "img-responsive"
img.style.width = 16
let link = row.insertCell()
link.className = "container"
let completion = row.insertCell()
let complindicator = document.createElement('div')
let complframe = document.createElement('div')
complframe.style["margin-bottom"] = 0
complframe.className = 'progress'
complindicator.className = 'progress-bar progress-bar-success'
complindicator.id = this.holder.id + "-progress-" + name.replace(/[\s\./]/g, '-')
complindicator.setAttribute('role', 'progressbar')
complindicator.setAttribute('aria-valuemin', 0)
complindicator.setAttribute('aria-valuemax', 100)
complindicator.setAttribute('aria-valuenow', 0)
complindicator.style.width = "0%"
complframe.appendChild(complindicator)
completion.appendChild(complframe)
completion.className = 'container'
let thelink = document.createElement('a')
thelink.className = ""
thelink.title = name
thelink.innerHTML = name
row.onclick = on_click
link.appendChild(thelink)
return complindicator.id
}
FileManager.prototype.track = function(path)
{
let fm = this
let parts = path.slice(path_prefix.length).split('/')
let curparts = $(this.navigator).children().toArray()
function makeLink(thepath, i)
{
let offset = path_prefix.length
for (let k=i;k >= 0;k--)
offset = thepath.indexOf('/', offset) + 1
if (offset == 0)
offset = thepath.length
let reduced = thepath.slice(0, offset)
return function()
{
fm.gotoPath(reduced)
}
}
for (let i in parts)
{
if (parts[i].length == 0)
parts[i] = 'Корень'
if (curparts.length <= i)
{
$(this.navigator).append('<li>'+parts[i]+'</li>').children().last()
.on('click', makeLink(path, i))
continue
}
if ($(curparts[i]).text() != parts[i])
$(curparts[i]).text(parts[i])
$(curparts[i]).removeClass('active')
}
$(this.navigator).children().slice(parts.length).remove()
$(this.navigator).children().last().addClass('active')
}
FileManager.prototype.updateTree = function (file_json, path)
{
let fm = this
if (path.length > 0)
{
this.track(path)
}
let offset = path.lastIndexOf("/")
if (offset >= path_prefix.length - 1)
{
let prev_path = path.slice(0,offset)
offset = path.lastIndexOf("?")
if (offset != -1)
{
prev_path = prev_path + path.slice(offset)
}
fm.addFile( "..", "up",function()
{
fm.gotoPath(prev_path)
})
}
$.each(file_json, function(i, e)
{
if (e.size >= 1024000)
{return}
let pbid = fm.addFile(e.name, e.type, function()
{
fm.gotoPath(e.path)
})
theWorker.justSend("getstatus", {id:pbid, path:e.path})
})
}
FileManager.prototype.gotoPath = function (path)
{
this.gotoAdvancedPath(path, "track")
}
FileManager.prototype.gotoAdvancedPath = function(path, tracking)
{
let fm = this
let callback
if(path.match(/^.*\.[a-zA-Z]+$/)) callback = function(json){
fm.gotoAdvancedPath(path.replace(/\/[^\/]+$/, ""), tracking)
.then(function(){
//console.log("onfile called", history)
fm.on_file(json)
if(tracking != "none")
history.replaceState({
path: path,
json: history.state.json,
file_json: json,
}, "Интерфейс переводчика", "?open=" + path)
})
}
else{
path = path.replace(/\/$/, "")
let pusher = history.pushState
if(tracking == "replace") pusher = history.replaceState
else if (tracking == "none") pusher = function (){}
callback = function(json){
//console.log("onfolder called", history)
pusher.call(history, {
path: path,
json: json,
file_json: false,
}, "Интерфейс переводчика", "?open=" + path)
$(fm.table).find('tbody').html('')
fm.updateTree(json, path)
}
}
if (!this.account)
{
this.on_file(path)
return
}
return this.account.getJSON(this.account.get_repo_suffix() + "contents/"
+ path + '?ref=' + this.account.branch)
.then(callback)
}
FileManager.prototype.gotoHome = function ()
{
this.gotoAdvancedPath(path_prefix, "replace")
}