summaryrefslogtreecommitdiff
path: root/media/javascript/lib/winlessmovable.htc
blob: d14cc149bb5369f0910871ba00131901570c9113 (plain)
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
<!-- ---------------------------------------------------------------------
//
//  Copyright 1999 Microsoft Corporation.  All Rights Reserved.
//
//  File:         movable.htc
//
//  Description:  This behavior allows the web author to make page elements
//                moveable via the mouse or through script. The movement can
//                be limited to a set area, or to horizontal or vertical 
//                movement only. 
//
//-------------------------------------------------------------------- -->

<PROPERTY NAME="movable"    />
<PROPERTY NAME="direction"  />
<PROPERTY NAME="snapable"   />
<PROPERTY NAME="selectable" />
    
<METHOD   NAME="moveTo"     />
<METHOD   NAME="snapToGrid" />
    
<EVENT    NAME="ondrag"           ID="drag"      />
<EVENT    NAME="ondragstart"      ID="dragstart" />
<EVENT    NAME="ondragend"        ID="dragend"   />
<EVENT    NAME="onerror"          ID="error"     />

<ATTACH   EVENT="onmouseup"       HANDLER="DoMouseUp"   />
<ATTACH   EVENT="onmousedown"     HANDLER="DoMouseDown" />
<ATTACH   EVENT="onclick"         HANDLER="DoSelect"    />
<ATTACH   EVENT="onselectstart"   HANDLER="DoSelect"    />
<ATTACH   EVENT="ondocumentready" HANDLER="SetDefaults" />


<SCRIPT LANGUAGE="jscript">

//+----------------------------------------------------------------------------
//
//  Global Variables
//
//-----------------------------------------------------------------------------

var iOffsetX;                       // On the dragstart event, this variable is
                                    //    set to track the difference between the
                                    //    mouse position and the corner of the
                                    //    element

var iOffsetY;                       // Same as iOffsetX, but for Y coordinate

var normZindex = style.zIndex;      // Tracks the regular zIndex so it can be
                                    //    restored once the dragend event occurs

var zBound = new Array              // Used for parsing the mvBoundary prop
    ('Top', 'Right',                //     into it's four component parts 
    'Bottom', 'Left');


//+----------------------------------------------------------------------------
//
//  Function:       SetDefaults
//
//  Description:    Called during the initialization of the behavior.  Sets
//                  the required settings for CSS properties, the defaults for
//                  custom CSS properties, and attaches the onpropertychange
//                  event (not done in the header to prevent firing the event
//                  during initialization).
//
//  Arguments:      none
//
//  Returns:        nothing
//
//-----------------------------------------------------------------------------

function SetDefaults()
{
    //  Required CSS properties
    style.left = offsetLeft;
    style.top = offsetTop;
    style.position = "absolute";

    //
    //  Set these properties before the individual ones are set next.  Thus,
    //  individual properties will override the container properties here.
    //
    style['mvBoundary'] = currentStyle['mv--boundary'];
    style['mvGrid'] = currentStyle['mv--grid'];

    //  Custom CSS Property Defaults
    CustomDefault('mv--boundary-left','mvBoundaryLeft',null);
    CustomDefault('mv--boundary-right','mvBoundaryRight',null);
    CustomDefault('mv--boundary-top','mvBoundaryTop',null);
    CustomDefault('mv--boundary-bottom','mvBoundaryBottom',null);
    CustomDefault('mv--grid-rows','mvGridRows',null);
    CustomDefault('mv--grid-cols','mvGridCols',null);

    //  Format the grid and boundary
    FormatGrid();
    FormatBoundary();

    //  Attach the onpropertychange event
    attachEvent("onpropertychange", DoPropChange);
}


//+----------------------------------------------------------------------------
//
//  Function:       CustomDefault
//
//  Description:    Sets the defaults for custom CSS properties and establishes
//                  a scriptable name for the property
//
//  Arguments:      sCSSName - the CSS name of the property
//                  sScriptName - the desired Scriptable name of the property
//                  sDefault - the desired default value
//
//  Returns:        nothing
//
//-----------------------------------------------------------------------------

function CustomDefault(sCSSName, sScriptName, sDefault)
{
    if (currentStyle[sCSSName] == null)
    {
        style[sCSSName] = sDefault;
    }
    else style[sCSSName] = currentStyle[sCSSName];
    
    style[sScriptName] = style[sCSSName];
}


//+----------------------------------------------------------------------------
//
//  Function:       FormatGrid
//
//  Description:    Parse the mvGrid space-delimited string to get mvGridRows
//                  and mvGridCols
//
//  Arguments:      none
//
//  Returns:        nothing
//
//-----------------------------------------------------------------------------

