zhuguifei
2026-03-10 58402bd5e762361363a0f7d7907153c77dbb819f
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
/**
    @name Edo.managers.ResizeManager
    @class
    @single
    @description 尺寸调节管理器   
    @example 
 
 
*/ 
Edo.managers.ResizeManager = {
    all: {},
    overlay: null,
    
    startResize: function(cfg){
        //event:事件对象
        //handler:方向
        //handlerEl:方向元素
        //minWidth,minHeight
        //target:   目标对象
        
        Edo.apply(this, cfg,{
            minWidth: 10,
            minHeight: 10,
            onresizestart: Edo.emptyFn,
            onresize: Edo.emptyFn,
            onresizecomplete: Edo.emptyFn
        });
        var drag = new Edo.util.Drag({
            onStart: this.onStart.bind(this),
            onMove: this.onMove.bind(this),
            onStop: this.onStop.bind(this)
        });
        
        //初始化拖拽需要的一些坐标属性    
        var el = this.target.el || this.target;
        this.initXY = this.event.xy;                     //鼠标的初始点击位置
        this.handlerBox = Edo.util.Dom.getBox(this.handlerEl);        //目标handler的初始位置
        this.box = this.box || Edo.util.Dom.getBox(el);
    
        drag.start(this.event);
        
    },  
    /**        
        @name Edo.managers.ResizeManager#reg
        @function 
        @description 注册尺寸调节器
        @param {Object} config 尺寸调节配置对象
<pre>
{
    target: 目标组件对象,
    transparent: false, //是否透明
    handlers: [e东,s南,w西,n北, es东南, ws西南, sn西北, en东北]//不传递为['se']
}
</pre>        
    */      
    reg: function(o){
        //target: 目标组件对象
        //transparent:是否透明
        //handlers: [e东,s南,w西,n北, es东南, ws西南, sn西北, en东北]
        //mode: 'in','out','hide'   //在内显示,在外显示,隐藏等...默认在内不显示
        
        var el = o.target.el || o.target;
        var id = el.id;
        if(!id) throw new Error('必须指定id');
        if(this.all[id]) this.unreg(o);            
        this.all[id] = o;
        o.minWidth = o.minWidth || o.target.minWidth ||10;
        o.minHeight = o.minHeight || o.target.minHeight || 10;
        
        o.els = {};
        if(!o.handlers) o.handlers = ['se'];
        for(var i=0; i<o.handlers.length; i++){
            var h = o.handlers[i];
            var s = '<div direction="'+h+'" class="e-resizer e-resizer-'+h+'"></div>';
            var d = Edo.util.Dom.append(el, s);
            d.direction = h;
            if(o.resizable !== false){
                Edo.util.Dom.on(d, 'mousedown', this.onMouseDown, o);
            }
            o.els[h] = d;
        }
        
        if(!o.transparent) Edo.util.Dom.addClass(el, 'e-resizer-over');
        if(o.cls) Edo.util.Dom.addClass(el, o.cls);
        if(o.square) Edo.util.Dom.addClass(el, 'e-resizer-square');
        
    },    
    /**        
        @name Edo.managers.ResizeManager#unreg
        @function 
        @description 注销尺寸调节器
        @param {UIComponent} 组件
    */    
    unreg: function(id){
        id = id.id || id;
        var o = this.all[id];
        if(o){             
            for(var h in o.els){
                var el = o.els[h];
                Edo.util.Dom.clearEvent(el);    
                Edo.util.Dom.removeClass(el, 'e-resizer-over');
                Edo.util.Dom.removeClass(el, 'e-resizer-square');
                Edo.util.Dom.remove(el);
            }                   
            delete this.all[o.id];                                
        }
    },
    //private
    fire: function(e, o){
        
        this.size = this.box = this.getResizeBox(this.event);
        
        e = Edo.apply(e, this);
        o = o || this.target;   
        
        if(o.el){
            o.fireEvent(e.type, e);
        }else{
            Edo.util.Dom.fireEvent(o, e.type, e);
        }
        e['on'+e.type].c(this, e);
    },
    getOverlay: function (cursor){
        var bd = document;
        if(!this.overlay){
            this.overlay = Edo.util.Dom.append(bd.body, '<div class="e-resizer-overlay"></div>');
            Edo.util.Dom.selectable(this.overlay, false);
        }
        Edo.util.Dom.setStyle(this.overlay, "cursor", cursor);        
        Edo.util.Dom.setSize(this.overlay, Edo.util.Dom.getScrollWidth(bd), Edo.util.Dom.getScrollHeight(bd));
        bd.body.appendChild(this.overlay);        
    },
    getResizeBox: function(e){                 
        //拖拽的时候,要注意,不能小于minWidth,minHeight
        var xy = e.xy;
        
        this.offsetX = this.handlerBox.right - this.initXY[0];
        this.offsetY = this.handlerBox.bottom - this.initXY[1]; //右下
        this.offsetX2 = this.initXY[0] - this.handlerBox.x;     //左上
        this.offsetY2 = this.initXY[1] - this.handlerBox.y;
        
        var px = xy[0] + this.offsetX, py = xy[1] + this.offsetY;//当前x,y鼠标位置,加入了偏移   
        var px2 = xy[0] - this.offsetX, py2 = xy[1] - this.offsetY;//当前x,y鼠标位置,加入了偏移   
                 
        
        var x = this.box.x, y = this.box.y, w = this.box.width, h = this.box.height, right = this.box.right, bottom = this.box.bottom;            
        
        var mw = this.minWidth || 0, mh = this.minHeight || 0;
        switch(this.handler){
            case "e":
                w = px - x;
                w = Math.max(mw, w);
                break;
            case "s":
                h = py - y;
                h = Math.max(mh, h);
                break;
            case "se":
                w = px - x;
                w = Math.max(mw, w);
                h = py - y;
                h = Math.max(mh, h);                    
                break;
            case "n":
                if(bottom - py2 < mh){
                    y = bottom - mh;
                }else{
                    y = py2;
                }
                h = bottom - y;
                break;
            case "w":
                if(right - px2 < mw){
                    x = right - mw;
                }else{
                    x = px2;
                }
                w = right - x;
                break;
            case "nw":
                if(bottom - py2 < mh){
                    y = bottom - mh;
                }else{
                    y = py2;
                }
                h = bottom - y;                
                if(right - px2 < mw){
                    x = right - mw;
                }else{
                    x = px2;
                }
                w = right - x;
                break;
            case "ne":
                if(bottom - py2 < mh){
                    y = bottom - mh;
                }else{
                    y = py2;
                }
                h = bottom - y;
                w = px - x;
                w = Math.max(mw, w);
                break;
           case "sw":
                if(right - px2 < mw){
                    x = right - mw;
                }else{
                    x = px2;
                }
                w = right - x;
                h = py - y;
                h = Math.max(mh, h);
                break;
        }        
        return {x: x, y: y, width: w, height: h, right: x + w, bottom: y + h}
    },
    onStart: function(drag){
        this.event = drag.event;
        var t = this.handlerEl;
        
        if(this.autoProxy !== false){        
            this.proxy = Edo.util.Dom.append(document.body, '<div class="e-resizer-proxy"></div>');        
            Edo.util.Dom.setBox(this.proxy, this.box);
        }
        
        this.getOverlay(Edo.util.Dom.getStyle(t, 'cursor'));
        
        
        this.fire({
            type: 'resizestart',
            target: this
        });
    },
    onMove: function(drag){
        this.event = drag.event;        
//        fire({
//            type: 'resize',
//            target: this
//        });
        if(this.autoProxy !== false){        
            var box = this.getResizeBox(this.event);
            Edo.util.Dom.setBox(this.proxy, box);
        }
        this.fire({
            type: 'resize',
            target: this
        });
    },
    onStop: function (drag){
        this.event = drag.event;                
    
        var size = this.getResizeBox(this.event);
        
        if(this.autoResize !== false){
            if(this.target.el){
                this.target.set('size', size);
            }else{
                Edo.util.Dom.setSize(this.target, size.width, size.height);
            }
        }
        Edo.util.Dom.remove(this.overlay);
        if(this.autoProxy !== false){
            Edo.util.Dom.remove(this.proxy);
        }
        
        this.fire({
            type: 'resizecomplete',
            target: this
        });
        this.autoResize = this.handler = this.handlerEl = this.proxy = this.initXY = this.handlerBox = this.box = this.autoProxy = null;
    },
    onMouseDown: function(e){
        if(e.button != 0) return false;
                
        var t = e.target;
        
        //得到目标DOM元素
        var el = this.target.el || this.target;        
        //得到拖拽方向
        var d = t.direction;
        
        if(d && t.parentNode === el){
//            Edo.managers.ResizeManager.startResize({
//                target: this.target,
//                event: e,
//                handler: d,
//                handlerEl: t,
//                minWidth: this.minWidth,
//                minHeight: this.minHeight
//            });
            Edo.managers.ResizeManager.startResize(Edo.applyIf({
                target: this.target,
                event: e,
                handler: d,
                handlerEl: t,
                minWidth: this.minWidth,
                minHeight: this.minHeight
            }, this));
            e.stop();
        }
    }
};