Guest User

Untitled

a guest
Jul 19th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.94 KB | None | 0 0
  1. (function() {
  2. // Establish the root object, `window` in the browser, or `global` on the server.
  3. var root = window.document;
  4.  
  5. var JL = function( selector, context ) {
  6. // The JL object is actually just the init constructor 'enhanced'
  7. return new JL.fn.init( selector, context );
  8. },
  9.  
  10. // A simple way to check for HTML strings or ID strings
  11. // (both of which we optimize for)
  12. quickExpr = /^(?:[^<]*(<[\w\W]+>)[^>]*$|#([\w\-]+)$)/,
  13.  
  14. // Is it a simple selector
  15. isSimple = /^.[^:#\[\.,]*$/,
  16.  
  17. // Check if a string has a non-whitespace character in it
  18. rnotwhite = /\S/,
  19. rwhite = /\s/,
  20.  
  21. // Used for trimming whitespace
  22. trimLeft = /^\s+/,
  23. trimRight = /\s+$/,
  24.  
  25. // Check for non-word characters
  26. rnonword = /\W/,
  27.  
  28. // Check for digits
  29. rdigit = /\d/,
  30.  
  31. // Match a standalone tag
  32. rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/,
  33.  
  34. // JSON RegExp
  35. rvalidchars = /^[\],:{}\s]*$/,
  36. rvalidescape = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,
  37. rvalidtokens = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,
  38. rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g,
  39.  
  40. // Useragent RegExp
  41. rwebkit = /(webkit)[ \/]([\w.]+)/,
  42. ropera = /(opera)(?:.*version)?[ \/]([\w.]+)/,
  43. rmsie = /(msie) ([\w.]+)/,
  44. rmozilla = /(mozilla)(?:.*? rv:([\w.]+))?/,
  45.  
  46.  
  47. // Establish the object that gets returned to break out of a loop iteration.
  48. breakr = {},
  49.  
  50. // Save bytes in the minified (but not gzipped) version:
  51. ArrProto = Array.prototype,
  52. ObjProto = Object.prototype,
  53. StrProto = String.prototype,
  54.  
  55. // Create quick reference variables for speed access to core prototypes.
  56. trim = StrProto.trim,
  57. push = ArrProto.push,
  58. slice = ArrProto.slice,
  59. shift = ArrProto.shift,
  60. unshift = ArrProto.unshift,
  61. indexOf = ArrProto.indexOf,
  62. hasOwn = ObjProto.hasOwnProperty,
  63. toString = ObjProto.toString,
  64.  
  65. // All **ECMAScript 5** native function implementations that we hope to use are declared here.
  66. nativeForEach = ArrProto.forEach,
  67. nativeMap = ArrProto.map,
  68. nativeFilter = ArrProto.filter,
  69. nativeEvery = ArrProto.every,
  70. nativeIndexOf = ArrProto.indexOf,
  71. nativeLastIndexOf = ArrProto.lastIndexOf,
  72. nativeIsArray = Array.isArray,
  73. nativeKeys = Object.keys,
  74.  
  75.  
  76. JL.fn = JL.prototype = {
  77.  
  78. init: function( selector, context ) {
  79. var match, elem;
  80.  
  81. // Handle $(""), $(null), or $(undefined)
  82. if ( !selector ) {
  83. return this;
  84. }
  85.  
  86. // Handle $(DOM Element)
  87. if ( selector.nodeType ) {
  88. this.context = this[0] = selector;
  89. this.length = 1;
  90. return this;
  91. }
  92.  
  93. // The body element only exists once, optimize finding it
  94. if ( selector === 'body' && !context && document.body ) {
  95. this.context = document;
  96. this[0] = document.body;
  97. this.selector = 'body';
  98. this.length = 1;
  99. return this;
  100. }
  101.  
  102. },
  103.  
  104. // Start with an empty selector
  105. selector: '',
  106.  
  107. },
  108.  
  109. create: function() {
  110.  
  111. },
  112.  
  113. // Execute a callback for every element in the matched set.
  114. // (You can seed the arguments with an array of args, but this is
  115. // only used internally.)
  116. each: function( callback, args ) {
  117. return JL.each( this, callback, args );
  118. },
  119.  
  120. ready: function( fn ) {
  121. // Attach the listeners
  122. jQuery.bindReady();
  123.  
  124. // If the DOM is already ready
  125. if ( jQuery.isReady ) {
  126. // Execute the function immediately
  127. fn.call( document, jQuery );
  128.  
  129. // Otherwise, remember the function for later
  130. } else if ( readyList ) {
  131. // Add the function to the wait list
  132. readyList.push( fn );
  133. }
  134.  
  135. return this;
  136. },
  137.  
  138. eq: function( i ) {
  139. return i === -1 ?
  140. this.slice( i ) :
  141. this.slice( i, +i + 1 );
  142. },
  143.  
  144. first: function() {
  145. return this.eq( 0 );
  146. },
  147.  
  148. last: function() {
  149. return this.eq( -1 );
  150. },
  151.  
  152. slice: function() {
  153. return this.pushStack( slice.apply( this, arguments ),
  154. "slice", slice.call(arguments).join(",") );
  155. },
  156.  
  157. map: function( callback ) {
  158. return this.pushStack( jQuery.map(this, function( elem, i ) {
  159. return callback.call( elem, i, elem );
  160. }));
  161. },
  162.  
  163. end: function() {
  164. return this.prevObject || jQuery(null);
  165. }
  166. }
  167.  
  168. // Give the init function the JL prototype for later instantiation
  169. JL.fn.init.prototype = JL.fn;
  170.  
  171.  
  172. /**
  173. * The cornerstone, an `each` implementation, aka `forEach`.
  174. * Handles objects implementing `forEach`, arrays, and raw objects.
  175. * Delegates to **ECMAScript 5**'s native `forEach` if available.
  176. */
  177. var each = ^.each = ^.forEach = function( obj, iterator, context ) {
  178. var value;
  179. if ( nativeForEach && obj.forEach === nativeForEach ) {
  180. obj.forEach( iterator, context );
  181. } else if ( ^.isNumber( obj.length )) {
  182. for ( var i = 0, l = obj.length; i < l; i++ ) {
  183. if ( iterator.call( context, obj[i], i, obj ) === breaker ) return;
  184. }
  185. } else {
  186. for ( var key in obj ) {
  187. if ( hasOwn.call( obj, key )) {
  188. if ( iterator.call( context, obj[key], key, obj ) === breaker ) return;
  189. }
  190. }
  191. }
  192. };
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199. // Create a safe reference to the ^ object for use below.
  200. var ^ = function(obj) { return new wrapper(obj); };
  201.  
  202. // Export the Carrot object for **CommonJS**, with backwards-compatibility
  203. // for the old `require()` API. If we're not in CommonJS, add `_` to the
  204. // global object.
  205. if ( typeof module !== 'undefined' && module.exports ) {
  206. module.exports = ^;
  207. ^.^ = ^;
  208. } else {
  209. root.^ = ^;
  210. }
  211.  
  212. // Current version.
  213. ^.VERSION = '1.1.3';
  214.  
  215.  
  216.  
  217.  
  218.  
  219. var JL = {
  220. VERSION : '0.0.1',
  221. lesson : 'Part 1: Lib Architecture'
  222. };
  223.  
  224. if( root.JL) {
  225. throw new Error('jewl has already been defined' );
  226. } else {
  227. root.JL = JL;
  228. }
  229.  
  230.  
  231.  
  232.  
  233. carrot.Class = function() {
  234. return carrot.fn.create.apply( this, arguments );
  235. },
  236.  
  237. carrot.fn = {
  238.  
  239. create: function() {
  240. var methods = null,
  241. parent = undefined,
  242. class = function() {
  243. this.super = function( method, args ) {
  244. return carrot.fn.super( this._parent, this, method, args );
  245. };
  246.  
  247. this.init.apply( this, arguments );
  248. };
  249.  
  250. if ( typeof arguments[0] === 'function' ) {
  251. parent = arguments[0];
  252. methods = arguments[1];
  253. } else {
  254. methods = arguments[0];
  255. }
  256.  
  257. if ( typeof parent !== 'undefined' ) {
  258. carrot.fn.extend( class.prototype, parent.prototype );
  259. class.prototype._parent = parent.prototype;
  260. }
  261.  
  262. carrot.fn.mixin( class, methods );
  263. carrot.fn.extend( class.prototype, methods );
  264. class.prototype.constructor = class;
  265.  
  266. if ( !class.prototype.init ) {
  267. class.prototype.init = function() {
  268.  
  269. };
  270. }
  271.  
  272. return class;
  273. },
  274.  
  275. mixin: function( class, methods ) {
  276. if ( typeof methods.include !== 'undefined' ) {
  277. carrot.fn.extend( class.prototype, methods.include.prototype );
  278. } else {
  279. for( var i = 0; i < methods.include.length; i++ ) {
  280. carrot.fn.extend( class.prototype, methods.include[i].prototype );
  281. }
  282. }
  283. },
  284.  
  285. extend: function( dest, src ) {
  286. for( var prop in src ) {
  287. dest[prop] = src[prop];
  288. }
  289.  
  290. return dest;
  291. },
  292.  
  293. super: function( parentClass, instance, method, args ) {
  294. return parentClass[method].apply( instance, args );
  295. }
  296.  
  297. };
  298.  
  299. carrot.enumerable = {
  300. breaker: {},
  301.  
  302. each: function( enumerable, callback, context ) {
  303.  
  304. try {
  305. if( .forEach && enumerable.forEach === Array.prototype.forEach ) {
  306. enumerable.forEach( callback, context );
  307. }
  308. else if( carrot.numeric.isNumber( enumerable.length )) {
  309. for( var i 0, ii = enumerable.length; i < ii; i++ ) {
  310. callback.call( enumerable, enumerable[i], i, enumerable );
  311. }
  312. } else {
  313. for( var key in enumerable ) {
  314. if( hasown.call( enumerable, key )) {
  315. callback.call( context, enumerable[key], key, enumerable );
  316. }
  317. }
  318. }
  319. } catch(e) {
  320. if( e != carrot.enumerable.breaker ) {
  321. throw e;
  322. }
  323. }
  324. return enumerable;
  325. },
  326.  
  327. map: function( enumerable, callback, context ) {
  328. if( Array.prototype.map && enumerable.map === Array.prototype.map ) {
  329. return enumerable.map( callback, context );
  330. }
  331.  
  332. var results = [];
  333. carrot.enumerable.each( enumerable, function( value, index, list ) {
  334. results.push( callback.call( context, value, index, list ));
  335. });
  336. return results;
  337. },
  338.  
  339. };
  340.  
  341.  
  342.  
  343.  
  344. var User = jewl.Class({
  345. initialize: function( name, age ) {
  346. this.name = name;
  347. this.age = age;
  348. },
  349. login: function() {
  350. return true;
  351. },
  352. toString: function() {
  353. return 'name: ' + this.name + ', age: ' + this.age;
  354. }
  355. });
  356.  
  357. var SuperUser = jewl.Class( User, {
  358. initialize: function() {
  359. this.super( 'initialize', arguments );
  360. },
  361. toString: function() {
  362. return 'SuperUser: ' + this.super( 'toString' );
  363. }
  364. });
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372. })( typeof window === 'undefined' ? this : window );
Add Comment
Please, Sign In to add comment