function FormatGrid()
{
    if (style['mvGrid'] != null)
    {
        if (style['mvGridCols'] == null)
        {
            style['mvGridCols'] = parseInt(style['mvGrid'].substring(
                0,style['mvGrid'].indexOf(" ")));
        }
    
        if (style['mvGridRows'] == null)
        {
            style['mvGridRows'] = parseInt(style['mvGrid'].substring(
                style['mvGrid'].indexOf(" ")+1,style['mvGrid'].length));
        }
    }
    
    //  Call snapToGrid to enforce new values
    snapToGrid();
}


//+----------------------------------------------------------------------------
//
//  Function:       FormatBoundary
//
//  Description:    Parse the mvBoundary space-delimited string to get 
//                  mvBoundaryTop, mvBoundaryRight, mvBoundaryBottom, and
//                  mvBoundaryLeft.
//
//  Arguments:      none
//
//  Returns:        nothing
//
//-----------------------------------------------------------------------------

function FormatBoundary()
{
    if (style['mvBoundary'] != null)
    {
        var iStart = 0;
        var iEnd = style['mvBoundary'].indexOf(" ");
                
        for (i=0; i<zBound.length; i++)
        {
            style['mvBoundary' + zBound[i]] = 
                style['mvBoundary'].substring(iStart,iEnd);
                
            if (iEnd == style['mvBoundary'].length) break;
            
            iStart = iEnd + 1;
            iEnd = style['mvBoundary'].indexOf(" ", iStart);
            if (iEnd == -1) iEnd = style['mvBoundary'].length;
        }
    }

    SetBoundary();
}


//+----------------------------------------------------------------------------
//
//  Function:       DoPropChange
//
//  Description:    Runs on the onpropertychange event and calls the necessary
//                  functions to correctly handle the property that was just
//                  changed.
//
//  Arguments:      none
//
//  Returns:        nothing
//
//-----------------------------------------------------------------------------

function DoPropChange()
{
    var propertyName = window.event.propertyName;

    //
    //  Handle CSS property changes by calling necessary functions, setting
    //  variables, and/or setting styles
    //
    if (propertyName.substring(0,5) == "style")
    {
        switch(propertyName)
        {
            case "style.zIndex"             :
                normZindex = style.zIndex;
                break;
                
            case "style.position"           :
                style.position = "absolute";
                break;
   
            case "style.mvGridRows"           :
                snapToGrid();
                break;
                
            case "style.mvGridCols"           :
                snapToGrid();
                break;
                
             case "style.mvGrid"               :
                FormatGrid();
                break;               
                
            case "style.mvBoundaryLeft"       :
                SetBoundary();
                break;

            case "style.mvBoundaryTop"        :
                SetBoundary();
                break;
                
            case "style.mvBoundaryRight"      :
                SetBoundary();
                break;
                
            case "style.mvBoundaryBottom"     :
                SetBoundary();
                break;
                               
            case "style.mvBoundary"           :
                FormatBoundary();
                break;
        }
    }
    else
    {
        //
        //  Detach the onpropertychange event to prevent it from firing while
        //  the changes are handled
        //
        detachEvent("onpropertychange", DoPropChange);
        
        switch(propertyName)
        {
            case "movable"                  :
                break;
              
            case "direction"                :
                break;
                
            case "snapable"                 :
                if (snapable == true || snapable == "true") snapToGrid();
                break;
                
            case "selectable"               :
                break;    

            default                         :
                ReturnError(propertyName + " not a valid property");
                break;
        }
        
        //  Re-attach the onpropertychange event
        attachEvent("onpropertychange", DoPropChange);
    }
}


//+----------------------------------------------------------------------------
//
//  Function:       moveTo
//                  
//  Description:    Moves the piece to the specified coordinates by calling
//                  the MoveElement() function.  
//
//  Arguments:      iNewX - Left position to move the piece to
//                  iNewY - Top position to move the piece to
//
//  Returns:        true if the movable property is set to false
//                  false if iNewX or iNewY do not contain numbers
//
//-----------------------------------------------------------------------------

function moveTo(iNewX, iNewY)
{
    if (movable == false || movable == "false") return true;

    iNewX = parseInt(iNewX);
    iNewY = parseInt(iNewY);

    if (isNaN(iNewX) && isNaN(iNewY)) return false;
    
    //  Call MoveElement to move the piece
    MoveElement(iNewX, iNewY);
}


//+----------------------------------------------------------------------------
//
//  Function:       snapToGrid
//
//  Description:    Based on the grid established with the mvGridRows and
//                  mvGridCols properties, snap the piece to the grid.
//
//  Arguments:      none
//
//  Returns:        nothing
//
//-----------------------------------------------------------------------------

function snapToGrid()
{
    //  Call MoveElement to move the piece
    MoveElement(offsetLeft, offsetTop, true);
}


