1 // ==========================================================================
  2 // Project:   SproutCore - JavaScript Application Framework
  3 // Copyright: ©2006-2011 Strobe Inc. and contributors.
  4 //            Portions ©2008-2011 Apple Inc. All rights reserved.
  5 // License:   Licensed under MIT license (see license.js)
  6 // ==========================================================================
  7 // ==========================================================================
  8 // Project:   SC.designsController
  9 // ==========================================================================
 10 /*globals SC */
 11 /*jslint evil: true*/
 12 
 13 /**
 14 
 15   (Document Your Controller Here)
 16 
 17   this controller is used by Greenhouse to list all of the views in a page files
 18 
 19   @extends SC.Object
 20 */
 21 SC.designsController = SC.ArrayController.create(SC.CollectionViewDelegate,
 22 /** @scope SC.designsController.prototype */ {
 23 
 24   setDesigns: function(page, iframe){
 25     var designs = [];
 26 
 27     for(var v in page){
 28       if(page.hasOwnProperty(v)){
 29         if(v !== '__sc_super__' && page[v] && page[v].kindOf){
 30           if(page[v].kindOf(iframe.SC.Pane)){
 31             designs.push(SC.Object.create({type: 'pane', view: page.get(v), name: v}));
 32           }
 33           else if(page[v].kindOf(iframe.SC.View)){
 34             designs.push(SC.Object.create({type: 'view', view: page.get(v), name: v}));
 35           }
 36           else if(page[v].kindOf(iframe.SC.Page)){
 37             designs.push(SC.Object.create({type: 'page', view: page.get(v), name: v}));
 38           }
 39           else if(page[v].kindOf(iframe.SC.Controller)){
 40             designs.push(SC.Object.create({type: 'controller', name: v, view: page.get(v)}));
 41           }
 42           else if(page[v].kindOf(iframe.SC.Object) && !page[v].isPageDesignController){
 43             designs.push(SC.Object.create({type: 'controller', name: v, view: page.get(v)}));
 44           }
 45 
 46         }
 47       }
 48     }
 49     this.set('content', designs);
 50     this.set('page', page);
 51   },
 52 
 53   // ..........................................................
 54   // Drop Target
 55   //
 56 
 57   collectionViewComputeDragOperations: function(view, drag, op){
 58     return SC.DRAG_ANY;
 59   },
 60   /**
 61     Called by the collection view during a drag to let you determine the
 62     kind and location of a drop you might want to accept.
 63 
 64     You can override this method to implement fine-grained control over how
 65     and when a dragged item is allowed to be dropped into a collection view.
 66 
 67     This method will be called by the collection view both to determine in
 68     general which operations you might support and specifically the operations
 69     you would support if the user dropped an item over a specific location.
 70 
 71     If the `proposedDropOperation` parameter is `SC.DROP_ON` or
 72     `SC.DROP_BEFORE`, then the `proposedInsertionPoint` will be a
 73     non-negative value and you should determine the specific operations you
 74     will support if the user dropped the drag item at that point.
 75 
 76     If you do not like the proposed drop operation or insertion point, you
 77     can override these properties as well by setting the proposedDropOperation
 78     and proposedInsertionIndex properties on the collection view during this
 79     method.  These properties are ignored all other times.
 80 
 81     @param view {SC.CollectionView} the collection view
 82     @param drag {SC.Drag} the current drag object
 83     @param op {Number} proposed logical OR of allowed drag operations.
 84     @param proposedInsertionIndex {Number} an index into the content array
 85       representing the proposed insertion point.
 86     @param proposedDropOperation {String} the proposed drop operation.  Will be one of SC.DROP_ON, SC.DROP_BEFORE, or SC.DROP_ANY.
 87     @returns the allowed drag operation.  Defaults to op
 88   */
 89   collectionViewValidateDragOperation: function(view, drag, op, proposedInsertionIndex, proposedDropOperation) {
 90     var data = drag.dataForType('SC.Object');
 91     if(data){
 92       return SC.DRAG_ANY;
 93     }
 94     else{
 95       // don't allow dropping on by default
 96       return (proposedDropOperation & SC.DROP_ON) ? SC.DRAG_NONE : op ;
 97     }
 98   },
 99 
100   /**
101     Called by the collection view to actually accept a drop.  This method will
102     only be invoked *AFTER* your `validateDrop` method has been called to
103     determine if you want to even allow the drag operation to go through.
104 
105     You should actually make changes to the data model if needed here and
106     then return the actual drag operation that was performed.  If you return
107     SC.DRAG_NONE and the dragOperation was `SC.DRAG_REORDER`, then the default
108     reorder behavior will be provided by the collection view.
109 
110     @param view {SC.CollectionView}
111     @param drag {SC.Drag} the current drag object
112     @param op {Number} proposed logical OR of allowed drag operations.
113     @param proposedInsertionIndex {Number} an index into the content array representing the proposed insertion point.
114     @param proposedDropOperation {String} the proposed drop operation.  Will be one of SC.DROP_ON, SC.DROP_BEFORE, or SC.DROP_ANY.
115     @returns the allowed drag operation.  Defaults to proposedDragOperation
116   */
117   collectionViewPerformDragOperation: function(view, drag, op, proposedInsertionIndex, proposedDropOperation) {
118     var data = drag.dataForType('SC.Object'),
119         page = this.get('page'),
120         scClass,
121         that = this;
122     if(data){
123       var actionObj = SC.Object.create({
124         data: data,
125         addItemToPage: function(name){
126           scClass = eval(this.getPath('data.scClass'));
127           var type = SC.kindOf(scClass, SC.View) ? 'view' : 'controller';
128 
129           page[name] = scClass.design().create({page: page});
130           that.pushObject(SC.Object.create({type: type, view: page.get(name), name: name}));
131         }
132       });
133 
134       SC._Greenhouse.sendAction('newPageElement', actionObj);
135       return SC.DRAG_ANY;
136     }
137     return SC.DRAG_NONE ;
138   }
139 }) ;
140