PageRenderTime 29ms CodeModel.GetById 17ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/mod_base/lib/js/modules/z.feedback.js

https://code.google.com/p/zotonic/
JavaScript | 95 lines | 65 code | 6 blank | 24 comment | 21 complexity | d90aca46570cb9e4a6099e1887a9b8a1 MD5 | raw file
 1/* feedback js
 2----------------------------------------------------------
 3
 4@package:	Zotonic 2011	
 5@Author:	Marc Worrell <marc@worrell.nl>
 6
 7Copyright 2011 Marc Worrell
 8
 9Licensed under the Apache License, Version 2.0 (the "License");
10you may not use this file except in compliance with the License.
11You may obtain a copy of the License at
12 
13http://www.apache.org/licenses/LICENSE-2.0
14 
15Unless required by applicable law or agreed to in writing, software
16distributed under the License is distributed on an "AS IS" BASIS,
17WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18See the License for the specific language governing permissions and
19limitations under the License.
20
21---------------------------------------------------------- */
22
23$.widget("ui.feedback", 
24{
25	_init: function() 
26	{
27		var self = this;
28		z_ensure_id(this.element);
29		$('#'+this.options.trigger)
30			.bind('keyup', function() { self.update(); })
31			.bind('change', function() { self.update(); });
32	},
33	
34	update: function() 
35	{
36		if (this.input_updater != undefined)
37		{
38			clearInterval(this.input_updater);
39			this.input_updater = undefined;
40		}
41		
42		var self = this;
43		this.input_updater = setInterval(function()
44		{
45			// Fetch the update
46			var args = [];
47			var trigger = $('#'+self.options.trigger);
48			if (trigger.length == 0) {
49			    clearInterval(self.input_updater);
50			    self.input_updater = undefined;
51			} else {
52			    if (trigger.prop('tagName').toLowerCase() == 'form') {
53    				args = trigger.formToArray();
54    			} else {
55    				var v = $.fieldValue(trigger, true);
56    				if (v && v.constructor == Array) {
57    					for(var j=0, jmax=v.length; j < jmax; j++)
58    						args.push({name: "triggervalue", value: v[j]});
59    				}
60    				else if (v !== null && typeof v != 'undefined')
61    					args.push({name: "triggervalue", value: v});
62    			}
63
64    			// Stop when there is an 'in-flight' update with the same args
65    			if (self.element.hasClass('loading')) {
66    				if (self.last_args == $.param(args)) {
67    					clearInterval(self.input_updater);
68    					self.input_updater = undefined;
69    				}
70    			} else {
71    				self.element.addClass('loading');
72    				self.last_args = $.param(args);
73    				clearInterval(self.input_updater);
74
75                    // Form changed, post it to the server
76    				var notify_args = {};
77    				for (var i=0; i<args.length; i++) {
78    				    notify_args[args[i].name] = args[i].value;
79    				}
80    				notify_args.z_trigger_id = self.options.trigger;
81    				notify_args.z_target_id  = $(self.element).attr('id');
82    				notify_args.z_delegate   = self.options.delegate;
83    				z_notify("feedback", notify_args);
84    			}
85    		}
86		}, self.options.timeout);
87	}
88});
89
90$.ui.feedback.defaults = {
91	delegate: undefined,
92	trigger: undefined,
93	template: undefined,
94	timeout: 400
95}