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 /*global jQuery*/
  8 sc_require('system/builder');
  9 
 10 // Alias jQuery as SC.$ and SC.CoreQuery for compatibility
 11 SC.$ = SC.CoreQuery = jQuery;
 12 
 13 // Add some plugins to SC.$. jQuery will get these also. -- test in system/core_query/additions
 14 SC.mixin(SC.$.fn,
 15   /** @scope SC.$.prototype */ {
 16 
 17   isCoreQuery: YES, // walk like a duck
 18 
 19   /** @private - better loggin */
 20   toString: function () {
 21     var values = [],
 22       len = this.length,
 23       idx = 0;
 24 
 25     for (idx = 0; idx < len; idx++) {
 26       values[idx] = '%@: %@'.fmt(idx, this[idx] ? this[idx].toString() : '(null)');
 27     }
 28     return "<$:%@>(%@)".fmt(SC.guidFor(this), values.join(' , '));
 29   },
 30 
 31   /**
 32     Returns YES if all member elements are visible.  This is provided as a
 33     common test since CoreQuery does not support filtering by
 34     psuedo-selector.
 35 
 36     @deprecated Version 1.10
 37   */
 38   isVisible: function () {
 39     return Array.prototype.every.call(this, function (elem) {
 40       return SC.$.isVisible(elem);
 41     });
 42   },
 43 
 44   /**
 45     Attempts to find the views managing the passed DOM elements and returns
 46     them.   This will start with the matched element and walk up the DOM until
 47     it finds an element managed by a view.
 48 
 49     @returns {Array} array of views or null.
 50     @deprecated Version 1.10
 51   */
 52   view: function () {
 53     //@if(debug)
 54     // Deprecation warning.
 55     SC.warn("Developer Warning: SC.$.view() has been deprecated and will be removed in the future. Please use SC.viewFor(element) instead.");
 56     //@endif
 57 
 58     return SC.viewFor(this[0]);
 59   },
 60 
 61   /**
 62     Returns YES if any of the matched elements have the passed element or CQ object as a child element.
 63   */
 64   within: function (el) {
 65     if (this.filter(el).length) { return true; }
 66     return !!this.has(el).length;
 67   }
 68 
 69 });
 70 
 71 /**
 72   Make CoreQuery enumerable.  Since some methods need to be disambiguated,
 73   we will implement some wrapper functions here.
 74 
 75   Note that SC.Enumerable is implemented on SC.Builder, which means the
 76   CoreQuery object inherits this automatically.  jQuery does not extend from
 77   SC.Builder though, so we reapply SC.Enumerable just to be safe.
 78 */
 79 (function () {
 80   var original = {},
 81       wrappers = {
 82 
 83     // if you call find with a selector, then use the jQuery way.  If you
 84     // call with a function/target, use Enumerable way
 85     find: function (callback, target) {
 86       return (target !== undefined) ? SC.Enumerable.find.call(this, callback, target) : original.find.call(this, callback);
 87     },
 88 
 89     // ditto for filter - execute SC.Enumerable style if a target is passed.
 90     filter: function (callback, target) {
 91       return (target !== undefined) ?
 92         this.pushStack(SC.Enumerable.filter.call(this, callback, target)) :
 93         original.filter.call(this, callback);
 94     },
 95 
 96     // filterProperty is an SC.Enumerable thing, but it needs to be wrapped
 97     // in a CoreQuery object.
 98     filterProperty: function (key, value) {
 99       return this.pushStack(
100         SC.Enumerable.filterProperty.call(this, key, value));
101     },
102 
103     // indexOf() is best implemented using the jQuery index()
104     indexOf: SC.$.index,
105 
106     // map() is a little tricky because jQuery is non-standard.  If you pass
107     // a context object, we will treat it like SC.Enumerable.  Otherwise use
108     // jQuery.
109     map: function (callback, target) {
110       return (target !== undefined) ?
111         SC.Enumerable.map.call(this, callback, target) :
112         original.map.call(this, callback);
113     }
114   };
115 
116   // loop through an update some enumerable methods.
117   var fn = SC.$.fn,
118     enumerable = SC.Enumerable,
119     value;
120 
121   for (var key in enumerable) {
122     if (enumerable.hasOwnProperty(key)) {
123       value = enumerable[key];
124 
125       if (key in wrappers) {
126         original[key] = fn[key];
127         value = wrappers[key];
128       }
129 
130       fn[key] = value;
131     }
132   }
133 })();
134 
135 // Add some global helper methods.
136 SC.mixin(SC.$, {
137 
138   /** @private helper method to determine if an element is visible.  Exposed
139    for use in testing.
140 
141     @deprecated Version 1.10
142   */
143   isVisible: function (elem) {
144     //@if(debug)
145     SC.warn("Developer Warning: The isVisible() helper has been deprecated and will be removed in the future.");
146     //@endif
147     var CQ = SC.$;
148     return ("hidden" != elem.type) && (CQ.css(elem, "display") != "none") && (CQ.css(elem, "visibility") != "hidden");
149   }
150 
151 });
152 
153 
154