1 // ========================================================================== 2 // Project: SC - designPage 3 // Copyright: ©2010 Mike Ball 4 // ========================================================================== 5 /*globals SC */ 6 /*jslint evil: true*/ 7 /** 8 This View is used by Greenhouse when application is in design mode 9 10 11 @extends SC.ContainerView 12 */ 13 SC.DesignerDropTarget = SC.ContainerView.extend({ 14 15 inGlobalOffset: YES, 16 17 // .......................................................... 18 // Key Events 19 // 20 acceptsFirstResponder: YES, 21 22 keyDown: function(evt) { 23 return this.interpretKeyEvents(evt); 24 }, 25 26 keyUp: function(evt) { 27 return YES; 28 }, 29 30 deleteForward: function(evt){ 31 var c = SC.designsController.getPath('page.designController'); 32 if(c) c.deleteSelection(); 33 return YES; 34 }, 35 36 deleteBackward: function(evt){ 37 var c = SC.designsController.getPath('page.designController'); 38 if(c) c.deleteSelection(); 39 return YES; 40 }, 41 42 moveLeft: function(sender, evt) { 43 return YES; 44 }, 45 46 moveRight: function(sender, evt) { 47 return YES; 48 }, 49 50 moveUp: function(sender, evt) { 51 return YES; 52 }, 53 54 moveDown: function(sender, evt) { 55 return YES; 56 }, 57 58 // .......................................................... 59 // Drag and drop code 60 // 61 isDropTarget: YES, 62 63 targetIsInIFrame: YES, 64 65 dragStarted: function(drag, evt) { 66 }, 67 68 dragEntered: function(drag, evt) { 69 }, 70 71 dragUpdated: function(drag, evt) {}, 72 73 dragExited: function(drag, evt) {}, 74 75 dragEnded: function(drag, evt) {}, 76 77 78 computeDragOperations: function(drag, evt) { 79 return SC.DRAG_ANY; 80 }, 81 82 83 acceptDragOperation: function(drag, op) { 84 var data = drag.dataForType('SC.Object'), 85 scClass = eval(data.get('scClass')); 86 return scClass.kindOf(SC.View); 87 }, 88 89 /** 90 Called to actually perform the drag operation. 91 92 Override this method to actually perform the drag operation. This method 93 is only called if you returned `YES` in `acceptDragOperation()`. 94 95 Return the operation that was actually performed or `SC.DRAG_NONE` if the 96 operation was aborted. 97 98 The default implementation returns `SC.DRAG_NONE` 99 100 @param {SC.Drag} drag The drag instance managing this drag 101 @param {DragOp} op The proposed drag operation. A drag constant. 102 103 @return {DragOp} Drag Operation actually performed 104 */ 105 performDragOperation: function(drag, op) { 106 var data = drag.dataForType('SC.Object'), 107 cv = this.get('contentView'), 108 loc = drag.get('location'), 109 iframeOffset = drag.globalTargetOffset, 110 design, size, newView, defaults, layout; 111 var page = cv.get('page'); 112 var designController = page.get('designController'), 113 rootDesigner = designController.get('rootDesigner'); 114 var rootDesignerFrame = rootDesigner.get('frame'); 115 //TODO: [MB] should we move most of this into the designer's addView? 116 //size and location 117 size = data.get('size'); 118 loc.x = loc.x - iframeOffset.x - rootDesignerFrame.x; 119 loc.y = loc.y - iframeOffset.y - rootDesignerFrame.y; 120 //setup design (use eval to make sure code comes from iframe) 121 //TODO use new Function("return "+data.get('scClass))() ?... 122 design = eval(data.get('scClass')); 123 defaults = data.get('defaults') || {}; 124 layout = defaults.layout || {}; 125 layout = SC.merge(layout, {top: loc.y, left: loc.x}); 126 //pull width and height from ghost if none exists form defaults 127 if(!layout.width) layout.width = drag.getPath('ghostView.layout').width; 128 if(!layout.height) layout.height = drag.getPath('ghostView.layout').height; 129 defaults.layout = layout; 130 design = design.design(defaults); 131 //drop it in the root designer 132 newView = design.create({page: page}); 133 if(rootDesigner && newView){ 134 rootDesigner.addView(newView); 135 //cv.appendChild(newView); 136 } 137 page.get('designController').select(newView.get('designer')); 138 return SC.DRAG_ANY; 139 } 140 141 142 }); 143