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