[9] | 1 | if(jQuery) (function($){
|
---|
| 2 |
|
---|
| 3 | $.extend($.fn, {
|
---|
| 4 | violetTree: function(o) {
|
---|
| 5 | if( !o ) var o = {};
|
---|
| 6 | if( o.user == undefined ) o.user = null;
|
---|
| 7 | if( o.homeDirNameDisplay == undefined ) o.homeDirNameDisplay = 'Home';
|
---|
[11] | 8 | if( o.host == undefined ) o.host = 'http://localhost/';
|
---|
| 9 | if( o.script == undefined ) o.script = 'ajax/privatecontent/getcontent';
|
---|
[17] | 10 | if( o.container == undefined ) o.container = null;
|
---|
[9] | 11 | if( o.dirIDprefix == undefined ) o.dirIDprefix = 'vsdir_';
|
---|
| 12 |
|
---|
| 13 | if( o.expandSpeed == undefined ) o.expandSpeed= 500;
|
---|
| 14 | if( o.collapseSpeed == undefined ) o.collapseSpeed= 500;
|
---|
| 15 | if( o.expandEasing == undefined ) o.expandEasing = null;
|
---|
| 16 | if( o.collapseEasing == undefined ) o.collapseEasing = null;
|
---|
[42] | 17 | if( o.me == undefined ) o.me = this;
|
---|
[9] | 18 |
|
---|
| 19 | if( o.directoryTreeData == undefined ) o.directoryTreeData = null;
|
---|
| 20 | if( o.grid == undefined ) o.grid = null;
|
---|
[17] | 21 | if( o.contextmenuON == undefined ) o.contextmenuON = true;
|
---|
[14] | 22 |
|
---|
[16] | 23 | var currentObj = {};
|
---|
[17] | 24 | if ( currentObj.type == undefined ) currentObj.type = 'directory';
|
---|
[16] | 25 |
|
---|
[9] | 26 | // PRIVATE methods
|
---|
| 27 | var sendCommand = function (p) {
|
---|
| 28 | if( p.postdata == undefined ) p.postdata = null;
|
---|
[11] | 29 | if( p.script == undefined ) p.script = o.script;
|
---|
[9] | 30 | if( p.callbackSuccess == undefined ) p.callbackSuccess = null;
|
---|
| 31 | if( p.callbackDone == undefined ) p.callbackDone = null;
|
---|
| 32 | if( p.callbackFail == undefined ) p.callbackFail = null;
|
---|
| 33 | if( p.callbackAlways == undefined ) p.callbackAlways = null;
|
---|
| 34 |
|
---|
[11] | 35 | if (p.script != null) {
|
---|
| 36 | $.post(o.host + p.script, p.postdata, function (data){
|
---|
[9] | 37 | if (data) {
|
---|
| 38 | parseData = $.parseJSON(data);
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | if (p.callbackSuccess != null) {
|
---|
| 42 | p.callbackSuccess(parseData);
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | }).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);});
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | var loadTree = function () {
|
---|
| 50 | sendCommand ({postdata:null,callbackSuccess:renderTree});
|
---|
| 51 | };
|
---|
| 52 |
|
---|
| 53 | var renderTree = function (parseData) {
|
---|
[17] | 54 | $(o.container).find ('.vstree').remove();
|
---|
| 55 |
|
---|
[9] | 56 | o.directoryTreeData = parseData;
|
---|
| 57 | var directoryData = parseData.DIRECTORIES;
|
---|
| 58 |
|
---|
| 59 | var homeNode = createNode({
|
---|
| 60 | id:0,
|
---|
| 61 | name:o.homeDirNameDisplay,
|
---|
| 62 | });
|
---|
[17] | 63 |
|
---|
[9] | 64 | selectDir($(homeNode).find('> A'));
|
---|
| 65 | if (directoryData != null) {
|
---|
| 66 | for (var i = 0; i < directoryData.length ; i++) {
|
---|
| 67 | var node = createNode ({
|
---|
| 68 | id: directoryData[i].id,
|
---|
| 69 | name: directoryData[i].name,
|
---|
[17] | 70 | currentNode: $(o.container).find('#' + o.dirIDprefix + directoryData[i].parentID).find('> A'),
|
---|
[9] | 71 | hidden: (directoryData[i].parentID > 0) ? true : false
|
---|
| 72 | })
|
---|
| 73 | };
|
---|
| 74 | }
|
---|
| 75 | };
|
---|
| 76 |
|
---|
| 77 | //Create a node of Tree
|
---|
| 78 | var createNode = function (d) {
|
---|
| 79 | if( !d ) var d = {};
|
---|
| 80 | if( d.id == undefined ) d.id = null;
|
---|
| 81 | if( d.name == undefined ) d.name = null;
|
---|
[17] | 82 | if( d.currentNode == undefined ) d.currentNode = null;
|
---|
[9] | 83 | if( d.hidden == undefined ) d.hidden = true;
|
---|
| 84 | if( d.clickEvent == undefined ) d.clickEvent = openDir;
|
---|
| 85 | if( d.customEvent == undefined ) d.customEvent = null; //customEvent.eventName, customEvent.eventTrigger
|
---|
| 86 | if( d.addToJSONData == undefined ) d.addToJSONData = false;
|
---|
| 87 |
|
---|
[16] | 88 | var disabledItemsList = null;
|
---|
| 89 | if (d.name == o.homeDirNameDisplay)
|
---|
[18] | 90 | disabledItemsList = ['rename','copy','cut','delete'];
|
---|
[16] | 91 |
|
---|
[17] | 92 | if (d.currentNode != null) {
|
---|
[9] | 93 | var strHTML = '<ul class="vstree"><li id="' + o.dirIDprefix + d.id + '" class="directory collapsed"><a href="#" rel="' + d.name + '">' + d.name + '</a></li></ul>';
|
---|
| 94 |
|
---|
[17] | 95 | $(d.currentNode).parent().append(strHTML);
|
---|
[9] | 96 | if (d.hidden == true)
|
---|
[17] | 97 | $(o.container).find('#' + o.dirIDprefix + d.id).parent().css('display','none');
|
---|
[9] | 98 |
|
---|
| 99 | }else if (d.id == 0){
|
---|
| 100 | var strHTML = '<ul class="vstree"><li id="' + o.dirIDprefix + d.id + '" class="home"><a href="#" rel="' + d.name + '">' + d.name + '</a></li></ul>';
|
---|
| 101 | $(o.container).append(strHTML);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | //bind new node to data
|
---|
| 105 | if (d.addToJSONData == true) {
|
---|
| 106 | //o.directoryTreeData.DIRECTORIES.length
|
---|
| 107 | var newdir = {};
|
---|
| 108 | newdir.id = d.id;
|
---|
| 109 | newdir.name = d.name;
|
---|
[42] | 110 |
|
---|
| 111 | console.log(d.currentNode);
|
---|
| 112 |
|
---|
[17] | 113 | newdir.parentID = $(o.container).find(d.currentNode).parent().attr('id').substring(o.dirIDprefix.length, $(d.currentNode).parent().attr('id').length);
|
---|
[9] | 114 | o.directoryTreeData.DIRECTORIES.push(newdir);
|
---|
| 115 | sendtoGrid();
|
---|
| 116 | }
|
---|
[14] | 117 |
|
---|
[9] | 118 | //bind event on new node
|
---|
[17] | 119 | $(o.container).find('#' + o.dirIDprefix + d.id).find('a').bind("click", function(e){d.clickEvent(this);return false;});
|
---|
[14] | 120 |
|
---|
[17] | 121 | if (o.contextmenuON) {
|
---|
| 122 | $(o.container).find('#' + o.dirIDprefix + d.id).find('a').contextMenu({
|
---|
| 123 | menu: 'treeMenu',
|
---|
| 124 | disabledItems: disabledItemsList
|
---|
| 125 | }, function(action, el, pos) {
|
---|
| 126 | selectDir(el);
|
---|
| 127 | switch(action) {
|
---|
| 128 | case 'rename':
|
---|
| 129 | rename(el);
|
---|
| 130 | break;
|
---|
[42] | 131 | case 'newfolder':
|
---|
[17] | 132 | case 'share':
|
---|
| 133 | case 'copy':
|
---|
| 134 | case 'cut':
|
---|
| 135 | o.grid.showModal(currentObj, action);
|
---|
| 136 | break;
|
---|
[18] | 137 | case 'delete':
|
---|
| 138 | break;
|
---|
[17] | 139 | default:
|
---|
| 140 | break;
|
---|
| 141 | }
|
---|
| 142 | });
|
---|
| 143 | }
|
---|
[14] | 144 |
|
---|
[9] | 145 | if (d.customEvent != null)
|
---|
[17] | 146 | $(o.container).find('#' + o.dirIDprefix + d.id).find('a').bind(d.customEvent.eventName, function(e){d.customEvent.eventTrigger(this)});
|
---|
[9] | 147 |
|
---|
[17] | 148 | return $(o.container).find('#' + o.dirIDprefix + d.id);
|
---|
[9] | 149 | }
|
---|
| 150 | //END - Create a node of Tree
|
---|
[42] | 151 |
|
---|
[9] | 152 | var deleteNode = function (parsedData) {
|
---|
| 153 | if (parsedData.isSuccess == false) return false;
|
---|
| 154 | if (!$('#' + o.dirIDprefix + parsedData.id).hasClass('home'))
|
---|
| 155 | $('#' + o.dirIDprefix + parsedData.id).parent().remove();
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | var isHome = function (n) {
|
---|
| 159 | return $(n).parent().hasClass('home');
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | var countNodeChild = function (n) {
|
---|
| 163 | var parent = $(n).parent();
|
---|
| 164 | return $(parent).find('UL').size();
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | var openDir = function (o) {
|
---|
| 168 | if( $(o).parent().hasClass('collapsed') ) {
|
---|
| 169 | $(o).parent().find('> UL:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing });
|
---|
| 170 | $(o).parent().removeClass('collapsed').addClass('expanded');
|
---|
| 171 | }
|
---|
| 172 | else if( $(o).parent().hasClass('expanded') ) {
|
---|
| 173 | $(o).parent().find('> UL').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing });
|
---|
| 174 | $(o).parent().removeClass('expanded').addClass('collapsed');
|
---|
| 175 | }
|
---|
| 176 | selectDir(o);
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | var selectDir = function (c) {
|
---|
| 180 | $('.currentDir').removeClass('currentDir');
|
---|
| 181 | $(c).addClass('currentDir');
|
---|
[16] | 182 |
|
---|
| 183 | var cid = $(c).parent().attr('id');
|
---|
| 184 | var pid = cid.substring(o.dirIDprefix.length, cid.length);
|
---|
| 185 |
|
---|
| 186 | currentObj.id = pid;
|
---|
| 187 | currentObj.name = $(c).attr('rel');
|
---|
| 188 |
|
---|
| 189 | keyboardRename ();
|
---|
| 190 |
|
---|
[9] | 191 | sendtoGrid();
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | var sendtoGrid = function () {
|
---|
[17] | 195 | if (o.grid == null) return false;
|
---|
| 196 |
|
---|
[42] | 197 | o.grid.setTree(o.me);
|
---|
| 198 | o.grid.setData({
|
---|
[9] | 199 | directoryTreeData: o.directoryTreeData,
|
---|
| 200 | curentParent:$(o.container).find('.currentDir').parent().attr('id'),
|
---|
| 201 | dirIDprefix: o.dirIDprefix
|
---|
| 202 | });
|
---|
| 203 | }
|
---|
| 204 |
|
---|
[16] | 205 | var rename = function (o) {
|
---|
| 206 | var editor = document.createElement ('INPUT');
|
---|
| 207 | editor.type = 'text';
|
---|
| 208 | editor.className = 'rename';
|
---|
| 209 |
|
---|
| 210 | $(o).text('');
|
---|
| 211 | $(o).append(editor);
|
---|
| 212 |
|
---|
| 213 | $(editor).attr('value', currentObj.name);
|
---|
| 214 | editor.focus();
|
---|
| 215 | $(editor).select();
|
---|
| 216 |
|
---|
| 217 | $(editor).bind('focusout',function (e) {
|
---|
| 218 | cancelRename($(o));
|
---|
| 219 | });
|
---|
| 220 |
|
---|
| 221 | $(editor).bind('keypress',function (e) {
|
---|
| 222 | var keycode = (e.keyCode ? e.keyCode : e.which);
|
---|
| 223 | if(keycode == '13'){
|
---|
| 224 | completeRename($(o));
|
---|
| 225 | }
|
---|
| 226 | else {
|
---|
| 227 |
|
---|
| 228 | }
|
---|
| 229 | e.stopPropagation();
|
---|
| 230 | });
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | var cancelRename = function (o) {
|
---|
| 234 | $(o).find('>INPUT').remove();
|
---|
| 235 | $(o).text(currentObj.name);
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | var completeRename = function (o) {
|
---|
| 239 | var postdata = {id:currentObj.id,
|
---|
| 240 | objtype:'directory',
|
---|
| 241 | newname:$(o).find('INPUT.rename').val()}
|
---|
| 242 |
|
---|
| 243 | sendCommand({
|
---|
| 244 | script:'ajax/privatecontent/rename',
|
---|
| 245 | postdata:postdata,
|
---|
| 246 | callbackSuccess: function (parsedData) {
|
---|
| 247 | if (parsedData.RESULT == true) {
|
---|
| 248 | currentObj.name = parsedData.UPDATED.name;
|
---|
| 249 | $(o).find('INPUT.rename').remove();
|
---|
| 250 | $(o).text(currentObj.name);
|
---|
| 251 | }else return false;
|
---|
| 252 | }
|
---|
| 253 | });
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | var keyboardRename = function () {
|
---|
| 257 | $(document).bind('keydown','keypress', function(e) {
|
---|
| 258 | var selectedItem = $('.currentDir');
|
---|
| 259 |
|
---|
| 260 | switch( e.keyCode ) {
|
---|
| 261 | case 113:
|
---|
| 262 | rename(selectedItem);
|
---|
| 263 | break;
|
---|
| 264 | case 27:
|
---|
| 265 | cancelRename(selectedItem);
|
---|
| 266 | break;
|
---|
| 267 | default:
|
---|
| 268 | break;
|
---|
| 269 | }
|
---|
| 270 | });
|
---|
| 271 | }
|
---|
| 272 |
|
---|
[9] | 273 | loadTree();
|
---|
| 274 | // END - PRIVATE methods
|
---|
| 275 |
|
---|
| 276 |
|
---|
| 277 | this.expandAll = function() {
|
---|
| 278 | $(o.container).find('UL:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing });
|
---|
| 279 | };
|
---|
| 280 |
|
---|
| 281 | this.getCurrentDirName = function() {
|
---|
| 282 | return $(o.container).find('.currentDir').attr('rel');
|
---|
| 283 | };
|
---|
| 284 |
|
---|
| 285 | this.getCurrentDir = function() {
|
---|
| 286 | return $(o.container).find('.currentDir');
|
---|
| 287 | };
|
---|
| 288 |
|
---|
[42] | 289 | this.createDir = function(data) {
|
---|
| 290 | var cNode = $('#' + o.dirIDprefix + data.parentID).find('> A');
|
---|
| 291 | createNode({
|
---|
| 292 | id: data.id,
|
---|
| 293 | name: data.name,
|
---|
| 294 | currentNode: cNode,
|
---|
| 295 | hidden: false,
|
---|
| 296 | addToJSONData:true});
|
---|
[9] | 297 | }
|
---|
| 298 |
|
---|
| 299 | this.deleteDir = function (c) {
|
---|
[18] | 300 | if (isHome(c)) return;
|
---|
[9] | 301 | var childExisted = (countNodeChild(c) > 0) ? true:false;
|
---|
[18] | 302 |
|
---|
| 303 | $.confirm ({
|
---|
| 304 | text: 'Bạn có muá»n xóa thư mục <span style="font-weight:bold">' + currentObj.name + "</span> khÃŽng?",
|
---|
| 305 | title: "Xác nháºn xóa!",
|
---|
| 306 | confirm: function(button) {
|
---|
| 307 | var cid = $(c).parent().attr('id');
|
---|
| 308 | var pid = cid.substring(o.dirIDprefix.length, cid.length);
|
---|
| 309 | var postdata = {id:pid,delallchild:true}
|
---|
| 310 | sendCommand({
|
---|
| 311 | script:'ajax/privatecontent/deletedir',
|
---|
| 312 | postdata:postdata,
|
---|
| 313 | callbackSuccess: function (parsedData) {deleteNode(parsedData)}
|
---|
| 314 | });
|
---|
| 315 | },
|
---|
| 316 | cancel: function(button) {
|
---|
| 317 | // do something
|
---|
| 318 | },
|
---|
| 319 | confirmButton: "Äá»ng Ü xóa",
|
---|
| 320 | cancelButton: "KhÃŽng",
|
---|
| 321 | post: false
|
---|
| 322 | });
|
---|
[9] | 323 | }
|
---|
[42] | 324 |
|
---|
| 325 | this.upload = function () {
|
---|
| 326 | $('#box-upload').modal('show');
|
---|
| 327 |
|
---|
| 328 | $("#fileuploader").uploadFile({
|
---|
| 329 | url: o.host + 'ajax/privatecontent/upload',
|
---|
| 330 | fileName:"myfile",
|
---|
| 331 | multiple:true,
|
---|
| 332 | maxFileCount: 5,
|
---|
| 333 | dragDropStr: "<span><b>Kéo thả các file và o Äây!</b></span>",
|
---|
| 334 | uploadErrorStr:"Tải lên có lá»i!"
|
---|
| 335 | });
|
---|
| 336 |
|
---|
| 337 | $('#box-upload').on('hide.bs.modal', function () {
|
---|
| 338 | $('#box-upload').find('.modal-body>.row>.col-xs-12>').remove();
|
---|
| 339 | $('#box-upload').find('.modal-body>.row>.col-xs-12').append('<div id="fileuploader">Chá»n file:</div>');
|
---|
| 340 | });
|
---|
| 341 | }
|
---|
[9] | 342 |
|
---|
| 343 | this.copy = function () {
|
---|
[42] | 344 |
|
---|
[9] | 345 | }
|
---|
| 346 |
|
---|
| 347 | this.paste = function () {
|
---|
[42] | 348 |
|
---|
[9] | 349 | }
|
---|
| 350 |
|
---|
| 351 | this.initialize = function() {
|
---|
| 352 | return this;
|
---|
| 353 | };
|
---|
[17] | 354 |
|
---|
| 355 | this.getSelectedObj = function () {
|
---|
| 356 | return currentObj;
|
---|
| 357 | }
|
---|
[9] | 358 |
|
---|
| 359 | return this.initialize();
|
---|
| 360 | }
|
---|
| 361 | });
|
---|
| 362 |
|
---|
| 363 | })(jQuery); |
---|