-
Notifications
You must be signed in to change notification settings - Fork 1
/
core_file.js
124 lines (93 loc) · 3.36 KB
/
core_file.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
sc_require('core');
sc_require('models/file');
/*
THIS IS DEPRECATED...
*/
/*globals Greenhouse */
Greenhouse.mixin({
/*
@property
*/
rootFolder: null,
loadFileList: function(){
if(!this._listRequest) this._listRequest = SC.Request.create({type: 'GET', isJSON: YES, address: '/sproutcore/fs?action=list'});
this._listRequest.notify(this,this._listCompleted, {}).send();
},
_listCompleted: function(request, params){
var root = Greenhouse.File.create({treeItemIsExpanded: YES});
var response = this._parse_response(request.response(), root);
root.set('contents', response);
this.set('rootFolder', root);
Greenhouse.filesController.set('content', root);
Greenhouse.makeFirstResponder(Greenhouse.READY);
},
/*
wraps everything in an Greenhouse.File object
*/
_parse_response: function(content, parent){
for(var i=0; i < content.length; i+=1){
content[i] = Greenhouse.File.create(content[i]);
if(content[i].contents){
content[i].contents = this._parse_response(content[i].contents, content[i]);
}
content[i].set('parent',parent);
}
return content;
},
getFile: function(file){
if(!this._getRequest) this._getRequest = SC.Request.create({type: 'GET'});
this._getRequest.set('address', "/sproutcore/fs/%@".fmt(file.get('path')));
this._getRequest.notify(this,this._getCompleted, {file: file}).send();
},
_getCompleted: function(request, params){
var file = params.file;
file.requestComplete(request.response());
//TODO: set content type...
},
commitFile: function(file){
if(!this._postRequest) this._postRequest = SC.Request.create({type: 'POST'});
this._postRequest.set('address', "/sproutcore/fs/%@?action=overwrite".fmt(file.get('path')));
this._postRequest.notify(this,this._commitCompleted, {file: file}).send(file.get('body'));
},
_commitCompleted: function(request, params){
var file = params.file;
file.requestComplete();
},
/*
create folder
*/
createFolder: function(file){
if(!this._postRequest) this._postRequest = SC.Request.create({type: 'POST'});
this._postRequest.set('address', "/sproutcore/fs/%@?action=mkdir".fmt(file.get('path')));
this._postRequest.notify(this,this._createFolderCompleted,{file: file}).send();
},
_createFolderCompleted: function(request, params){
var file = params.file;
file.requestComplete();
},
/*
create a file
*/
createFile: function(file){
if(!this._postRequest) this._postRequest = SC.Request.create({type: 'POST'});
this._postRequest.set('address', "/sproutcore/fs/%@?action=touch".fmt(file.get('path')));
this._postRequest.notify(this,this._createFileCompleted,{file: file}).send();
},
_createFileCompleted: function(request, params){
var file = params.file;
file.requestComplete();
},
/*
destroys a file
*/
destroyFile: function(file){
if(!this._postRequest) this._postRequest = SC.Request.create({type: 'POST'});
this._postRequest.set('address', "/sproutcore/fs/%@?action=remove".fmt(file.get('path')));
this._postRequest.notify(this,this._destroyFileCompleted,{file: file}).send();
},
_destroyFileCompleted: function(request, params){
var file = params.file;
file.requestComplete();
file.destroy();
}
});