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', main.portal.strings['hide'], [['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', main.portal.strings['delete'], [['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) {
223 var strhide = main.portal.strings['hide'];
224 var strshow = main.portal.strings['show'];
225 if (YAHOO.util.Dom.hasClass(this.getEl(), 'hidden')) {
226 this.getEl().className = this.originalClass;
227 this.viewbutton.childNodes[0].src = this.viewbutton.childNodes[0].src.replace(/show.gif/i, 'hide.gif');
228 this.viewbutton.childNodes[0].alt = this.viewbutton.childNodes[0].alt.replace(strshow, strhide);
229 this.viewbutton.title = this.viewbutton.title.replace(strshow, strhide);
232 main.connect('POST', 'class=block&field=visible', null,
233 'value=1&instanceId='+this.instanceId);
236 this.originalClass = this.getEl().className;
237 this.getEl().className = "hidden sideblock";
238 this.viewbutton.childNodes[0].src = this.viewbutton.childNodes[0].src.replace(/hide.gif/i,'show.gif');
239 this.viewbutton.childNodes[0].alt = this.viewbutton.childNodes[0].alt.replace(strhide, strshow);
240 this.viewbutton.title = this.viewbutton.title.replace(strhide, strshow);
243 main.connect('POST', 'class=block&field=visible', null,
244 'value=0&instanceId='+this.instanceId);
250 block_class.prototype.delete_button = function() {
251 // Remove from local model.
252 if (main.rightcolumn.has_block(this)) {
253 main.rightcolumn.remove_block(this);
254 } else if (main.leftcolumn.has_block(this)) {
255 main.leftcolumn.remove_block(this);
257 // Remove block from the drag and drop group in YUI.
258 this.removeFromGroup('blocks');
260 // Remove from remote model.
261 main.connect('DELETE', 'class=block&instanceId='+this.instanceId);
264 main.blocks.splice(main.get_block_index(this), 1);
265 this.getEl().parentNode.removeChild(this.getEl());
268 YAHOO.log("Deleting "+this.getEl().id);
273 block_class.prototype.updatePosition = function(index, columnId) {
274 //update the db for the position
275 main.connectQueue_add('POST', 'class=block&field=position', null,
276 'value='+index+'&column='+columnId+'&instanceId='+this.instanceId);
279 YAHOO.log("Updating position of "+this.getEl().id+" to index "+index+" on column "+columnId);
285 * column class, DD targets
288 function column_class(id,group,config,ident){
289 this.init_column(id,group,config,ident);
291 YAHOO.extend(column_class, YAHOO.util.DDTarget);
293 column_class.prototype.debug = false;
295 column_class.prototype.init_column = function(id, group,config,ident){
298 this.initTarget(id,group,config);
299 this.blocks = new Array();
302 // YAHOO.log("init_column "+id+"-"+el.id);
303 this.region = YAHOO.util.Region.getRegion(id);
308 column_class.prototype.find_sub_collision = function(dragRegion){
309 if(this.debug)YAHOO.log("Finding Collisions on "+this.getEl().id+" with "+this.blocks.length+" blocks");
310 //find collisions with sub_elements(blocks), return array of collisions with regions of collision
311 var collisions = new Array();
312 for(i=0;i<this.blocks.length;i++){
313 if(this.debug)YAHOO.log("testing region "+this.blocks[i].region+" against" + dragRegion + "intersect ="+this.blocks[i].region.intersect(dragRegion));
314 var intersect = this.blocks[i].region.intersect(dragRegion);
315 if(intersect != null){
316 index = collisions.length;
317 collisions[index] = new Array();
318 collisions[index][0] = i;
319 collisions[index][1] = this.blocks[i].region.intersect(dragRegion);
320 if(this.debug)YAHOO.log(index+" Collides with "+this.blocks[i].getEl().id+" area" + collisions[index][1].getArea());
326 column_class.prototype.add_block = function(el){
327 this.blocks[this.blocks.length] = el;
330 column_class.prototype.insert_block = function(el,targetel){
331 var blockcount = this.blocks.length;
333 var tempStore = nextStore = null;
334 for(var i=0;i<blockcount;i++){
336 tempStore = this.blocks[i];
337 this.blocks[i] = nextStore;
338 nextStore = tempStore;
340 }else if(this.blocks[i] == targetel){
342 nextStore = this.blocks[i];
348 if(found<0){//insert at end
349 found = this.blocks.length;
354 el.updatePosition(found,this.ident);
357 column_class.prototype.has_block = function(el){
358 var blockcount = this.blocks.length;
359 for(var i=0;i<blockcount;i++)
360 if(this.blocks[i]==el)
366 column_class.prototype.remove_block = function(el){
367 var blockcount = this.blocks.length;
369 for(var i=0;i<blockcount;i++){
370 if(this.blocks[i]==el || found){
374 if(i < blockcount-1){
375 this.blocks[i] = this.blocks[i+1];
381 YAHOO.log("column "+this.indent+" has "+blockcount+"blocks");