Guest User

Untitled

a guest
Feb 18th, 2018
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   Biblioteca para criação de classes
  3.   Autor: Wilson Pinto Júnior <wilsonpjunior@gmail.com>
  4.   18 de setembro de 2011
  5. */
  6.  
  7. $.classes = {};
  8.  
  9. $.registerClass = function (name, cls) {
  10.     /*
  11.       Registra a classe no registro
  12.     */
  13.     $.classes[name] = cls;
  14. }
  15.  
  16. $.superClass = function (obj) {
  17.     /*
  18.       funciona como o super do python
  19.       retornando os prototype da classe superior
  20.     */
  21.     return obj._parentClass.prototype;
  22. }
  23.  
  24. $.registerInitClass = function (cls, attr) {
  25.     /*
  26.       registra a classe como plugin
  27.       cls: classe em si
  28.       attr: como ela será armazenada no elemento
  29.      */
  30.     $.fn[attr] = function (options) {
  31.         var element = $(this);
  32.         var p = element.data(attr);
  33.  
  34.         if (p) return p;
  35.  
  36.     p = new cls(this, options);
  37.         element.data(attr, p);
  38.         return p;
  39.     }
  40. }
  41.  
  42. $.inherits = function (child, parent, options) {
  43.     /*
  44.       Gera herança de um objeto
  45.       child: classe filha
  46.       parent: classe pai ou nome (se estiver registrada)
  47.       options: opções que serão rescritas na classe filha
  48.     */
  49.  
  50.     if (parent.constructor == String)
  51.         parent = $.classes[parent];
  52.    
  53.     var _super = parent.prototype;
  54.     child.prototype = {};
  55.    
  56.     //copia os attributos da classe pai para a filha
  57.     for (var k in _super) {
  58.         child.prototype[k] = _super[k];
  59.     }
  60.  
  61.     for (var k in options||{}) {
  62.         if (_super[k] && typeof(_super[k] == 'function')) {
  63.             child.prototype[k] = (function(name, fn){
  64.                 return function() {
  65.                     var tmp = this._super;
  66.            
  67.                     // Add a new ._super() method that is the same method
  68.                     // but on the super-class
  69.                     this._super = _super[name];
  70.                    
  71.                     // The method only need to be bound temporarily, so we
  72.                     // remove it when we're done executing
  73.                     var ret = fn.apply(this, arguments);        
  74.                     this._super = tmp;
  75.            
  76.                     return ret;
  77.                 };
  78.             })(k, options[k]);
  79.         } else {
  80.             child.prototype[k] = options[k];
  81.         }
  82.     }
  83.  
  84.     child.prototype._parentClass = parent;
  85.  
  86.     //super method for constructor
  87.     child.prototype._super = function () {
  88.         var tmp = this._super;
  89.        
  90.         if (parent.prototype._super)
  91.             this._super = parent.prototype._super;
  92.         else
  93.             this._super = null;
  94.  
  95.         var ret = parent.apply(this, arguments)
  96.  
  97.         this._super = tmp;
  98.         return ret;
  99.     }
  100.     child.prototype.constructor = child;
  101. }
Add Comment
Please, Sign In to add comment