2 * library for ajaxcourse formats, the classes and related functions for drag and drop blocks
4 * this library requires a 'main' object created in calling document
10 //set Drag and Drop to Intersect mode:
11 YAHOO
.util
.DDM
.mode
= YAHOO
.util
.DDM
.INTERSECT
;
15 * class for draggable block, extends YAHOO.util.DDProxy
17 function block_class(id
,group
,config
){
18 this.init_block(id
,group
,config
);
20 YAHOO
.extend(block_class
, YAHOO
.util
.DDProxy
);
22 block_class
.prototype.debug
= false;
25 block_class
.prototype.init_block = function(id
, sGroup
, config
) {
32 this.init(id
, sGroup
, config
);
37 this.instanceId
= this.getEl().id
.replace(/inst/i, '');
39 // Add the drag class (move handle) only to blocks that need it.
40 YAHOO
.util
.Dom
.addClass(this.getEl(), 'drag');
42 this.addInvalidHandleType('a');
44 var s
= this.getEl().style
;
46 s
.filter
= "alpha(opacity=76)";
48 // specify that this is not currently a drop target
49 this.isTarget
= false;
51 this.region
= YAHOO
.util
.Region
.getRegion(this.getEl());
52 this.type
= block_class
.TYPE
;
55 this.viewbutton
= null;
56 this.originalClass
= this.getEl().className
;
62 block_class
.prototype.startDrag = function(x
, y
) {
63 //operates in intersect mode
64 YAHOO
.util
.DDM
.mode
= YAHOO
.util
.DDM
.INTERSECT
;
66 YAHOO
.log(this.id
+ " startDrag");
68 var dragEl
= this.getDragEl();
69 var clickEl
= this.getEl();
71 dragEl
.innerHTML
= clickEl
.innerHTML
;
72 dragEl
.className
= clickEl
.className
;
73 dragEl
.style
.color
= this.DDM
.getStyle(clickEl
, "color");;
74 dragEl
.style
.backgroundColor
= this.DDM
.getStyle(clickEl
, "backgroundColor");
75 dragEl
.style
.border
= '0px';
77 var s
= clickEl
.style
;
79 s
.filter
= "alpha(opacity=10)";
81 var targets
= YAHOO
.util
.DDM
.getRelated(this, true);
82 YAHOO
.log(targets
.length
+ " targets");
84 //restyle side boxes to highlight
85 for (var i
=0; i
<targets
.length
; i
++) {
87 var targetEl
= targets
[i
].getEl();
89 targetEl
.style
.background
= "#fefff0";
90 targetEl
.opacity
= .3;
91 targetEl
.filter
= "alpha(opacity=30)";
95 block_class
.prototype.endDrag = function() {
96 // reset the linked element styles
97 var s
= this.getEl().style
;
99 s
.filter
= "alpha(opacity=100)";
104 block_class
.prototype.onDragDrop = function(e
, id
) {
105 // get the drag and drop object that was targeted
108 if ("string" == typeof id
) {
109 oDD
= YAHOO
.util
.DDM
.getDDById(id
);
111 oDD
= YAHOO
.util
.DDM
.getBestMatch(id
);
114 var el
= this.getEl();
117 YAHOO
.log("id="+id
+" el="+e
+" x="+YAHOO
.util
.Dom
.getXY(this.getDragEl()));
119 //var collisions = this.find_collisions(e,id);
122 //YAHOO.util.DDM.moveToEl(el, oDD.getEl());
128 block_class
.prototype.find_target = function(column
){
129 var collisions
= column
.find_sub_collision(YAHOO
.util
.Region
.getRegion(this.getDragEl()));
132 var insertbefore
= null;
133 if(collisions
.length
== 0)
136 insertbefore
= column
.blocks
[collisions
[0][0]];
141 block_class
.prototype.resetTargets = function() {
142 // reset the target styles
143 var targets
= YAHOO
.util
.DDM
.getRelated(this, true);
144 for (var i
=0; i
<targets
.length
; i
++) {
145 var targetEl
= targets
[i
].getEl();
146 targetEl
.style
.background
= "";
147 targetEl
.opacity
= 1;
148 targetEl
.filter
= "alpha(opacity=100)";
152 block_class
.prototype.move_block = function(columnid
){
153 if(this.debug
)YAHOO
.log("Dropped on "+columnid
[0]);
154 //var column = YAHOO.util.DDM.getDDById(columnid[0].);
155 column
= columnid
[0];
156 var inserttarget
= this.find_target(column
);
158 if(this.debug
&& inserttarget
!= null)YAHOO
.log("moving "+this.getEl().id
+" before "+inserttarget
.getEl().id
+" - parentNode="+this.getEl().parentNode
.id
);
160 if(this == inserttarget
){
161 if(this.debug
)YAHOO
.log("Dropping on self, resetting");
166 //remove from document
167 if(this.getEl().parentNode
!= null)
168 this.getEl().parentNode
.removeChild(this.getEl());
170 //insert into correct place
171 if(inserttarget
!= null ){
172 inserttarget
.getEl().parentNode
.insertBefore(this.getEl(),inserttarget
.getEl());
174 }else if(column
== main
.rightcolumn
){//if right side insert before admin block
175 column
.getEl().insertBefore(this.getEl(),main
.adminBlock
);
178 column
.getEl().appendChild(this.getEl());
181 this.reset_regions();
183 //remove block from current array
184 if(main
.rightcolumn
.has_block(this))
185 main
.rightcolumn
.remove_block(this);
187 else if(main
.leftcolumn
.has_block(this))
188 main
.leftcolumn
.remove_block(this);
190 //insert into new array
191 column
.insert_block(this,inserttarget
);
196 block_class
.prototype.reset_regions = function() {
197 var blockcount
= main
.blocks
.length
;
198 for (i
=0; i
<blockcount
; i
++) {
199 main
.blocks
[i
].region
= YAHOO
.util
.Region
.getRegion(main
.blocks
[i
].getEl());
204 block_class
.prototype.init_buttons = function() {
205 var viewbutton
= main
.mk_button('a', '/t/hide.gif', [['class', 'icon hide']]);
206 YAHOO
.util
.Event
.addListener(viewbutton
, 'click', this.toggle_hide
, this, true);
208 var deletebutton
= main
.mk_button('a', '/t/delete.gif', [['class', 'icon delete']]);
209 YAHOO
.util
.Event
.addListener(deletebutton
, 'click', this.delete_button
, this, true);
211 this.viewbutton
= viewbutton
;
213 buttonCont
= YAHOO
.util
.Dom
.getElementsByClassName('commands', 'div', this.getEl())[0];
216 buttonCont
.appendChild(viewbutton
);
217 buttonCont
.appendChild(deletebutton
);
222 block_class
.prototype.toggle_hide = function(e
, target
, isCosmetic
) {
224 if (YAHOO
.util
.Dom
.hasClass(this.getEl(), 'hidden')) {
225 this.getEl().className
= this.originalClass
;
226 this.viewbutton
.childNodes
[0].src
= this.viewbutton
.childNodes
[0].src
.replace(/show.gif/i, 'hide.gif');
229 main
.connect('POST', 'class=block&field=visible', null,
230 'value=1&instanceId='+this.instanceId
);
233 this.originalClass
= this.getEl().className
;
234 this.getEl().className
= "hidden sideblock";
235 this.viewbutton
.childNodes
[0].src
= this.viewbutton
.childNodes
[0].src
.replace(/hide.gif/i,'show.gif');
238 main
.connect('POST', 'class=block&field=visible', null,
239 'value=0&instanceId='+this.instanceId
);
245 block_class
.prototype.delete_button = function() {
246 // Remove from local model.
247 if (main
.rightcolumn
.has_block(this)) {
248 main
.rightcolumn
.remove_block(this);
249 } else if (main
.leftcolumn
.has_block(this)) {
250 main
.leftcolumn
.remove_block(this);
252 // Remove block from the drag and drop group in YUI.
253 this.removeFromGroup('blocks');
255 // Remove from remote model.
256 main
.connect('DELETE', 'class=block&instanceId='+this.instanceId
);
259 main
.blocks
.splice(main
.get_block_index(this), 1);
260 this.getEl().parentNode
.removeChild(this.getEl());
263 YAHOO
.log("Deleting "+this.getEl().id
);
268 block_class
.prototype.updatePosition = function(index
, columnId
) {
269 //update the db for the position
270 main
.connectQueue_add('POST', 'class=block&field=position', null,
271 'value='+index
+'&column='+columnId
+'&instanceId='+this.instanceId
);
274 YAHOO
.log("Updating position of "+this.getEl().id
+" to index "+index
+" on column "+columnId
);
280 * column class, DD targets
283 function column_class(id
,group
,config
,ident
){
284 this.init_column(id
,group
,config
,ident
);
286 YAHOO
.extend(column_class
, YAHOO
.util
.DDTarget
);
288 column_class
.prototype.debug
= false;
290 column_class
.prototype.init_column = function(id
, group
,config
,ident
){
293 this.initTarget(id
,group
,config
);
294 this.blocks
= new Array();
297 // YAHOO.log("init_column "+id+"-"+el.id);
298 this.region
= YAHOO
.util
.Region
.getRegion(id
);
303 column_class
.prototype.find_sub_collision = function(dragRegion
){
304 if(this.debug
)YAHOO
.log("Finding Collisions on "+this.getEl().id
+" with "+this.blocks
.length
+" blocks");
305 //find collisions with sub_elements(blocks), return array of collisions with regions of collision
306 var collisions
= new Array();
307 for(i
=0;i
<this.blocks
.length
;i
++){
308 if(this.debug
)YAHOO
.log("testing region "+this.blocks
[i
].region
+" against" + dragRegion
+ "intersect ="+this.blocks
[i
].region
.intersect(dragRegion
));
309 var intersect
= this.blocks
[i
].region
.intersect(dragRegion
);
310 if(intersect
!= null){
311 index
= collisions
.length
;
312 collisions
[index
] = new Array();
313 collisions
[index
][0] = i
;
314 collisions
[index
][1] = this.blocks
[i
].region
.intersect(dragRegion
);
315 if(this.debug
)YAHOO
.log(index
+" Collides with "+this.blocks
[i
].getEl().id
+" area" + collisions
[index
][1].getArea());
321 column_class
.prototype.add_block = function(el
){
322 this.blocks
[this.blocks
.length
] = el
;
325 column_class
.prototype.insert_block = function(el
,targetel
){
326 var blockcount
= this.blocks
.length
;
328 var tempStore
= nextStore
= null;
329 for(var i
=0;i
<blockcount
;i
++){
331 tempStore
= this.blocks
[i
];
332 this.blocks
[i
] = nextStore
;
333 nextStore
= tempStore
;
335 }else if(this.blocks
[i
] == targetel
){
337 nextStore
= this.blocks
[i
];
343 if(found
<0){//insert at end
344 found
= this.blocks
.length
;
349 el
.updatePosition(found
,this.ident
);
352 column_class
.prototype.has_block = function(el
){
353 var blockcount
= this.blocks
.length
;
354 for(var i
=0;i
<blockcount
;i
++)
355 if(this.blocks
[i
]==el
)
361 column_class
.prototype.remove_block = function(el
){
362 var blockcount
= this.blocks
.length
;
364 for(var i
=0;i
<blockcount
;i
++){
365 if(this.blocks
[i
]==el
|| found
){
369 if(i
< blockcount
-1){
370 this.blocks
[i
] = this.blocks
[i
+1];
376 YAHOO
.log("column "+this.indent
+" has "+blockcount
+"blocks");