// // Requirements // $; $.template; $.toJSON; js_commons = 'defined'; // // JavaScript Extension // Array.prototype.size = function () { return this.length; }; function Hash(hash) { hash = hash || {}; var _this = this; $.each(hash, function(key){_this[key] = this}); }; Hash.prototype.size = function () { var l = -1; for (var k in this) { l++; } return l; }; inspect = function(data){ console.dir(data); }; // // jQuery Extension // $.extend({ infoMessage: function(message){ try{ this.jGrowl(message); }catch(e){console.log(e)}; }, errorMessage: function(message){ try{ this.jGrowl(message); }catch(e){console.log(e)}; }, checkForMessages: function(json){ if(json['info']) $.infoMessage(json['info']); if(json['error']) $.errorMessage(json['error']); } }); $.fn.extend({ serializeObject: function(){ var o = {}; var a = this.serializeArray(); $.each(a, function() { if (o[this.name]) { if (!o[this.name].push) { o[this.name] = [o[this.name]]; } o[this.name].push(this.value || ''); } else { o[this.name] = this.value || ''; } }); return o; }, identify: function() { var id = $(this).attr('id'); if(!id){ var i = 0; do { i++; id = 'auto_id_' + i; } while($('#' + id).length > 0); $(this).attr('id', id); } return id; }, first: function(){ return $((this.size() > 0) ? this[0] : this) }, last: function(){ return $((this.size() > 0) ? this[this.size() - 1] : this) }, blank: function(){ return this.size() < 1 } }); // // jQuery Template // $.template.regx.standard = /\#\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g; // // Behaviour Initializers // $.extend({ initializers: [], add_initializer: function(name, query, initializer){ this.initializers.push({name: name + '_init', query: query, initializer: initializer}); }, initialize: function() { $($.initializers).each(function() { var _this = this; $(this.query).each(function(){ var e = $(this); if(!e.data(_this.name)){ e.data(_this.name, true); _this.initializer.apply($(this)); } }); }); } }); $(function(){ $.initialize(); }); $(document).ajaxSuccess(function(){ $.initialize(); }); // // link_to // $.fn.extend({ link_to: function(options) { options = $.extend({url: $(this).attr('href'), method: 'get'}, options); options.method = options.method.toLowerCase(); // if(options.method == 'put' || options.method == 'delete') options.method = 'post'; try{ if(options.ajax){ var data = { _method: options.method, target: $(this).identify() }; if(options.method != 'get') data.authenticity_token = $.authenticity_token; $.ajax({ data: $.param(data), dataType: 'script', type: options.method, url: options.url }); }else{ var url = $(this).attr('href'); var form = $("