//+----------------------------------------------------------------------------
//
//  Function:       SetBoundary
//
//  Description:    Move the element within the boundaries specified by the
//                  mvBoundary properties.
//
//  Arguments:      none
//
//  Returns:        nothing
//
//-----------------------------------------------------------------------------

function SetBoundary()
{
    //  Obey right boundary
    if (style.mvBoundaryRight != null
        && style.mvBoundaryRight < style.posLeft + offsetWidth)
    {
        style.left = style.mvBoundaryRight - offsetWidth;
    }
    
    //  Obey left boundary
    if (style.mvBoundaryLeft
        && style.mvBoundaryLeft > style.posLeft)
    {
        style.left = style.mvBoundaryLeft;
    }
    
    //  Obey bottom boundary
    if (style.mvBoundaryBottom
        && style.mvBoundaryBottom < style.posTop + offsetHeight)
    {
        style.top = style.mvBoundaryBottom - offsetHeight;
    }
    
    //  Obey top boundary
    if (style.mvBoundaryTop
        && style.mvBoundaryTop > style.posTop)
    {
        style.top = style.mvBoundaryTop;
    }
    
    //  If the element is snapable, call snapToGrid to snap it.
    if (snapable == true || snapable == "true") snapToGrid();
}


//+----------------------------------------------------------------------------
//
//  Function:       MoveElement
//                  
//  Description:    Moves the piece to the specified coordinates.  If any
//                  of the mvGrid or mvBoundary properties are set, they are
//                  enforced.
//
//  Arguments:      iNewX - Left position to move the piece to
//                  iNewY - Top position to move the piece to
//                  bSnapTo - called explicitly from snapToGrid()
//
//  Returns:        nothing
//
//-----------------------------------------------------------------------------

function MoveElement(iNewX, iNewY, bSnapTo)
{
    if (direction != "vertical" && iNewX != null)
    {
        //
        //  If the piece is snapable, then both the grid and the boundary
        //  (if one exists) have to be enforced
        //
        if ((snapable == true || snapable == "true" 
            || bSnapTo == true) && style.mvGridCols != null)
        {
            //  Find the closest grid
            var iSnapX = (Math.round(iNewX/style.mvGridCols)) * style.mvGridCols;

            //  If the piece is outside of the boundaries, put on a grid inside
            if (style.mvBoundaryLeft != null
                && iSnapX < style.mvBoundaryLeft)
            {
                iSnapX = (Math.ceil(style.mvBoundaryLeft/style.mvGridCols))
                    * style.mvGridCols;
            }
            else if (style.mvBoundaryRight != null
                && iSnapX > style.mvBoundaryRight - offsetWidth)
            {
                iSnapX = (Math.floor((style.mvBoundaryRight-offsetWidth)
                    /style.mvGridCols)) * style.mvGridCols;
            }
            iNewX = iSnapX;
        }
        //
        //  Otherwise, if the piece has just a boundary, then it needs to be
        //  enforced.  If the piece is outside the boundaries, put it inside
        //
        else if (style.mvBoundaryLeft != null
            && iNewX < style.mvBoundaryLeft)
        {
            iNewX = style.mvBoundaryLeft;
        }
        else if (style.mvBoundaryRight != null
            && iNewX > style.mvBoundaryRight - offsetWidth)
        {
            iNewX = style.mvBoundaryRight - offsetWidth;
        }

        //  Put the piece in it's (possibly adjusted) position
        style.left = iNewX;
    }

    if (direction != "horizontal" && iNewY != null)
    {
        //
        //  If the piece is snapable, then both the grid and the boundary
        //  (if one exists) have to be enforced
        //
        if ((snapable == true || snapable == "true" 
            || bSnapTo == true) && style.mvGridRows != null)
        {
            //  Find the closest grid
            var iSnapY = (Math.round(iNewY/style.mvGridRows)) * style.mvGridRows;

            //  If the piece is outside of the boundaries, put on a grid inside
            if (style.mvBoundaryTop != null
                && iSnapY < style.mvBoundaryTop)
            {
                iSnapY = (Math.ceil(style.mvBoundaryTop/style.mvGridRows))
                    * style.mvGridRows;
            }
            else if (style.mvBoundaryBottom != null 
                && iSnapY > style.mvBoundaryBottom - offsetHeight)
            {
                iSnapY = (Math.floor((style.mvBoundaryBottom-offsetHeight)
                    /style.mvGridRows)) * style.mvGridRows;
            }
            
            iNewY = iSnapY;
        }
        //
        //  Otherwise, if the piece has just a boundary, then it needs to be
        //  enforced.  If the piece is outside the boundaries, put it inside
        //
        else if (style.mvBoundaryTop != null
            && iNewY < style.mvBoundaryTop)
        {
            iNewY = style.mvBoundaryTop;
        }
        else if (style.mvBoundaryBottom != null
            && iNewY > style.mvBoundaryBottom - offsetHeight)
        {
            iNewY = style.mvBoundaryBottom - offsetHeight;
        }
    
        //  Put the piece in it's (possibly adjusted) position
        style.top = iNewY;
    }
}


