jsmind.draggable.js 13.8 KB
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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
/*
 * Released under BSD License
 * Copyright (c) 2014-2015 hizzgdev@163.com
 * 
 * Project Home:
 *   https://github.com/hizzgdev/jsmind/
 */

(function($w){
    'use strict';
    var $d = $w.document;
    var __name__ = 'jsMind';
    var jsMind = $w[__name__];
    if(!jsMind){return;}
    if(typeof jsMind.draggable != 'undefined'){return;}

    var jdom = jsMind.util.dom;
    var jcanvas = jsMind.util.canvas;

    var old_parent = "";
    var new_parent = "";
    var selectd_node = "";
    var tempArr = [];

    var clear_selection = 'getSelection' in $w ? function(){
         $w.getSelection().removeAllRanges();
    } : function(){
         $d.selection.empty();
    };

    var options = {
        line_width : 5,
        lookup_delay : 500,
        lookup_interval : 80
    };

    jsMind.draggable = function(jm){
    	//console.error(jm);
        this.jm = jm;
        this.e_canvas = null;
        this.canvas_ctx = null;
        this.shadow = null;
        this.shadow_w = 0;
        this.shadow_h = 0;
        this.active_node = null;
        this.target_node = null;
        this.target_direct = null;
        this.client_w = 0;
        this.client_h = 0;
        this.offset_x = 0;
        this.offset_y = 0;
        this.hlookup_delay = 0;
        this.hlookup_timer = 0;
        this.capture = false;
        this.moved = false;
    };

    jsMind.draggable.prototype = {
        init:function(){
            this._create_canvas();
            this._create_shadow();
            this._event_bind();
        },

        resize:function(){
            this.jm.view.e_nodes.appendChild(this.shadow);
            this.e_canvas.width=this.jm.view.size.w;
            this.e_canvas.height=this.jm.view.size.h;
        },

        _create_canvas:function(){
            var c = $d.createElement('canvas');
            this.jm.view.e_panel.appendChild(c);
            var ctx = c.getContext('2d');
            this.e_canvas = c;
            this.canvas_ctx = ctx;
        },

        _create_shadow:function(){
            var s = $d.createElement('jmnode');
            s.style.visibility = 'hidden';
            s.style.zIndex = '3';
            s.style.cursor = 'move';
            s.style.opacity= '0.7';
            this.shadow = s;
        },

        reset_shadow:function(el){
            var s = this.shadow.style;
            this.shadow.innerHTML = el.innerHTML;
            s.left = el.style.left;
            s.top = el.style.top;
            s.width = el.style.width;
            s.height = el.style.height;
            s.backgroundImage = el.style.backgroundImage;
            s.backgroundSize = el.style.backgroundSize;
            s.transform = el.style.transform;
            this.shadow_w = this.shadow.clientWidth;
            this.shadow_h = this.shadow.clientHeight;

        },

        show_shadow:function(){
            if(!this.moved){
                this.shadow.style.visibility = 'visible';
            }
        },

        hide_shadow:function(){
            this.shadow.style.visibility = 'hidden';
        },

        clear_lines:function(){
            jcanvas.clear(this.canvas_ctx, 0, 0, this.jm.view.size.w, this.jm.view.size.h);
        },

        _magnet_shadow:function(node){
            if(!!node){
                this.canvas_ctx.lineWidth = options.line_width;
                this.canvas_ctx.strokeStyle = 'rgba(0,0,0,0.3)';
                this.canvas_ctx.lineCap = 'round';
                this.clear_lines();
                jcanvas.lineto(this.canvas_ctx,
                    node.sp.x,
                    node.sp.y,
                    node.np.x,
                    node.np.y);
            }
        },

        _lookup_close_node:function(){
            var root = this.jm.get_root();
            var root_location = root.get_location();
            var root_size = root.get_size();
            var root_x = root_location.x + root_size.w/2;

            var sw = this.shadow_w;
            var sh = this.shadow_h;
            var sx = this.shadow.offsetLeft;
            var sy = this.shadow.offsetTop;

            var ns,nl;

            var direct = (sx + sw/2)>=root_x ?
                            jsMind.direction.right : jsMind.direction.left;
            var nodes = this.jm.mind.nodes;
            var node = null;
            var min_distance = Number.MAX_VALUE;
            var distance = 0;
            var closest_node = null;
            var closest_p = null;
            var shadow_p = null;
            for(var nodeid in nodes){
                var np,sp;
                node = nodes[nodeid];
                if(node.isroot || node.direction == direct){
                    if(node.id == this.active_node.id){
                        continue;
                    }
                    ns = node.get_size();
                    nl = node.get_location();
                    if(direct == jsMind.direction.right){
                        if(sx-nl.x-ns.w<=0){continue;}
                        distance = Math.abs(sx-nl.x-ns.w) + Math.abs(sy+sh/2-nl.y-ns.h/2);
                        np = {x:nl.x+ns.w-options.line_width,y:nl.y+ns.h/2};
                        sp = {x:sx+options.line_width,y:sy+sh/2};
                    }else{
                        if(nl.x-sx-sw<=0){continue;}
                        distance = Math.abs(sx+sw-nl.x) + Math.abs(sy+sh/2-nl.y-ns.h/2);
                        np = {x:nl.x+options.line_width,y:nl.y+ns.h/2};
                        sp = {x:sx+sw-options.line_width,y:sy+sh/2};
                    }
                    if(distance < min_distance){
                        closest_node = node;
                        closest_p = np;
                        shadow_p = sp;
                        min_distance = distance;
                    }
                }
            }
            var result_node = null;
            if(!!closest_node){
                result_node = {
                    node:closest_node,
                    direction:direct,
                    sp:shadow_p,
                    np:closest_p
                };
            }
            return result_node;
        },

        lookup_close_node:function(){
            var node_data = this._lookup_close_node();
            if(!!node_data){
                this._magnet_shadow(node_data);
                this.target_node = node_data.node;
                this.target_direct = node_data.direction;
            }
        },

        _event_bind:function(){
            var jd = this;
            var container = this.jm.view.container;
            jdom.add_event(container,'mousedown',function(e){
                var evt = e || event;
                jd.dragstart.call(jd,evt);
            });
            jdom.add_event(container,'mousemove',function(e){
                var evt = e || event;
                jd.drag.call(jd,evt);
            });
            jdom.add_event(container,'mouseup',function(e){
                var evt = e || event;
                jd.dragend.call(jd,evt);
            });
            jdom.add_event(container,'touchstart',function(e){
                var evt = e || event;
                jd.dragstart.call(jd,evt);
            });
            jdom.add_event(container,'touchmove',function(e){
                var evt = e || event;
                jd.drag.call(jd,evt);
            });
            jdom.add_event(container,'touchend',function(e){
                var evt = e || event;
                jd.dragend.call(jd,evt);
            });
        },

        dragstart:function(e){
            if(!this.jm.get_editable()){return;}
            if(this.capture){return;}
            this.active_node = null;

            var jview = this.jm.view;
            var el = e.target || event.srcElement;
            el =  getJmnode(el);
            if(el ==null){
            	return;
            }
            if(this.jm.get_selected_node() && this.jm.get_selected_node().parent != null){
            	 old_parent = this.jm.get_selected_node().parent.id;
            }
            selectd_node = this.jm.get_selected_node().id;
            if(el.tagName.toLowerCase() != 'jmnode'){return;}
            var nodeid = jview.get_binded_nodeid(el);
            if(!!nodeid){
                var node = this.jm.get_node(nodeid);
                if(!node.isroot){
                    this.reset_shadow(el);
                    this.active_node = node;
                    this.offset_x = (e.clientX || e.touches[0].clientX) - el.offsetLeft;
                    this.offset_y = (e.clientY || e.touches[0].clientY) - el.offsetTop;
                    this.client_hw = Math.floor(el.clientWidth/2);
                    this.client_hh = Math.floor(el.clientHeight/2);
                    if(this.hlookup_delay != 0){
                        $w.clearTimeout(this.hlookup_delay);
                    }
                    if(this.hlookup_timer != 0){
                        $w.clearInterval(this.hlookup_timer);
                    }
                    var jd = this;
                    this.hlookup_delay = $w.setTimeout(function(){
                        jd.hlookup_delay = 0;
                        jd.hlookup_timer = $w.setInterval(function(){
                            jd.lookup_close_node.call(jd);
                        },options.lookup_interval);
                    },options.lookup_delay);
                    this.capture = true;
                }
            }
        },

        drag:function(e){
            if(!this.jm.get_editable()){return;}
            if(this.capture){
                e.preventDefault();
                this.show_shadow();
                this.moved = true;
                clear_selection();
                var px = (e.clientX || e.touches[0].clientX) - this.offset_x;
                var py = (e.clientY || e.touches[0].clientY) - this.offset_y;
                var cx = px + this.client_hw;
                var cy = py + this.client_hh;
                this.shadow.style.left = px + 'px';
                this.shadow.style.top = py + 'px';
                clear_selection();
            }
        },

        dragend:function(e){
            if(!this.jm.get_editable()){return;}
            if(this.capture){
                if(this.hlookup_delay != 0){
                    $w.clearTimeout(this.hlookup_delay);
                    this.hlookup_delay = 0;
                    this.clear_lines();
                }
                if(this.hlookup_timer != 0){
                    $w.clearInterval(this.hlookup_timer);
                    this.hlookup_timer = 0;
                    this.clear_lines();
                }
                if(this.moved){
                    var src_node = this.active_node;
                    var target_node = this.target_node;
                    if(target_node != null){
                    	 new_parent = target_node.id;
                    }
                    var target_direct = this.target_direct;
                    this.move_node(src_node,target_node,target_direct);
                }
                this.hide_shadow();
            }
            this.moved = false;
            this.capture = false;
            //保存当前图
            findAllChildrenByNode(this.jm.get_root());
            //抛向全局的函数,参数
            saveDragendMind(tempArr,old_parent,new_parent,selectd_node);
            // saveContacterMind();
            //置空临时的数据
            old_parent = "";
            new_parent = "";
            selectd_node = "";
            tempArr = [];
        },
       

        move_node:function(src_node,target_node,target_direct){
            var shadow_h = this.shadow.offsetTop;
            if(!!target_node && !!src_node && !jsMind.node.inherited(src_node, target_node)){
                // lookup before_node
                var sibling_nodes = target_node.children;
                var sc = sibling_nodes.length;
                var node = null;
                var delta_y = Number.MAX_VALUE;
                var node_before = null;
                var beforeid = '_last_';
                while(sc--){
                    node = sibling_nodes[sc];
                    if(node.direction == target_direct && node.id != src_node.id){
                        var dy = node.get_location().y - shadow_h;
                        if(dy > 0 && dy < delta_y){
                            delta_y = dy;
                            node_before = node;
                            beforeid = '_first_';
                        }
                    }
                }
                if(!!node_before){beforeid = node_before.id;}
                this.jm.move_node(src_node.id, beforeid, target_node.id, target_direct);
            }
            this.active_node = null;
            this.target_node = null;
            this.target_direct = null;
        },

        jm_event_handle:function(type,data){
            if(type === jsMind.event_type.resize){
                this.resize();
            }
        }
    };
    //添加组和数组的方法
    function  findAllChildrenByNode(node){
        var id;
        var parentid;
        var obj;
        var direction;
        var children = node.children;
        var length = children.length;
        if(length>0){
            for(var i = 0 ;i < length;i++){
                id = children[i].id;
                parentid = children[i].parent.id;
                direction = children[i].direction;
                obj = {"id":id,"parentid":parentid,"direction":direction};
                tempArr.push(obj);
                findAllChildrenByNode(children[i]);
            }
        }
    }
    
    function getJmnode(el){
    	if(el.tagName != null){
	    		if(el.tagName.toLowerCase() == 'jmnode'){
	    			return el;
    			}else{
	    			return getJmnode(el.parentNode);
    			}
    		}
    }

    var draggable_plugin = new jsMind.plugin('draggable',function(jm){
        var jd = new jsMind.draggable(jm);
        jd.init();
        jm.add_event_listener(function(type,data){
            jd.jm_event_handle.call(jd,type,data);
        });
    });

    jsMind.register_plugin(draggable_plugin);
})(window);