PageRenderTime 237ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/core/admin/theme/js/widgets.js

http://snowcms.googlecode.com/
JavaScript | 300 lines | 239 code | 29 blank | 32 comment | 28 complexity | d8ad405b77e11be7b5cc5f849e1c6a34 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. /*
  2. Class: Widgets
  3. */
  4. function Widgets()
  5. {
  6. }
  7. // Variable: areas
  8. Widgets.prototype.areas = {};
  9. // Variable: moving
  10. Widgets.prototype.moving = null;
  11. // Function: AddArea
  12. Widgets.prototype.AddArea = function(areaId, currentWidgets)
  13. {
  14. this.areas[areaId] = currentWidgets;
  15. };
  16. // Function: Save
  17. Widgets.prototype.Save = function(element, widgetClass, widgetId)
  18. {
  19. $('#widget_save-' + widgetId).attr('disabled', 'disabled');
  20. $.ajax({
  21. 'type': 'POST',
  22. 'cache': false,
  23. 'data': 'request_type=ajax&save=true&widget_id=' + encodeURIComponent(widgetId) + '&' + (Widgets.CollectOptions(element).join('&')) + '&sid=' + session_id,
  24. 'url': baseurl + '/index.php?action=admin&sa=themes&section=widgets',
  25. 'dataType': 'JSON',
  26. 'success': function(result, status, xhr)
  27. {
  28. if(result['error'])
  29. {
  30. alert(result['error']);
  31. }
  32. if(result['widget_id'] >= 0)
  33. {
  34. $('#widget_save-' + result['widget_id']).attr('disabled', false);
  35. }
  36. },
  37. });
  38. return false;
  39. };
  40. Widgets.prototype.CollectOptions = function(element)
  41. {
  42. var children = $(element).children();
  43. var options = [];
  44. for(var index = 0; index < $(element).children().length; index++)
  45. {
  46. if(typeof $(children[index]).attr('name') != 'undefined')
  47. {
  48. options.push(encodeURIComponent($(children[index]).attr('name')) + '=' + encodeURIComponent($(children[index]).val()));
  49. }
  50. else if($(children[index]).children().length > 0)
  51. {
  52. var childOptions = Widgets.CollectOptions(children[index]);
  53. for(var j in childOptions)
  54. {
  55. options.push(childOptions[j]);
  56. }
  57. }
  58. }
  59. return options;
  60. };
  61. // Function: Delete
  62. Widgets.prototype.Delete = function(element, widgetId)
  63. {
  64. if(confirm('Do you really want to delete this widget?'))
  65. {
  66. $.ajax({
  67. 'type': 'POST',
  68. 'cache': false,
  69. 'data': 'request_type=ajax&delete=true&widget_id=' + encodeURIComponent(widgetId) + '&sid=' + session_id,
  70. 'url': baseurl + '/index.php?action=admin&sa=themes&section=widgets',
  71. 'dataType': 'JSON',
  72. 'success': function(result, status, xhr)
  73. {
  74. if(result['error'])
  75. {
  76. alert(result['error']);
  77. }
  78. else
  79. {
  80. // It has been removed from the list of widgets, so now we will
  81. // remove it from the page, along with the box that allows you
  82. // to move another widget under it.
  83. $('#widget_id-' + result['widgetId']).remove();
  84. $('#moveHere_' + result['widgetId']).remove();
  85. }
  86. },
  87. });
  88. }
  89. };
  90. // Function: Expand
  91. Widgets.prototype.Expand = function(img, widgetId)
  92. {
  93. if($('#options_' + widgetId).css('display') == 'block')
  94. {
  95. $('#options_' + widgetId).parent().removeClass('widget-no-radius');
  96. $('#options_' + widgetId).parent().addClass('widget-radius');
  97. $('#options_' + widgetId).css('display', 'none');
  98. $(img).attr('title', widget_expand);
  99. $(img).attr('src', themeurl + '/style/images/expand-double-arrow.png');
  100. $(img).mouseover(function()
  101. {
  102. this.src = themeurl + '/style/images/expand-double-arrow.png';
  103. });
  104. $(img).mouseout(function()
  105. {
  106. this.src = themeurl + '/style/images/expand-arrow.png';
  107. });
  108. }
  109. else
  110. {
  111. $('#options_' + widgetId).parent().addClass('widget-radius');
  112. $('#options_' + widgetId).parent().removeClass('widget-no-radius');
  113. $('#options_' + widgetId).css('display', 'block');
  114. $(img).attr('title', widget_collapse);
  115. $(img).attr('src', themeurl + '/style/images/collapse-double-arrow.png');
  116. $(img).mouseover(function()
  117. {
  118. this.src = themeurl + '/style/images/collapse-double-arrow.png';
  119. });
  120. $(img).mouseout(function()
  121. {
  122. this.src = themeurl + '/style/images/collapse-arrow.png';
  123. });
  124. }
  125. };
  126. // Function: StartMove
  127. Widgets.prototype.StartMove = function(element, widgetId)
  128. {
  129. // Moving something else? We can only do one at a time!
  130. if(this.moving != null)
  131. {
  132. if(typeof this.moving == 'string')
  133. {
  134. this.StopPlace();
  135. }
  136. else
  137. {
  138. this.StopMove();
  139. }
  140. }
  141. $('.move-selected-widget').css('display', 'block');
  142. $('p', '.move-selected-widget').html(widgets_moveHere);
  143. // We're moving an existing widget.
  144. this.moving = widgetId;
  145. };
  146. // Function: StartPlace
  147. Widgets.prototype.StartPlace = function(element, widgetClass)
  148. {
  149. // If we're moving something else, we should stop that.
  150. if(this.moving != null)
  151. {
  152. if(typeof this.moving == 'string')
  153. {
  154. this.StopPlace();
  155. }
  156. else
  157. {
  158. this.StopMove();
  159. }
  160. }
  161. // Show all the locations where the widget can be added.
  162. $('.move-selected-widget').css('display', 'block');
  163. $('p', '.move-selected-widget').html(widgets_moveHere);
  164. // We're moving this widget, now -- well, adding it.
  165. this.moving = widgetClass;
  166. };
  167. // Function: Move
  168. Widgets.prototype.MoveHere = function(element, areaId, after)
  169. {
  170. // We don't need to show the places that the widget can be moved anymore.
  171. $('.move-selected-widget').css('display', 'block');
  172. // Well, except the place it is being moved -- we will show a little
  173. // message.
  174. $('p', element).text(widgets_pleaseWait);
  175. // If this is a widget that is being added for the first time, then we
  176. // have to do something different than if we're moving an already set up
  177. // widget.
  178. if(typeof this.moving == 'string')
  179. {
  180. $.ajax({
  181. 'type': 'POST',
  182. 'cache': false,
  183. 'data': 'request_type=ajax&move=false&widget_class=' + this.moving + '&area_id=' + encodeURIComponent(areaId) + '&after=' + encodeURIComponent(after) + '&sid=' + session_id,
  184. 'url': baseurl + '/index.php?action=admin&sa=themes&section=widgets',
  185. 'dataType': 'JSON',
  186. 'success': function(result, status, xhr)
  187. {
  188. if(result['error'])
  189. {
  190. Widgets.StopPlace();
  191. alert(result['error']);
  192. }
  193. else
  194. {
  195. Widgets.StopPlace();
  196. $('#moveHere_' + result['after']).after('<div class="widget-container" id="widget_id-' + result['widget_info']['id'] + '">' +
  197. '<p class="widget-name" title="' + widget_moveThis + '" onclick="Widgets.StartMove(this, ' + result['widget_info']['id'] + ');">' + result['widget_info']['title'] + '</p>' +
  198. '<p class="widget-expand"><img src="' + themeurl + '/style/images/collapse-arrow.png" alt="" title="' + widget_collapse + '" onclick="Widgets.Expand(this, ' + result['widget_info']['id'] + ');" onmouseover="this.src = \'' + themeurl + '/style/images/collapse-double-arrow.png\';" onmouseout="this.src = \'' + themeurl + '/style/images/collapse-arrow.png\';" /></p>' +
  199. '<div class="break">' +
  200. '</div>' +
  201. '<div id="options_' + result['widget_info']['id'] + '" class="widget-options" style="display: block;">' +
  202. result['widget_info']['form'] +
  203. '</div>' +
  204. '</div>' +
  205. '<div id="moveHere_' + result['widget_info']['id'] + '" class="move-selected-widget" onclick="Widgets.MoveHere(this, \'' + result['widget_area'] + '\', ' + result['widget_info']['id'] + ');">' +
  206. '<p>' + widgets_moveHere + '</p>' +
  207. '</div>');
  208. }
  209. },
  210. });
  211. }
  212. else
  213. {
  214. $.ajax({
  215. 'type': 'POST',
  216. 'cache': false,
  217. 'data': 'request_type=ajax&move=true&widget_id=' + this.moving + '&area_id=' + encodeURIComponent(areaId) + '&after=' + encodeURIComponent(after) + '&sid=' + session_id,
  218. 'url': baseurl + '/index.php?action=admin&sa=themes&section=widgets',
  219. 'dataType': 'JSON',
  220. 'success': function(result, status, xhr)
  221. {
  222. if(result['error'])
  223. {
  224. Widgets.StopMove();
  225. alert(result['error']);
  226. }
  227. // Did the user actually move it, or was it in the same place
  228. // in the end?
  229. else if(result['moved'])
  230. {
  231. Widgets.StopMove();
  232. // This will be added back...
  233. $('#moveHere_' + result['widget_info']['id']).remove();
  234. $('#widget_id-' + result['widget_info']['id']).remove();
  235. $('#moveHere_' + result['after']).after('<div class="widget-container" id="widget_id-' + result['widget_info']['id'] + '">' +
  236. '<p class="widget-name" title="' + widget_moveThis + '" onclick="Widgets.StartMove(this, ' + result['widget_info']['id'] + ');">' + result['widget_info']['title'] + '</p>' +
  237. '<p class="widget-expand"><img src="' + themeurl + '/style/images/collapse-arrow.png" alt="" title="' + widget_collapse + '" onclick="Widgets.Expand(this, ' + result['widget_info']['id'] + ');" onmouseover="this.src = \'' + themeurl + '/style/images/collapse-double-arrow.png\';" onmouseout="this.src = \'' + themeurl + '/style/images/collapse-arrow.png\';" /></p>' +
  238. '<div class="break">' +
  239. '</div>' +
  240. '<div id="options_' + result['widget_info']['id'] + '" class="widget-options" style="display: block;">' +
  241. result['widget_info']['form'] +
  242. '</div>' +
  243. '</div>' +
  244. '<div id="moveHere_' + result['widget_info']['id'] + '" class="move-selected-widget" onclick="Widgets.MoveHere(this, \'' + result['widget_area'] + '\', ' + result['widget_info']['id'] + ');">' +
  245. '<p>' + widgets_moveHere + '</p>' +
  246. '</div>');
  247. }
  248. else
  249. {
  250. Widgets.StopMove();
  251. }
  252. },
  253. });
  254. }
  255. };
  256. // Function: StopPlace
  257. Widgets.prototype.StopPlace = function()
  258. {
  259. if(this.moving != null)
  260. {
  261. $('.move-selected-widget').css('display', 'none');
  262. this.moving = null;
  263. }
  264. };
  265. // Function: StopMove
  266. Widgets.prototype.StopMove = function()
  267. {
  268. // They do the same thing >.>
  269. Widgets.StopPlace();
  270. };
  271. var Widgets = new Widgets();