//+----------------------------------------------------------------------------
//
//  Function:       DoMouseDown 
//
//  Description:    Begins the moving process.
//
//  Arguments:      none
//
//  Returns:        true if the movable property is set to false 
//
//-----------------------------------------------------------------------------

function DoMouseDown()
{
    //  If the piece is not movable, don't allow it to be moved
    if (movable == false || movable == "false") return true;
    
//    if (Selectable == true || Selectable == "true")
//    {
//		var sTag = window.event.srcElement.tagName.toLowerCase();
//		if (sTag == "input" || sTag == "textarea" ||
//			sTag == "button" || sTag == "a" ||
//			sTag == "select" || sTag == "object")
//		return false;
//    }
        
    //  Capture the mouse
    setCapture();
    


    //
    //  Determine the difference between the mouse click on the element and
    //  the top left corner	
    //
	iOffsetX = window.event.x - element.style.pixelLeft;
	iOffsetY = window.event.y - element.style.pixelTop;

    //  Start tracking the mousemove
	attachEvent ("onmousemove", DoMouseMove);

	dragstart.fire();
}	


//+----------------------------------------------------------------------------
//
//  Function:       DoMouseMove
//
//  Description:    Moves the element.
//
//  Arguments:      none
//
//  Returns:        nothing
//
//-----------------------------------------------------------------------------

function DoMouseMove()
{
	if (direction != "vertical") 
	{
	    //  Set position based on mouse movement
	    var iNewX = window.event.x - iOffsetX;
    
        //  Obey left boundary
	    if (style.mvBoundaryLeft != null
	        && iNewX < style.mvBoundaryLeft)
	    {
	        iNewX = style.mvBoundaryLeft;
	    }
	    
	    //  Obey right boundary
	    if (style.mvBoundaryRight != null
	        && iNewX > style.mvBoundaryRight - offsetWidth)
	    {
	        iNewX = style.mvBoundaryRight - offsetWidth;
	    }

        //  Place element
	    style.left = iNewX;
	}

	if (direction != "horizontal")
	{
	    //  Set position based on mouse movement
	    var iNewY = window.event.y - iOffsetY;
	    
	    //  Obey top boundary
	    if (style.mvBoundaryTop != null 
	        && iNewY < style.mvBoundaryTop)
	    {
	        iNewY = style.mvBoundaryTop;
	    }
	    
	    //  Obey bottom boundary
	    if (style.mvBoundaryBottom != null 
	        && iNewY > style.mvBoundaryBottom - offsetHeight)
	    {
	        iNewY = style.mvBoundaryBottom - offsetHeight;
	    }
	    
	    //  Place element
	    style.top = iNewY;
	}
	
	drag.fire();
}


//+----------------------------------------------------------------------------
//
//  Function:       DoMouseUp
//
//  Description:    Ends the moving process.
//
//  Arguments:      none
//
//  Returns:        nothing
//
//-----------------------------------------------------------------------------

function DoMouseUp()
{
    //  Return the zIndex to its previous value
	style.zIndex = normZindex;

    //  Stop tracking the onmousemove event
	detachEvent ("onmousemove", DoMouseMove);
	
	//  Release the mouse
	releaseCapture();

    //  If it's snapable, snap it now
	if (snapable == "true" || snapable == true) snapToGrid();

    //
    //  Create a click on the srcElement. If the selectable property is set
    //  to true, this will allow clicks on links, etc. to occur
    //
    window.event.srcElement.click();
	
	dragend.fire();
}


//+----------------------------------------------------------------------------
//
//  Function:       DoSelect 
//
//  Description:    If the selectable property is set to false, this function
//                  cancels clicks and drags inside of the element itself.
//
//  Arguments:      none
//
//  Returns:        false (returnValue)
//
//-----------------------------------------------------------------------------

function DoSelect()
{
    if (selectable != "true" && selectable != true)
    {
        window.event.returnValue = false;
    }
}


//+----------------------------------------------------------------------------
//
//  Function:       ReturnError
//
//  Description:    Fires the error event, along with a descriptive text
//                  message.
//
//  Arguments:      sMsg - descriptive text message
//
//  Returns:        nothing
//
//-----------------------------------------------------------------------------

function ReturnError(sMsg)
{
    var oEvent = createEventObject();
    oEvent.setAttribute("error", sMsg);
    error.fire(oEvent);
}

</SCRIPT>