IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JavaScript Discussion :

utilisation tool man drag


Sujet :

JavaScript

  1. #1
    Membre actif
    Profil pro
    Inscrit en
    Avril 2005
    Messages
    818
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France, Drôme (Rhône Alpes)

    Informations forums :
    Inscription : Avril 2005
    Messages : 818
    Points : 288
    Points
    288
    Par défaut utilisation tool man drag
    Bonjour,

    J'utilise la fonction de drag de tool man.
    J'ai plusieurs div draggables.
    Voici comment je leur affecte la possibilitée d'être draggé dans une zone précise (zone de contrainte):
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    for(j=1;j<=nbDiv;j++)
    {
     var drag = ToolMan.drag();
     var coordinates = ToolMan.coordinates()
     var box = document.getElementById('id_div'+j);
    
     //On rend le div draggable(box = div a draggé, et document.getElementById("handle"+j) = zone a cliquer pour dragger l'objet
     group = drag.createSimpleGroup(box,document.getElementById("handle"+j));
    
     group.addTransform(function(coordinate,dragEvent)
     {
      var xhautgauche=getOffsetPosition('OrangeEditeur', 'Left')-26;
      var yhautgauche=getOffsetPosition('OrangeEditeur', 'Top');
      var xbasdroite=document.getElementById("OrangeEditeur").offsetWidth+xhautgauche-box.offsetWidth+25;
      var ybasdroite=document.getElementById("OrangeEditeur").offsetHeight+yhautgauche-box.offsetHeight-2;
    			
      //Coin haut gauche de contrainte
      var origin = coordinates.create(xhautgauche,yhautgauche);
      //Coin Bas droite de contrainte
      var origin2=coordinates.create(xbasdroite,ybasdroite);
    			
      return coordinate.constrainTo(origin, origin2);
    })
    }
    Le problème, c'est qu'ils sont tous contraints par rapport à la taille du dernier div...(Voir le code qui est en rouge)

    Comment faire pour que les coordonnées du point en bas à droite corresponde à la taille du div dragué et non à celles du dernier div crée?

    Si vous voulez je peux vous mettre le code des fonctions de drag...

  2. #2
    Membre actif
    Profil pro
    Inscrit en
    Avril 2005
    Messages
    818
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France, Drôme (Rhône Alpes)

    Informations forums :
    Inscription : Avril 2005
    Messages : 818
    Points : 288
    Points
    288
    Par défaut
    Comment récupérer l'id du div qui est draggé en temps réel?


    Si ca peut aider, je met le code source des fonctions de drag:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    /* Copyright (c) 2005 Tim Taylor Consulting (see LICENSE.txt) */
     
    /* FIXME: assumes position styles are specified in 'px' */
     
    ToolMan._coordinatesFactory = {
     
    	create : function(x, y) {
    		// FIXME: Safari won't parse 'throw' and aborts trying to do anything with this file
    		//if (isNaN(x) || isNaN(y)) throw "invalid x,y: " + x + "," + y
    		return new _ToolManCoordinate(this, x, y)
    	},
     
    	origin : function() {
    		return this.create(0, 0)
    	},
     
    	/*
    	 * FIXME: Safari 1.2, returns (0,0) on absolutely positioned elements
    	 */
    	topLeftPosition : function(element) {
    		var left = parseInt(ToolMan.css().readStyle(element, "left"))
    		var left = isNaN(left) ? 0 : left
    		var top = parseInt(ToolMan.css().readStyle(element, "top"))
    		var top = isNaN(top) ? 0 : top
     
    		return this.create(left, top)
    	},
     
    	bottomRightPosition : function(element) {
    		return this.topLeftPosition(element).plus(this._size(element))
    	},
     
    	topLeftOffset : function(element) {
    		var offset = this._offset(element) 
     
    		var parent = element.offsetParent
    		while (parent) {
    			offset = offset.plus(this._offset(parent))
    			parent = parent.offsetParent
    		}
    		return offset
    	},
     
    	bottomRightOffset : function(element) {
    		return this.topLeftOffset(element).plus(
    				this.create(element.offsetWidth, element.offsetHeight))
    	},
     
    	scrollOffset : function() {
    		if (window.pageXOffset) {
    			return this.create(window.pageXOffset, window.pageYOffset)
    		} else if (document.documentElement) {
    			return this.create(
    					document.body.scrollLeft + document.documentElement.scrollLeft, 
    					document.body.scrollTop + document.documentElement.scrollTop)
    		} else if (document.body.scrollLeft >= 0) {
    			return this.create(document.body.scrollLeft, document.body.scrollTop)
    		} else {
    			return this.create(0, 0)
    		}
    	},
     
    	clientSize : function() {
    		if (window.innerHeight >= 0) {
    			return this.create(window.innerWidth, window.innerHeight)
    		} else if (document.documentElement) {
    			return this.create(document.documentElement.clientWidth,
    					document.documentElement.clientHeight)
    		} else if (document.body.clientHeight >= 0) {
    			return this.create(document.body.clientWidth,
    					document.body.clientHeight)
    		} else {
    			return this.create(0, 0)
    		}
    	},
     
    	/**
    	 * mouse coordinate relative to the window (technically the
    	 * browser client area) i.e. the part showing your page
    	 *
    	 * NOTE: in Safari the coordinate is relative to the document
    	 */
    	mousePosition : function(event) {
    		event = ToolMan.events().fix(event)
    		return this.create(event.clientX, event.clientY)
    	},
     
    	/**
    	 * mouse coordinate relative to the document
    	 */
    	mouseOffset : function(event) {
    		event = ToolMan.events().fix(event)
    		if (event.pageX >= 0 || event.pageX < 0) {
    			return this.create(event.pageX, event.pageY)
    		} else if (event.clientX >= 0 || event.clientX < 0) {
    			return this.mousePosition(event).plus(this.scrollOffset())
    		}
    	},
     
    	_size : function(element) {
    	/* TODO: move to a Dimension class */
    		return this.create(element.offsetWidth, element.offsetHeight)
    	},
     
    	_offset : function(element) {
    		return this.create(element.offsetLeft, element.offsetTop)
    	}
    }
     
    function _ToolManCoordinate(factory, x, y) {
    	this.factory = factory
    	this.x = isNaN(x) ? 0 : x
    	this.y = isNaN(y) ? 0 : y
    }
     
    _ToolManCoordinate.prototype = {
    	toString : function() {
    		return "(" + this.x + "," + this.y + ")"
    	},
     
    	plus : function(that) {
    		return this.factory.create(this.x + that.x, this.y + that.y)
    	},
     
    	minus : function(that) {
    		return this.factory.create(this.x - that.x, this.y - that.y)
    	},
     
    	min : function(that) {
    		return this.factory.create(
    				Math.min(this.x , that.x), Math.min(this.y , that.y))
    	},
     
    	max : function(that) {
    		return this.factory.create(
    				Math.max(this.x , that.x), Math.max(this.y , that.y))
    	},
     
    	constrainTo : function (one, two) {
    		var min = one.min(two)
    		var max = one.max(two)
     
    		return this.max(min).min(max)
    	},
     
    	distance : function (that) {
    		return Math.sqrt(Math.pow(this.x - that.x, 2) + Math.pow(this.y - that.y, 2))
    	},
     
    	reposition : function(element) {
    		element.style["top"] = this.y + "px"
    		element.style["left"] = this.x + "px"
    	}
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    /* Copyright (c) 2005 Tim Taylor Consulting (see LICENSE.txt) */
     
    ToolMan._dragFactory = {
    	createSimpleGroup : function(element, handle) {
    		handle = handle ? handle : element
    		var group = this.createGroup(element)
    		group.setHandle(handle)
    		group.transparentDrag()
    		//group.onTopWhileDragging()
    		return group
    	},
     
    	createGroup : function(element) {
    		var group = new _ToolManDragGroup(this, element)
     
    		var position = ToolMan.css().readStyle(element, 'position')
    		if (position == 'static') {
    			element.style["position"] = 'relative'
    		} else if (position == 'absolute') {
    			/* for Safari 1.2 */
    			ToolMan.coordinates().topLeftOffset(element).reposition(element)
    		}
     
    		// TODO: only if ToolMan.isDebugging()
    		group.register('draginit', this._showDragEventStatus)
    		group.register('dragmove', this._showDragEventStatus)
    		group.register('dragend', this._showDragEventStatus)
     
    		return group
    	},
     
    	_showDragEventStatus : function(dragEvent) {
    		window.status = dragEvent.toString()
    	},
     
    	constraints : function() {
    		return this._constraintFactory
    	},
     
    	_createEvent : function(type, event, group) {
    		return new _ToolManDragEvent(type, event, group)
    	}
    }
     
    function _ToolManDragGroup(factory, element) {
    	this.factory = factory
    	this.element = element
    	this._handle = null
    	this._thresholdDistance = 0
    	this._transforms = new Array()
    	// TODO: refactor into a helper object, move into events.js
    	this._listeners = new Array()
    	this._listeners['draginit'] = new Array()
    	this._listeners['dragstart'] = new Array()
    	this._listeners['dragmove'] = new Array()
    	this._listeners['dragend'] = new Array()
    }
     
    _ToolManDragGroup.prototype = {
    	/*
    	 * TODO:
    	 *   - unregister(type, func)
    	 *   - move custom event listener stuff into Event library
    	 *   - keyboard nudging of "selected" group
    	 */
     
    	setHandle : function(handle) {
    		var events = ToolMan.events()
     
    		handle.toolManDragGroup = this
    		events.register(handle, 'mousedown', this._dragInit)
    		handle.onmousedown = function() { return false }
     
    		if (this.element != handle)
    			events.unregister(this.element, 'mousedown', this._dragInit)
    	},
     
    	register : function(type, func) {
    		this._listeners[type].push(func)
    	},
     
    	addTransform : function(transformFunc) {
    		this._transforms.push(transformFunc)
    	},
     
    	verticalOnly : function() {
    		this.addTransform(this.factory.constraints().vertical())
    	},
     
    	horizontalOnly : function() {
    		this.addTransform(this.factory.constraints().horizontal())
    	},
     
    	setThreshold : function(thresholdDistance) {
    		this._thresholdDistance = thresholdDistance
    	},
     
    	transparentDrag : function(opacity) {
    		var opacity = typeof(opacity) != "undefined" ? opacity : 0.75;
    		var originalOpacity = ToolMan.css().readStyle(this.element, "opacity")
     
    		this.register('dragstart', function(dragEvent) {
    			var element = dragEvent.group.element
    			element.style.opacity = opacity
    			element.style.filter = 'alpha(opacity=' + (opacity * 100) + ')'
    		})
    		this.register('dragend', function(dragEvent) {
    			var element = dragEvent.group.element
    			element.style.opacity = originalOpacity
    			element.style.filter = 'alpha(opacity=100)'
    		})
    	},
     
    	onTopWhileDragging : function(zIndex) {
    		var zIndex = typeof(zIndex) != "undefined" ? zIndex : 100000;
    		var originalZIndex = ToolMan.css().readStyle(this.element, "z-index")
     
    		this.register('dragstart', function(dragEvent) {
    			dragEvent.group.element.style.zIndex = zIndex
    		})
    		this.register('dragend', function(dragEvent) {
    			alert(originalZIndex)
    			dragEvent.group.element.style.zIndex = originalZIndex
    		})
    	},
     
    	_dragInit : function(event) {
    		event = ToolMan.events().fix(event)
    		var group = document.toolManDragGroup = this.toolManDragGroup
    		var dragEvent = group.factory._createEvent('draginit', event, group)
     
    		group._isThresholdExceeded = false
    		group._initialMouseOffset = dragEvent.mouseOffset
    		group._grabOffset = dragEvent.mouseOffset.minus(dragEvent.topLeftOffset)
    		ToolMan.events().register(document, 'mousemove', group._drag)
    		document.onmousemove = function() { return false }
    		ToolMan.events().register(document, 'mouseup', group._dragEnd)
     
    		group._notifyListeners(dragEvent)
    	},
     
    	_drag : function(event) {
    		event = ToolMan.events().fix(event)
    		var coordinates = ToolMan.coordinates()
    		var group = this.toolManDragGroup
    		if (!group) return
    		var dragEvent = group.factory._createEvent('dragmove', event, group)
     
    		var newTopLeftOffset = dragEvent.mouseOffset.minus(group._grabOffset)
     
    		// TODO: replace with DragThreshold object
    		if (!group._isThresholdExceeded) {
    			var distance = 
    					dragEvent.mouseOffset.distance(group._initialMouseOffset)
    			if (distance < group._thresholdDistance) return
    			group._isThresholdExceeded = true
    			group._notifyListeners(
    					group.factory._createEvent('dragstart', event, group))
    		}
     
    		for (i in group._transforms) {
    			var transform = group._transforms[i]
    			newTopLeftOffset = transform(newTopLeftOffset, dragEvent)
    		}
     
    		var dragDelta = newTopLeftOffset.minus(dragEvent.topLeftOffset)
    		var newTopLeftPosition = dragEvent.topLeftPosition.plus(dragDelta)
    		newTopLeftPosition.reposition(group.element)
    		dragEvent.transformedMouseOffset = newTopLeftOffset.plus(group._grabOffset)
     
    		group._notifyListeners(dragEvent)
     
    		var errorDelta = newTopLeftOffset.minus(coordinates.topLeftOffset(group.element))
    		if (errorDelta.x != 0 || errorDelta.y != 0) {
    			coordinates.topLeftPosition(group.element).plus(errorDelta).reposition(group.element)
    		}
    	},
     
    	_dragEnd : function(event) {
    		event = ToolMan.events().fix(event)
    		var group = this.toolManDragGroup
    		var dragEvent = group.factory._createEvent('dragend', event, group)
     
    		group._notifyListeners(dragEvent)
     
    		this.toolManDragGroup = null
    		ToolMan.events().unregister(document, 'mousemove', group._drag)
    		document.onmousemove = null
    		ToolMan.events().unregister(document, 'mouseup', group._dragEnd)
    	},
     
    	_notifyListeners : function(dragEvent) {
    		var listeners = this._listeners[dragEvent.type]
    		for (i in listeners) {
    			listeners[i](dragEvent)
    		}
    	}
    }
     
    function _ToolManDragEvent(type, event, group) {
    	this.type = type
    	this.group = group
    	this.mousePosition = ToolMan.coordinates().mousePosition(event)
    	this.mouseOffset = ToolMan.coordinates().mouseOffset(event)
    	this.transformedMouseOffset = this.mouseOffset
    	this.topLeftPosition = ToolMan.coordinates().topLeftPosition(group.element)
    	this.topLeftOffset = ToolMan.coordinates().topLeftOffset(group.element)
    }
     
    _ToolManDragEvent.prototype = {
    	toString : function() {
    		return "mouse: " + this.mousePosition + this.mouseOffset + "    " +
    				"xmouse: " + this.transformedMouseOffset + "    " +
    				"left,top: " + this.topLeftPosition + this.topLeftOffset
    	}
    }
     
    ToolMan._dragFactory._constraintFactory = {
    	vertical : function() {
    		return function(coordinate, dragEvent) {
    			var x = dragEvent.topLeftOffset.x
    			return coordinate.x != x
    					? coordinate.factory.create(x, coordinate.y) 
    					: coordinate
    		}
    	},
     
    	horizontal : function() {
    		return function(coordinate, dragEvent) {
    			var y = dragEvent.topLeftOffset.y
    			return coordinate.y != y
    					? coordinate.factory.create(coordinate.x, y) 
    					: coordinate
    		}
    	}
    }

  3. #3
    Membre actif
    Profil pro
    Inscrit en
    Avril 2005
    Messages
    818
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France, Drôme (Rhône Alpes)

    Informations forums :
    Inscription : Avril 2005
    Messages : 818
    Points : 288
    Points
    288
    Par défaut
    Apres moultes essais, j'ai trouvé!!
    Je met la réponse au cas où ca pourrait servir à quelqu'un...
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    dragEvent.group.element.id
    Ceci renvoi l'id du div que l'on drag...

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. utilisation plugin arbre drag and drop
    Par fabrizti dans le forum Général JavaScript
    Réponses: 0
    Dernier message: 27/10/2010, 10h03
  2. Réponses: 3
    Dernier message: 25/08/2008, 16h58
  3. Reprise de controle de la console après l'utilisation de man
    Par Madmac dans le forum Administration système
    Réponses: 3
    Dernier message: 20/02/2006, 08h17
  4. [debutant] drag & drop utilisation
    Par nixonne dans le forum Général JavaScript
    Réponses: 3
    Dernier message: 11/01/2006, 16h25
  5. [Velocity] Comment utiliser les velocity tools ?
    Par tnodev dans le forum Développement Web en Java
    Réponses: 3
    Dernier message: 17/07/2005, 11h03

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo