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