PageRenderTime 20ms CodeModel.GetById 9ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/mod_base/lib/js/apps/z.widgetmanager.js

https://code.google.com/p/zotonic/
JavaScript | 185 lines | 147 code | 16 blank | 22 comment | 29 complexity | 2233290e364867ff0f9bdeb60f66caab MD5 | raw file
  1/* Admin widgetManager class
  2----------------------------------------------------------
  3
  4@package:	Zotonic 
  5@Author:	Tim Benniks <tim@timbenniks.nl>
  6@Author:	Marc Worrell <marc@worrell.nl>
  7
  8Copyright 2009-2011 Tim Benniks
  9
 10Licensed under the Apache License, Version 2.0 (the "License");
 11you may not use this file except in compliance with the License.
 12You may obtain a copy of the License at
 13 
 14http://www.apache.org/licenses/LICENSE-2.0
 15 
 16Unless required by applicable law or agreed to in writing, software
 17distributed under the License is distributed on an "AS IS" BASIS,
 18WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 19See the License for the specific language governing permissions and
 20limitations under the License.
 21
 22---------------------------------------------------------- */
 23
 24;(function($)
 25{
 26	$.extend(
 27	{
 28		widgetManager: function(context)
 29		{
 30			context		= context || document.body;
 31			var stack	= [context];
 32
 33			while (stack.length > 0)
 34			{
 35				var objectOptions, defaults, element = stack.pop();
 36				if (element.className) 
 37				{
 38					var objectClass = element.className.match(/do_[a-zA-Z0-9_]+/g);
 39					if (objectClass) 
 40					{
 41						var n = objectClass.length;
 42						for (var i=0; i<n; i++)
 43						{
 44							var functionName = objectClass[i].substring(3);
 45							var defaultsName = functionName;
 46							
 47							if ('dialog' == functionName) functionName = 'show_dialog'; // work around to prevent ui.dialog redefinition
 48
 49							if (typeof $(element)[functionName] == "function")
 50							{
 51								if ($.ui && $.ui[functionName] && $.ui[functionName].defaults)
 52								{
 53									defaults = $.ui[functionName].defaults;
 54								}
 55								else
 56								{
 57									defaults = {}
 58								}
 59								$(element)[functionName]( $.extend({}, defaults, $(element).metadata(defaultsName)) );
 60							}
 61						}
 62					}
 63				}
 64
 65				if (element.childNodes) 
 66				{
 67					for (var i = 0; i< element.childNodes.length; i++)
 68					{
 69						if (element.childNodes[i].nodeType != 3)
 70						{
 71							stack.unshift(element.childNodes[i]);
 72						}
 73					}
 74				}
 75			}
 76		},
 77		
 78		misc: 
 79		{
 80			log: function(obj)
 81			{
 82				if(window.console) 
 83				{
 84					console.log(obj);
 85	
 86					if($.noticeAdd)
 87					{
 88						$.noticeAdd({
 89							text: 'Logging, check firebug: '+obj, 
 90							type: 'notice', 
 91							stay: 0
 92						});
 93					}
 94				} 
 95				else 
 96				{
 97					if($.noticeAdd)
 98					{
 99						$.noticeAdd({
100							text: 'logged: '+obj, 
101							type: 'notice', 
102							stay: 0
103						});
104					}
105					else
106					{
107						alert(obj.toSource());
108					}
109				}
110			},
111			
112			warn: function(text, obj)
113			{
114				obj = obj || '';
115				
116				if(window.console) 
117				{
118					console.warn(text, obj);
119				}
120				
121				if($.noticeAdd)
122				{
123					$.noticeAdd({
124						text: text, 
125						type: 'notice', 
126						stay: 1
127					});
128				}
129			},
130			
131			error: function(text, obj)
132			{
133				obj = obj || '';
134
135				if(window.console) 
136				{
137					console.error(text, obj);
138				}
139				
140				if($.noticeAdd)
141				{
142					$.noticeAdd({
143						text: text, 
144						type: 'error', 
145						stay: 1
146					});
147				}
148			}
149		}
150	});
151	
152	$.fn.metadata = function(functionName)
153	{
154		var elem = this[0];
155		var data_name = 'widget-'+functionName;
156		var data = $(elem).data(data_name);
157		if(data)
158		{
159			return data;
160		}
161		data = elem.getAttribute("data-"+functionName);
162		if(!data)
163		{
164			var m = /{(.*)}/.exec(elem.className);
165			if(m)
166			{
167				data = m[1];
168			}
169			else
170			{
171				data = "";
172			}
173		}
174		data = eval("({" + data.replace(/[\n\r]/g,' ') + "})");
175		$(elem).data(data_name, data);
176		return data;
177	};
178	
179	$.fn.widgetManager = function()
180	{
181		this.each(function() { $.widgetManager(this); });
182		return this;
183	};
184
185})(jQuery);