if (typeof(Class) === 'undefined') { /* Simple Inheritance http://ejohn.org/blog/simple-javascript-inheritance/ */ (function(){ var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/; // 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]) ? (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]) : 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 = arguments.callee; return Class; }; })(); } /* jQuery delayed observer (c) 2007 - Maxime Haineault (max@centdessin.com) Special thanks to Stephen Goguen & Tane Piper. Slight modifications by Elliot Winkler */ if (typeof(jQuery.fn.delayedObserver) === 'undefined') { (function() { var delayedObserverStack = []; var observed; function delayedObserverCallback(stackPos) { observed = delayedObserverStack[stackPos]; if (observed.timer) clearTimeout(observed.timer); observed.timer = setTimeout(function(){ observed.timer = null; observed.callback(observed.obj.val(), observed.obj); }, observed.delay * 1000); observed.oldVal = observed.obj.val(); } // going by // // I think these codes only work when using keyup or keydown function isNonPrintableKey(event) { var code = event.keyCode; return ( event.metaKey || (code >= 9 || code <= 16) || (code >= 27 && code <= 40) || (code >= 91 && code <= 93) || (code >= 112 && code <= 145) ); } jQuery.fn.extend({ delayedObserver:function(delay, callback){ $this = jQuery(this); delayedObserverStack.push({ obj: $this, timer: null, delay: delay, oldVal: $this.val(), callback: callback }); stackPos = delayedObserverStack.length-1; $this.keyup(function(event) { if (isNonPrintableKey(event)) return; observed = delayedObserverStack[stackPos]; if (observed.obj.val() == observed.obj.oldVal) return; else delayedObserverCallback(stackPos); }); } }); })(); } jQuery(document).ready(function() { RecordSelect.document_loaded = true; jQuery(document).on('ajax:before', 'div.record-select * li.record a', function(event) { var link = jQuery(this); if (link) { if (RecordSelect.notify(link) == false) { return false; } else { link.toggleClass("selected"); } } return true; }); }); var RecordSelect = new Object(); RecordSelect.document_loaded = false; RecordSelect.notify = function(item) { var e = item.closest('.record-select-handler'); var onselect = e.get(0).onselect || e.attr('onselect'); if (typeof onselect != 'function') onselect = eval(onselect); if (onselect) { try { var label = jQuery.trim(item.find('label').first().text()); if (!label) label = item.text(); onselect(item.parent().attr('id').substr(2), label, e); } catch(e) { alert(e); } return false; } else return true; } RecordSelect.observe = function(id) { var form = jQuery("#" + id); form.find('input.text-input').delayedObserver(0.35, function() { form.trigger("submit"); }); } RecordSelect.render_page = function(record_select_id, page) { jQuery('#' + record_select_id + ' ol').first().replaceWith(page); }; RecordSelect.Abstract = Class.extend({ /** * obj - the id or element that will anchor the recordselect to the page * url - the url to run the recordselect * options - ??? (check concrete classes) */ init: function(obj, url, options) { if (typeof(obj) == 'string') obj = '#' + obj; this.obj = jQuery(obj); this.url = url; this.options = options; this.container; if (this.options.onchange && typeof this.options.onchange != 'function') { this.options.onchange = eval(this.options.onchange); } if (RecordSelect.document_loaded) { this.onload(); } else { var _this = this; jQuery(document).ready(function() { _this.onload(); }) } }, /** * Finish the setup - IE doesn't like doing certain things before the page loads * --override-- */ onload: function() {}, /** * the onselect event handler - when someone clicks on a record * --override-- */ onselect: function(id, value) { alert(id + ': ' + value); }, /** * opens the recordselect */ open: function() { if (this.is_open()) return; var _this = this; jQuery.rails.fire(_this.obj, 'rs:before'); jQuery.ajax({ url: this.url, //type: "POST", data: _this.obj.data('params'), //dataType: options.ajax_data_type, success: function(data){ _this.container.html(data); _this.show(); jQuery(document.body).mousedown(jQuery.proxy(_this, "onbodyclick")); } }); }, /** * positions and reveals the recordselect */ show: function() { var offset = this.obj.offset(), top = this.obj.height() + offset.top; this.container.show(); this.container.css('left', offset.left); if (top + this.container.height() > jQuery(window).height()) this.container.css('bottom', jQuery(window).height() - offset.top); else this.container.css('top', top); if (this._use_iframe_mask()) { this.container.after('