Sha256: 3e27b5038965ccf4e44a06cb2c9639ab71bc4606d556969bc2ad043c195d3efd

Contents?: true

Size: 1.55 KB

Versions: 47

Compression:

Stored size: 1.55 KB

Contents

/**
 * Observer - Observe formelements for changes
 *
 * - Additional code from clientside.cnet.com
 *
 * @version		1.1
 *
 * @license		MIT-style license
 * @author		Harald Kirschner <mail [at] digitarald.de>
 * @copyright	Author
 */
var Observer = new Class({

	Implements: [Options, Events],

	options: {
		periodical: false,
		delay: 1000
	},

	initialize: function(el, onFired, options){
		this.element = $(el) || $$(el);
		this.addEvent('onFired', onFired);
		this.setOptions(options);
		this.bound = this.changed.bind(this);
		this.resume();
	},

	changed: function() {
		var value = this.element.get('value');
		if ($equals(this.value, value)) return;
		this.clear();
		this.value = value;
		this.timeout = this.onFired.delay(this.options.delay, this);
	},

	setValue: function(value) {
		this.value = value;
		this.element.set('value', value);
		return this.clear();
	},

	onFired: function() {
		this.fireEvent('onFired', [this.value, this.element]);
	},

	clear: function() {
		$clear(this.timeout || null);
		return this;
	},

	pause: function(){
		if (this.timer) $clear(this.timer);
		else this.element.removeEvent('keyup', this.bound);
		return this.clear();
	},

	resume: function(){
		this.value = this.element.get('value');
		if (this.options.periodical) this.timer = this.changed.periodical(this.options.periodical, this);
		else this.element.addEvent('keyup', this.bound);
		return this;
	}

});

var $equals = function(obj1, obj2) {
	return (obj1 == obj2 || JSON.encode(obj1) == JSON.encode(obj2));
};

Version data entries

47 entries across 16 versions & 1 rubygems

Version Path
pupu-0.2.1 examples/rango/media/pupu/autocompleter/javascripts/observer.js
pupu-0.2.1 examples/merb/public/pupu/autocompleter/javascripts/observer.js
pupu-0.2.1 spec/data/public/pupu/autocompleter/javascripts/observer.js
pupu-0.2 examples/merb/public/pupu/autocompleter/javascripts/observer.js
pupu-0.2 examples/rango/media/pupu/autocompleter/javascripts/observer.js
pupu-0.2 spec/data/public/pupu/autocompleter/javascripts/observer.js
pupu-0.1 examples/merb/public/pupu/autocompleter/javascripts/observer.js
pupu-0.1 spec/data/public/pupu/autocompleter/javascripts/observer.js
pupu-0.1 examples/rango/media/pupu/autocompleter/javascripts/observer.js
pupu-0.0.5.5 examples/rango/media/pupu/autocompleter/javascripts/observer.js
pupu-0.0.5.5 spec/data/public/pupu/autocompleter/javascripts/observer.js
pupu-0.0.5.5 examples/merb/public/pupu/autocompleter/javascripts/observer.js
pupu-0.0.5.4 examples/merb/public/pupu/autocompleter/javascripts/observer.js
pupu-0.0.5.4 examples/rango/media/pupu/autocompleter/javascripts/observer.js
pupu-0.0.5.4 spec/data/public/pupu/autocompleter/javascripts/observer.js
pupu-0.0.5.3 spec/data/public/pupu/autocompleter/javascripts/observer.js
pupu-0.0.5.3 examples/merb/public/pupu/autocompleter/javascripts/observer.js
pupu-0.0.5.3 examples/rango/media/pupu/autocompleter/javascripts/observer.js
pupu-0.0.5.2 spec/data/public/pupu/autocompleter/javascripts/observer.js
pupu-0.0.5.2 examples/merb/public/pupu/autocompleter/javascripts/observer.js