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 /**
  9   Indicates that the collection view expects to accept a drop ON the specified
 10   item.
 11 
 12   @type Number
 13 */
 14 SC.DROP_ON = 0x01 ;
 15 
 16 /**
 17   Indicates that the collection view expects to accept a drop BEFORE the
 18   specified item.
 19 
 20   @type Number
 21 */
 22 SC.DROP_BEFORE = 0x02 ;
 23 
 24 /**
 25   Indicates that the collection view expects to accept a drop AFTER the
 26   specified item.  This is treated just like SC.DROP_BEFORE is most views
 27   except for tree lists.
 28 
 29   @type Number
 30 */
 31 SC.DROP_AFTER = 0x04 ;
 32 
 33 /**
 34   Indicates that the collection view want's to know which operations would
 35   be allowed for either drop operation.
 36 
 37   @type Number
 38 */
 39 SC.DROP_ANY = 0x07 ;
 40 
 41 /**
 42   Indicates that the content should be aligned to the left.
 43 */
 44 SC.ALIGN_LEFT = 'left';
 45 
 46 /**
 47   Indicates that the content should be aligned to the right.
 48 */
 49 SC.ALIGN_RIGHT = 'right';
 50 
 51 /**
 52   Indicates that the content should be aligned to the center.
 53 */
 54 SC.ALIGN_CENTER = 'center';
 55 
 56 /**
 57   Indicates that the content should be aligned to the top.
 58 */
 59 SC.ALIGN_TOP = 'top';
 60 
 61 /**
 62   Indicates that the content should be aligned to the middle.
 63 */
 64 SC.ALIGN_MIDDLE = 'middle';
 65 
 66 /**
 67   Indicates that the content should be aligned to the bottom.
 68 */
 69 SC.ALIGN_BOTTOM = 'bottom';
 70 
 71 /**
 72   Indicates that the content should be aligned to the top and left.
 73 */
 74 SC.ALIGN_TOP_LEFT = 'top-left';
 75 
 76 /**
 77   Indicates that the content should be aligned to the top and right.
 78 */
 79 SC.ALIGN_TOP_RIGHT = 'top-right';
 80 
 81 /**
 82   Indicates that the content should be aligned to the bottom and left.
 83 */
 84 SC.ALIGN_BOTTOM_LEFT = 'bottom-left';
 85 
 86 /**
 87   Indicates that the content should be aligned to the bottom and right.
 88 */
 89 SC.ALIGN_BOTTOM_RIGHT = 'bottom-right';
 90 
 91 /**
 92   Indicates that the content does not specify its own alignment.
 93 */
 94 SC.ALIGN_DEFAULT = 'default';
 95 
 96 /**
 97   Indicates that the content should be positioned to the right.
 98 */
 99 SC.POSITION_RIGHT = 0;
100 
101 /**
102   Indicates that the content should be positioned to the left.
103 */
104 SC.POSITION_LEFT = 1;
105 
106 /**
107   Indicates that the content should be positioned above.
108 */
109 SC.POSITION_TOP = 2;
110 
111 /**
112   Indicates that the content should be positioned below.
113 */
114 SC.POSITION_BOTTOM = 3;
115 
116 
117 SC.mixin(/** @lends SC */ {
118 
119   /**
120     Reads or writes data from a global cache.  You can use this facility to
121     store information about an object without actually adding properties to
122     the object itself.  This is needed especially when working with DOM,
123     which can leak easily in IE.
124 
125     To read data, simply pass in the reference element (used as a key) and
126     the name of the value to read.  To write, also include the data.
127 
128     You can also just pass an object to retrieve the entire cache.
129 
130     @param elem {Object} An object or Element to use as scope
131     @param name {String} Optional name of the value to read/write
132     @param data {Object} Optional data.  If passed, write.
133     @returns {Object} the value of the named data
134   */
135   data: $.data,
136 
137   /**
138     Removes data from the global cache.  This is used throughout the
139     framework to hold data without creating memory leaks.
140 
141     You can remove either a single item on the cache or all of the cached
142     data for an object.
143 
144     @param elem {Object} An object or Element to use as scope
145     @param name {String} optional name to remove.
146     @returns {Object} the value or cache that was removed
147   */
148   removeData: $.removeData,
149 
150   // ..........................................................
151   // LOCALIZATION SUPPORT
152   //
153 
154   /**
155     Known loc strings
156 
157     @type Hash
158   */
159   STRINGS: {},
160 
161   /**
162     This is a simplified handler for installing a bunch of strings.  This
163     ignores the language name and simply applies the passed strings hash.
164 
165     @param {String} lang the language the strings are for
166     @param {Hash} strings hash of strings
167     @returns {SC} The receiver, useful for chaining calls to the same object.
168   */
169   stringsFor: function(lang, strings) {
170     SC.mixin(SC.STRINGS, strings);
171     return this ;
172   },
173 
174   /**
175     Returns the SC.View instance managing the given element.
176 
177     If no instance is found, returns `null`.
178 
179     @param {DOMElement} element The element to check.
180     @returns {SC.View} The view managing the element or null if none found.
181   */
182   viewFor: function (element) {
183     //@if(debug)
184     // Debug mode only arrgument validation.
185     // Ensure that every argument is correct and that the proper number of arguments is given.
186     if (!(element instanceof Element)) {
187       SC.error("Developer Error: Attempt to retrieve the SC.View instance for a non-element in SC.viewFor(): %@".fmt(element));
188     }
189 
190     if (arguments.length > 1) {
191       SC.warn("Developer Warning: SC.viewFor() is meant to be used with only one argument: element");
192     }
193     //@endif
194 
195     // Search for the view for the given element.
196     var viewCache = SC.View.views,
197       view = viewCache[element.getAttribute('id')];
198 
199     // If the element itself is not managed by an SC.View, walk up its element chain searching for an
200     // element that is.
201     if (!view) {
202       var parentNode;
203 
204       while (!view && (parentNode = element.parentNode) && (parentNode !== document)) {
205         var id;
206 
207         // Ensure that the parent node is an Element and not some other type of Node.
208         // Note: while `instanceOf Element` is the most accurate determiner, performance tests show
209         // that it's much quicker to check for the existence of either the `nodeType` property or
210         // the `querySelector` method.
211         // See http://jsperf.com/nodetype-1-vs-instanceof-htmlelement/4
212         // if (parentNode.querySelector && (id = parentNode.getAttribute('id'))) {
213         if ((id = parentNode.getAttribute('id'))) {
214           view = viewCache[id];
215         }
216 
217         // Check the parent node.
218         element = parentNode;
219       }
220 
221       // Avoid memory leaks (i.e. IE).
222       element = parentNode = null;
223     }
224 
225     // If view isn't found, return null rather than undefined.
226     return view || null;
227   }
228 
229 });
230