1 | if(jQuery) (function($){
|
---|
2 |
|
---|
3 | $.extend($.fn, {
|
---|
4 | violetGrid: function (o) {
|
---|
5 | if( !o ) var o = {};
|
---|
6 | if( o.container == undefined ) o.container = $(this);
|
---|
7 | if( o.defaultViewMode == undefined ) o.defaultViewMode = 'thumbnail';//or 'list'
|
---|
8 | if( o.directoryTreeData == undefined ) o.directoryTreeData = null;
|
---|
9 | if( o.dirIDprefix == undefined ) o.dirIDprefix = null;
|
---|
10 | if( o.host == undefined ) o.host = 'http://localhost/';
|
---|
11 |
|
---|
12 | if( o.sharemodal == undefined ) o.sharemodal = $("#box-shareto");
|
---|
13 | if( o.copymodal == undefined ) o.copymodal = $("#box-copyto");
|
---|
14 | if( o.movemodal == undefined ) o.movemodal = $("#box-moveto");
|
---|
15 |
|
---|
16 | var currentObj = {};
|
---|
17 |
|
---|
18 | var createNode = function (d) {
|
---|
19 | if( !d ) var d = {};
|
---|
20 | if( d.id == undefined ) d.id = null;
|
---|
21 | if( d.name == undefined ) d.name = null;
|
---|
22 | if( d.minetype == undefined ) d.minetype = 'directory';
|
---|
23 | if( d.parentID == undefined ) d.parentID = null;
|
---|
24 | if( d.clickEvent == undefined ) d.clickEvent = null;
|
---|
25 | if( d.customEvent == undefined ) d.customEvent = null;
|
---|
26 |
|
---|
27 | var strHTML = '<div class="vscell" rel="id:' + d.id + '">';
|
---|
28 | strHTML += '<div class="selector unselected">';
|
---|
29 | strHTML += '<div class="icon-' + d.minetype + '"></div>';
|
---|
30 | strHTML += '</div>';
|
---|
31 | strHTML += '<div class="file-name unselected">' + d.name + '</div>';
|
---|
32 | strHTML += '</div>';
|
---|
33 |
|
---|
34 | var disabledItemsList = null;
|
---|
35 |
|
---|
36 | if (d.minetype == 'directory') {
|
---|
37 | disabledItemsList = ['preview'];
|
---|
38 | }
|
---|
39 |
|
---|
40 | $(o.container).append(strHTML);
|
---|
41 |
|
---|
42 | $('div[rel="id:'+ d.id +'"]').bind('click',function(e){itemClick(this)});
|
---|
43 |
|
---|
44 | $('div[rel="id:'+ d.id +'"]').contextMenu({
|
---|
45 | menu: 'gridMenu',
|
---|
46 | disabledItems: disabledItemsList
|
---|
47 | }, function(action, el, pos) {
|
---|
48 | itemClick(el);
|
---|
49 | switch(action) {
|
---|
50 | case 'rename':
|
---|
51 | rename(el);
|
---|
52 | break;
|
---|
53 | case 'share':
|
---|
54 | showShareModal(currentObj);
|
---|
55 | break;
|
---|
56 | case 'copy':
|
---|
57 | case 'cut':
|
---|
58 | showCopyModal(currentObj, action);
|
---|
59 | break;
|
---|
60 | default:
|
---|
61 | break;
|
---|
62 | }
|
---|
63 |
|
---|
64 | });
|
---|
65 | }
|
---|
66 |
|
---|
67 | var keyboardRename = function () {
|
---|
68 | $(document).bind('keydown','keypress', function(e) {
|
---|
69 | var selectedItem = $('.selector.selected').parent();
|
---|
70 |
|
---|
71 | switch( e.keyCode ) {
|
---|
72 | case 113:
|
---|
73 | rename(selectedItem);
|
---|
74 | break;
|
---|
75 | case 27:
|
---|
76 | cancelRename(selectedItem);
|
---|
77 | break;
|
---|
78 | default:
|
---|
79 | break;
|
---|
80 | }
|
---|
81 | });
|
---|
82 | }
|
---|
83 |
|
---|
84 | var showShareModal = function (obj) {
|
---|
85 | if (obj.type == undefined || obj.type == null) obj.type = 'directory';
|
---|
86 | $(o.sharemodal).modal('show');
|
---|
87 | $(o.sharemodal).find('INPUT#txtSelectedObj').val(obj.name);
|
---|
88 | $(o.sharemodal).find('.modal-header').text('Chia sẻ ' + (obj.type == 'directory'?'thư mục ':'file ') + obj.name);
|
---|
89 | }
|
---|
90 |
|
---|
91 | var showCopyModal = function (obj, action) {
|
---|
92 | var modalTitle = action == 'copy' ? 'Sao chép ' + (obj.type == 'directory'?'thư mục ':'file ') + obj.name:'Di chuyá»n ' + (obj.type == 'directory'?'thư mục':'file') + ' ' + obj.name
|
---|
93 | var submitTitle = action == 'copy' ? 'Sao chép' : 'Di chuyá»n';
|
---|
94 | var submitIcon = action == 'copy' ? '<i class="icon-copy"></i>' : '<i class="icon-cut"></i>';
|
---|
95 |
|
---|
96 | //btn-primary
|
---|
97 | $(o.copymodal).modal('show');
|
---|
98 | $(o.copymodal).find('INPUT#txtSelectedObj').val(obj.name);
|
---|
99 | $(o.copymodal).find('.modal-header').text(modalTitle);
|
---|
100 | $(o.copymodal).find('.btn-primary').empty();
|
---|
101 | $(o.copymodal).find('.btn-primary').append(submitIcon + "\n" + submitTitle);
|
---|
102 |
|
---|
103 | var selectTree = $(o.copymodal).find('#select-destination-tree').violetTree({
|
---|
104 | container: $('#select-destination-tree'),
|
---|
105 | expandEasing: 'easeOutBounce',
|
---|
106 | collapseEasing: 'easeOutBounce',
|
---|
107 | homeDirNameDisplay: "Thư mục gá»c",
|
---|
108 | contextmenuON: false
|
---|
109 | });
|
---|
110 |
|
---|
111 | $(o.copymodal).find('.btn-primary').button().click(function() {excuteCopy(obj, selectTree, action)})
|
---|
112 |
|
---|
113 | $(o.copymodal).on('hide.bs.modal', function () {
|
---|
114 | $(o.copymodal).find('.btn-primary').unbind('click');
|
---|
115 | });
|
---|
116 | }
|
---|
117 |
|
---|
118 | var excuteCopy = function (obj, tree, action) {
|
---|
119 | var destObj = tree.getSelectedObj();
|
---|
120 |
|
---|
121 | //sendCommand
|
---|
122 | var postdata = {sourceid:obj.id,
|
---|
123 | sourcetype:obj.type,
|
---|
124 | destid: destObj.id,
|
---|
125 | desttype: destObj.type,
|
---|
126 | flag:action}
|
---|
127 |
|
---|
128 | sendCommand({
|
---|
129 | script:'ajax/privatecontent/copy',
|
---|
130 | postdata:postdata,
|
---|
131 | callbackSuccess: function (parsedData) {
|
---|
132 | if (parsedData.RESULT == true) {
|
---|
133 | //please add code here
|
---|
134 | }else return false;
|
---|
135 | }
|
---|
136 | });
|
---|
137 | }
|
---|
138 |
|
---|
139 | var itemClick = function (i) {
|
---|
140 | $(i).parent().find('.selector').removeClass('selected');
|
---|
141 | $(i).parent().find('.selector').addClass('unselected');
|
---|
142 | $(i).parent().find('.file-name').removeClass('selected');
|
---|
143 | $(i).parent().find('.file-name').addClass('unselected');
|
---|
144 |
|
---|
145 | $(i).find('.file-name').removeClass('unselected');
|
---|
146 | $(i).find('.file-name').addClass('selected');
|
---|
147 | $(i).find('.selector').removeClass('unselected');
|
---|
148 | $(i).find('.selector').addClass('selected');
|
---|
149 |
|
---|
150 | if ($(i).find('.file-name').text() != '')
|
---|
151 | currentObj.name = $(i).find('.file-name').text();
|
---|
152 |
|
---|
153 | currentObj.id = $(i).attr('rel').substring(3);
|
---|
154 | currentObj.type = $(i).find('>div>div').hasClass('icon-directory') ? 'directory':'file';
|
---|
155 | }
|
---|
156 |
|
---|
157 | var renderGrid = function (o) {
|
---|
158 | $(o.container).find ('.vscell').remove();
|
---|
159 |
|
---|
160 | var childDir = o.directoryTreeData.DIRECTORIES;
|
---|
161 | var childFile = o.directoryTreeData.FILES;
|
---|
162 | var curentDirID = o.curentParent.substring(o.dirIDprefix.length, o.curentParent.length);
|
---|
163 |
|
---|
164 | for (var i = 0 ; i < childDir.length; i++) {
|
---|
165 | if (childDir[i].parentID != curentDirID) continue;
|
---|
166 | createNode ({
|
---|
167 | id: childDir[i].id,
|
---|
168 | name: childDir[i].name,
|
---|
169 | parentID: curentDirID,
|
---|
170 | });
|
---|
171 | }
|
---|
172 |
|
---|
173 | for (var i = 0 ; i < childFile.length; i++) {
|
---|
174 | if (childFile[i].parentID != curentDirID) continue;
|
---|
175 | createNode ({
|
---|
176 | id: childFile[i].id,
|
---|
177 | name: childFile[i].name,
|
---|
178 | parentID: curentDirID,
|
---|
179 | minetype: childFile[i].minetype,
|
---|
180 | });
|
---|
181 | }
|
---|
182 |
|
---|
183 | keyboardRename();
|
---|
184 | }
|
---|
185 |
|
---|
186 | var rename = function (e) {
|
---|
187 | var nameObj = $(e).find('.file-name');
|
---|
188 | var editor = document.createElement ('INPUT');
|
---|
189 | editor.type = 'text';
|
---|
190 | editor.className = 'rename';
|
---|
191 |
|
---|
192 | $(nameObj).text('');
|
---|
193 | $(nameObj).append(editor);
|
---|
194 |
|
---|
195 | $(editor).attr('value', currentObj.name);
|
---|
196 | editor.focus();
|
---|
197 | $(editor).select();
|
---|
198 | $(editor).bind('focusout',function (e) {
|
---|
199 | cancelRename($(this).parent().parent());
|
---|
200 | });
|
---|
201 |
|
---|
202 | $(editor).bind('keypress',function (e) {
|
---|
203 | var keycode = (e.keyCode ? e.keyCode : e.which);
|
---|
204 | if(keycode == '13'){
|
---|
205 | completeRename($(this).parent());
|
---|
206 | }
|
---|
207 | else {
|
---|
208 |
|
---|
209 | }
|
---|
210 | e.stopPropagation();
|
---|
211 | });
|
---|
212 | }
|
---|
213 |
|
---|
214 | var cancelRename = function (e) {
|
---|
215 | $(e).find('INPUT.rename').remove();
|
---|
216 | $(e).find('.file-name').text(currentObj.name);
|
---|
217 | }
|
---|
218 |
|
---|
219 | var completeRename = function (e) {
|
---|
220 |
|
---|
221 | var postdata = {id:currentObj.id,
|
---|
222 | objtype:currentObj.type,
|
---|
223 | newname:$(e).find('INPUT.rename').val()}
|
---|
224 | sendCommand({
|
---|
225 | script:'ajax/privatecontent/rename',
|
---|
226 | postdata:postdata,
|
---|
227 | callbackSuccess: function (parsedData) {
|
---|
228 | if (parsedData.RESULT == true) {
|
---|
229 | currentObj.name = parsedData.UPDATED.name;
|
---|
230 | $(e).find('INPUT.rename').remove();
|
---|
231 | $(e).text(currentObj.name);
|
---|
232 | }else return false;
|
---|
233 | }
|
---|
234 | });
|
---|
235 | }
|
---|
236 |
|
---|
237 | var sendCommand = function (p) {
|
---|
238 | if( p.postdata == undefined ) p.postdata = null;
|
---|
239 | if( p.script == undefined ) p.script = o.script;
|
---|
240 | if( p.callbackSuccess == undefined ) p.callbackSuccess = null;
|
---|
241 | if( p.callbackDone == undefined ) p.callbackDone = null;
|
---|
242 | if( p.callbackFail == undefined ) p.callbackFail = null;
|
---|
243 | if( p.callbackAlways == undefined ) p.callbackAlways = null;
|
---|
244 |
|
---|
245 | if (p.script != null) {
|
---|
246 | $.post(o.host + p.script, p.postdata, function (data){
|
---|
247 | if (data) {
|
---|
248 | parseData = $.parseJSON(data);
|
---|
249 | }
|
---|
250 |
|
---|
251 | if (p.callbackSuccess != null) {
|
---|
252 | p.callbackSuccess(parseData);
|
---|
253 | }
|
---|
254 |
|
---|
255 | }).done(function() {if (p.callbackDone != null)p.callbackDone(this);}).fail(function() {if (p.callbackFail != null)p.callbackFail(this);}).always(function() {if (p.callbackAlways != null)p.callbackAlways(this);});
|
---|
256 | }
|
---|
257 | }
|
---|
258 |
|
---|
259 | this.getData = function (data) {
|
---|
260 | o.directoryTreeData = data.directoryTreeData;
|
---|
261 | o.curentParent = data.curentParent;
|
---|
262 | o.dirIDprefix = data.dirIDprefix
|
---|
263 | renderGrid(o);
|
---|
264 | }
|
---|
265 |
|
---|
266 | this.showModal = function (obj, act) {
|
---|
267 | switch( act ) {
|
---|
268 | case 'copy':
|
---|
269 | case 'cut':
|
---|
270 | showCopyModal(obj, act);
|
---|
271 | break;
|
---|
272 | case 'share':
|
---|
273 | showShareModal(obj);
|
---|
274 | break;
|
---|
275 | default:
|
---|
276 | break;
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | this.initialize = function() {
|
---|
281 | return this;
|
---|
282 | };
|
---|
283 |
|
---|
284 | return this.initialize();
|
---|
285 | }
|
---|
286 | });
|
---|
287 |
|
---|
288 | })(jQuery); |
---|