iainheng
2/23/2017 - 5:58 AM

jQuery OOP Class: This allows you to easily create a class in Jquery.

jQuery OOP Class: This allows you to easily create a class in Jquery.

/*!
 * Simple JavaScript Inheritance
 * By John Resig http://ejohn.org/
 * MIT Licensed.
 *
 * Extended by Jonathon Byrd to include function hooks
 * https://gist.github.com/Jonathonbyrd/724083
 *
 * Don't forget your Shims!
 * https://github.com/kriskowal/es5-shim/blob/master
 */
(function(){var a=false,b=/xyz/.test(function(){xyz})?/\b_super\b/:/.*/;function c(h){var f=h,e={before:[],after:[]};function g(){var j=[].slice.call(arguments,0),k=0,l;for(k=0;!!e.before[k];k+=1){l=e.before[k];l.apply(this,j)}var m=f.apply(this,arguments);for(k=0;!!e.after[k];k++){l=e.after[k];l.apply(this,j)}return m}g.bind=function(i){if(typeof this!=="function"){throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable")}var m=Array.prototype.slice.call(arguments,1),l=this,j=function(){},k=function(){return l.apply(this instanceof j&&i?this:i,m.concat(Array.prototype.slice.call(arguments)))};j.prototype=this.prototype;k.prototype=new j();return k};g.addHook=function(j,i){if(e[j] instanceof Array){e[j].push(i)}else{throw (function(){var k=new Error("Invalid hook type");k.expected=Object.keys(e);k.got=j;return k}())}};return g}function d(f,e){if(typeof jQuery=="undefined"){return f}return jQuery.extend(true,e,f)}this._Class=function(){};_Class.extend=function(i){var g=this.prototype;a=true;var f=new this();a=false;for(var e in i){f[e]=typeof i[e]=="function"&&typeof g[e]=="function"&&b.test(i[e])?(function(j,k){return function(){var m=this._super;this._super=g[j];var l=k.apply(this,arguments);this._super=m;return l}})(e,i[e]):(typeof i[e]=="function"?c(i[e]):(typeof i[e]=="object"&&typeof g[e]=="object"?d(i[e],g[e]):i[e]))}function h(){if(!a&&this.init){this.init.apply(this,arguments)}}h.prototype=f;h.constructor=h;h.extend=c(arguments.callee);return h}})();Class=_Class.extend({defaults:{container:null},data:{},init:function(a){this.setOptions(a)},setOptions:function(c){var b=new Object();for(var a in this.defaults){b[a]=this.defaults[a]}for(var a in c){b[a]=c[a]}this.data=b;return this},set:function(b,a){this.data[b]=a},get:function(a,b){return typeof this.data[a]!="undefined"?this.data[a]:b},element:function(){return this.get("container")},rawElement:function(){return this.element().context},uid:function(){return this._uid().replace(/[/,'').replace(/]/,"").replace(/:/,"")},_uid:function(){if(this.element().attr("id")){return this.element().attr("id")}if(this.element().attr("name")){return this.element().attr("name")}var a="";if(this.element().context.form.id){a=this.element().context.form.id}if(!a&&this.element().context.form.name){a=this.element().context.form.name}return a+this.element().index()}});
/*!
 * Simple JavaScript Inheritance
 * By John Resig http://ejohn.org/
 * MIT Licensed.
 *
 * Extended by Jonathon Byrd to include function hooks
 * https://gist.github.com/Jonathonbyrd/724083
 *
 * Don't forget your Shims!
 * https://github.com/kriskowal/es5-shim/blob/master
 */
(function(){
  var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;

  // creating hooks
  function hookable(fn) {
	  var ifn = fn,
		  hooks = {
			  before : [],
			  after : []
		  };
	  
	  function hookableFunction() {
		  var args = [].slice.call(arguments, 0),
			  i = 0,
			  fn;
		  for (i = 0; !!hooks.before[i]; i += 1) {
			  fn = hooks.before[i];
			  fn.apply(this, args);
		  }
		  var r = ifn.apply(this, arguments);
		  for (i = 0; !!hooks.after[i]; i++) {
			  fn = hooks.after[i];
			  fn.apply(this, args);
		  }
		  return r;
	  }
	  
	  hookableFunction.bind = function (oThis) {
	    if (typeof this !== "function") {
	      // closest thing possible to the ECMAScript 5 internal IsCallable function
	      throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
	    }
	 
	    var aArgs = Array.prototype.slice.call(arguments, 1), 
	        fToBind = this, 
	        fNOP = function () {},
	        fBound = function () {
	          return fToBind.apply(this instanceof fNOP && oThis
	                                 ? this
	                                 : oThis,
	                               aArgs.concat(Array.prototype.slice.call(arguments)));
	        };
	 
	    fNOP.prototype = this.prototype;
	    fBound.prototype = new fNOP();
	 
	    return fBound;
	  };
	  
	  hookableFunction.addHook = function (type, fn) {
		  if (hooks[type] instanceof Array) {
			  hooks[type].push(fn);
		  } else {
			  throw (function () {
				  var e = new Error("Invalid hook type");
				  e.expected = Object.keys(hooks);
				  e.got = type;
				  return e;
			  }());
		  }
	  };
	  
	  return hookableFunction;
  }
  
  // for extending objects
  function extend(o,oo) {
    if (typeof jQuery == 'undefined')
    return o;
    return jQuery.extend(true,oo,o);
  }

  // The base _Class implementation (does nothing)
  this._Class = function(){};

  // Create a new _Class that inherits from this _Class
  _Class.extend = function(prop) {
	var _super = this.prototype;
	
	// Instantiate a base _Class (but only create the instance,
	// don't run the init constructor)
	initializing = true;
	var prototype = new this();
	initializing = false;
	
	// Copy the properties over onto the new prototype
	for (var name in prop) {
	  // Check if we're overwriting an existing function
	  prototype[name] = 
		typeof prop[name] == "function" 
        && typeof _super[name] == "function" 
        && fnTest.test(prop[name])
        // this is how we override a function
		? (function(name, fn){
			  return function() {
				var tmp = this._super;
				
				// Add a new ._super() method that is the same method
				// but on the super-class
				this._super = _super[name];
				
				// The method only need to be bound temporarily, so we
				// remove it when we're done executing
				var ret = fn.apply(this, arguments);
				this._super = tmp;
				
				return ret;
			  };
		  })(name, prop[name])
        // this is how we create a function
		: (typeof prop[name] == 'function' ?hookable(prop[name]) 
        
          // direct overload of a property
          :(typeof prop[name] == 'object' 
              && typeof _super[name] == 'object' 
              // extend an object
              ?extend(prop[name],_super[name]) :prop[name]));
	}
	
	// The dummy class constructor
	function _Class() {
	  // All construction is actually done in the init method
	  if ( !initializing && this.init )
		this.init.apply(this, arguments);
	}
	
	// Populate our constructed prototype object
	_Class.prototype = prototype;
	
	// Enforce the constructor to be what we expect
	_Class.constructor = _Class;
	
	// And make this class extendable
	_Class.extend = hookable(arguments.callee);
	
	return _Class;
  };
})();

Class = _Class.extend({
  defaults:{
	container : null
  },
  data:{},
  init:function(o) {
	this.setOptions(o);
  },
  setOptions:function(o) {
	var obj = new Object();
	for (var attrname in this.defaults) { obj[attrname] = this.defaults[attrname]; }
	for (var attrname in o) { obj[attrname] = o[attrname]; }
	this.data = obj;
	
	return this;
  },
  set:function(p, value) {
	this.data[p] = value;
  },
  get:function(p, d) {
	return typeof this.data[p] != 'undefined' ?this.data[p] :d;
  },
  element:function() {
	return this.get('container');
  },
  rawElement:function() {
	return this.element().context;
  },
  uid:function(){
	return this._uid().replace(/[/,'').replace(/]/,'').replace(/:/,''); 
  },
  _uid:function() {
    if (this.element().attr('id'))
		return this.element().attr('id');

    if (this.element().attr('name'))
		return this.element().attr('name');
	
    var form = '';
    if (this.element().context.form.id)
    	form = this.element().context.form.id;

    if (!form && this.element().context.form.name)
    	form = this.element().context.form.name;
    
    return form + this.element().index();
  }
});
var StoreLocator;
(function(j){
/*!
 * Simple JavaScript Inheritance
 * By John Resig http://ejohn.org/
 * MIT Licensed.
 *
 * Extended by Jonathon Byrd to include function hooks
 * https://gist.github.com/Jonathonbyrd/724083
 *
 * Don't forget your Shims!
 * https://github.com/kriskowal/es5-shim/blob/master
 */
(function(){var a=false,b=/xyz/.test(function(){xyz})?/\b_super\b/:/.*/;function c(h){var f=h,e={before:[],after:[]};function g(){var j=[].slice.call(arguments,0),k=0,l;for(k=0;!!e.before[k];k+=1){l=e.before[k];l.apply(this,j)}var m=f.apply(this,arguments);for(k=0;!!e.after[k];k++){l=e.after[k];l.apply(this,j)}return m}g.bind=function(i){if(typeof this!=="function"){throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable")}var m=Array.prototype.slice.call(arguments,1),l=this,j=function(){},k=function(){return l.apply(this instanceof j&&i?this:i,m.concat(Array.prototype.slice.call(arguments)))};j.prototype=this.prototype;k.prototype=new j();return k};g.addHook=function(j,i){if(e[j] instanceof Array){e[j].push(i)}else{throw (function(){var k=new Error("Invalid hook type");k.expected=Object.keys(e);k.got=j;return k}())}};return g}function d(f,e){if(typeof jQuery=="undefined"){return f}return jQuery.extend(true,e,f)}this._Class=function(){};_Class.extend=function(i){var g=this.prototype;a=true;var f=new this();a=false;for(var e in i){f[e]=typeof i[e]=="function"&&typeof g[e]=="function"&&b.test(i[e])?(function(j,k){return function(){var m=this._super;this._super=g[j];var l=k.apply(this,arguments);this._super=m;return l}})(e,i[e]):(typeof i[e]=="function"?c(i[e]):(typeof i[e]=="object"&&typeof g[e]=="object"?d(i[e],g[e]):i[e]))}function h(){if(!a&&this.init){this.init.apply(this,arguments)}}h.prototype=f;h.constructor=h;h.extend=c(arguments.callee);return h}})();Class=_Class.extend({defaults:{container:null},data:{},init:function(a){this.setOptions(a)},setOptions:function(c){var b=new Object();for(var a in this.defaults){b[a]=this.defaults[a]}for(var a in c){b[a]=c[a]}this.data=b;return this},set:function(b,a){this.data[b]=a},get:function(a,b){return typeof this.data[a]!="undefined"?this.data[a]:b},element:function(){return this.get("container")},rawElement:function(){return this.element().context},uid:function(){return this._uid().replace(/[/,'').replace(/]/,"").replace(/:/,"")},_uid:function(){if(this.element().attr("id")){return this.element().attr("id")}if(this.element().attr("name")){return this.element().attr("name")}var a="";if(this.element().context.form.id){a=this.element().context.form.id}if(!a&&this.element().context.form.name){a=this.element().context.form.name}return a+this.element().index()}});
StoreLocator = Class.extend({
	defaults : {
		container : null,
		test : 'defaultvalue'
	},
	
	// Initializing
	init: function(o)
	{
		this.setOptions(o);
		
		this.doSomething();
	},

	doSomething: function()
	{
		console.log(this.get('test'));
	}
});
j.fn.StoreLocator = function(o){
	// initializing
	var args = arguments;
	var o = o || {'container':''};
	return this.each(function(){
		// load the saved object
		var api = j.data(this, 'StoreLocator');
		// create and save the object if it does not exist
		if (!api) {
			o.container = j(this);
			api = new StoreLocator(o);
			j.data(this, 'StoreLocator', api);
		}
                if (typeof api[o] == 'function') {
                        if (args[0] == o) delete args[0];
                        api[o].bind(api);
                        var parameters = Array.prototype.slice.call(args, 1);
                        return api[o].apply(api,parameters);
               }
		return api;
	});
};
})(jQuery);

// EXAMPLES
jQuery('#locator').StoreLocator({
	'test' : 'thisisatest'
});
var locator = jQuery('#locator').StoreLocator('element');

jQuery(document).ready(function() {
        // initialize
	var sl = new StoreLocator();
	
        // adding a hook to the function
        sl.doSomething.addHook('after', function(){console.log('test');});
 
        // trigger function
	sl.doSomething();
});



var AjaxCart;
(function(j){
	var AjaxCart = RClass.extend({
		add: function()
		{
			console.log(arguments);

			return this;
		}
	});
	j.AjaxCart = function(){
		// initializing
		var args = j.map(arguments, function(k, v) {
		    return [k];
		});
		// load the saved object
		var api = j.data(window, 'AjaxCart');
		// create and save the object if it does not exist
		if (!api) {
			api = new AjaxCart();
			j.data(window, 'AjaxCart', api);
		}
		// if this is a method call on an existing object, apply it
		if (typeof args[0] == 'string')
		{
			var funcName = args.splice(0,1);
			if (typeof api[funcName] == 'function')
				return api[funcName].apply( api, args );
		}
		return api;
	};
})(jQuery);

jQuery.AjaxCart('add', 'first arg', 'second arg');
//["first arg", "second arg"]