PageRenderTime 65ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/system/cms/modules/widgets/js/widgets.js

https://gitlab.com/sheldonels/pyrocms
JavaScript | 462 lines | 337 code | 93 blank | 32 comment | 21 complexity | c89b882febd535c8bc08637501679bef MD5 | raw file
  1. jQuery(function($){
  2. $.extend($.ui.accordion.prototype, {
  3. refresh: function(){
  4. this.destroy();
  5. this.widget().accordion(this.options)
  6. return this;
  7. }
  8. });
  9. pyro.cache.widget_forms = {
  10. add :{},
  11. edit :{}
  12. }
  13. pyro.widgets = {
  14. $areas : null,
  15. $boxes : null,
  16. $instances : null,
  17. $container : null,
  18. selector : {
  19. container : 'null',
  20. instances : 'null'
  21. },
  22. ui_options: {
  23. // Widget Areas
  24. accordion: {
  25. collapsible : true,
  26. header : '> section > header',
  27. autoHeight : false,
  28. clearStyle : true
  29. },
  30. // Widget Instances List
  31. sortable: {
  32. cancel : '.no-sortable, a, :input, option',
  33. placeholder : 'empty-drop-item',
  34. start: function(){
  35. pyro.widgets.$areas.accordion('resize');
  36. },
  37. stop: function(){
  38. pyro.widgets.$areas.accordion('resize');
  39. },
  40. update: function(){
  41. var order = [];
  42. $(this).children('li').each(function(){
  43. order.push($(this).attr('id'));
  44. });
  45. $.post(SITE_URL + 'widgets/ajax/update_order', { order: order.join(',') });
  46. }
  47. },
  48. // Widget Box
  49. draggable: {
  50. revert : 'invalid',
  51. cursor : 'move',
  52. helper : 'clone',
  53. cursorAt : {left: 100},
  54. refreshPositions: true,
  55. start : function(e, ui){
  56. // Grab our desired width from the widget area list
  57. var width = pyro.widgets.$instances.width() - 22;
  58. // Setup our new dragging object
  59. $(this).addClass('widget-drag')
  60. $(ui.helper).css('width', width);
  61. },
  62. stop: function() {
  63. $(this).removeClass('widget-drag');
  64. }
  65. },
  66. // Widget Instances List
  67. droppable: {
  68. hoverClass : 'drop-hover',
  69. accept : '.widget-box',
  70. greedy : true,
  71. tolerance : 'pointer',
  72. over : function(){
  73. pyro.widgets.$areas.accordion('resize');
  74. },
  75. out : function(){
  76. pyro.widgets.$areas.accordion('resize');
  77. },
  78. drop : function(e, ui){
  79. $('li.empty-drop-item', this).show().addClass('loading');
  80. pyro.widgets.prep_instance_form(ui.draggable, $(pyro.widgets.selector.instances, this));
  81. }
  82. }
  83. },
  84. init: function()
  85. {
  86. // Create/Edit Areas
  87. $('a.create-area, .widget-area-content > .buttons > a.edit').livequery(function(){
  88. $(this).colorbox({
  89. scrolling : false,
  90. width :'700',
  91. height :'400',
  92. onComplete : function(){
  93. pyro.widgets.handle_area_form(this);
  94. }
  95. });
  96. });
  97. // Delete Areas
  98. $('.widget-area-content > .buttons > button[value=delete]').live('click-confirmed', function(e){
  99. e.preventDefault();
  100. var $area = $(this).parents('.js-widget-area'),
  101. id = $area.attr('data-id'),
  102. url = SITE_URL + 'admin/widgets/areas/delete/' + id;
  103. $.post(url, {}, function(response){
  104. if (response.status == 'success')
  105. {
  106. $area.slideUp(function(){
  107. $(this).remove();
  108. });
  109. }
  110. pyro.add_notification(response.message);
  111. }, 'json');
  112. });
  113. // Edit Instances
  114. $('.widget-actions > a.button.edit').live('click', function(e){
  115. e.preventDefault();
  116. var $anchor = $(this);
  117. // hide
  118. $anchor.parents('.widget-instance').slideUp(50, function(){
  119. // fake loading..
  120. $(this).siblings('li.empty-drop-item').clone()
  121. // move
  122. .insertAfter(this)
  123. // show
  124. .show().addClass('loading clone');
  125. // next step
  126. pyro.widgets.prep_instance_form($anchor, this, 'edit');
  127. });
  128. });
  129. // Delete Instances
  130. $('.widget-actions > button[value=delete]').live('click-confirmed', function(e){
  131. e.preventDefault();
  132. var $item = $(this).parents('li.widget-instance'),
  133. id = $item.attr('id').replace(/instance-/, ''),
  134. url = SITE_URL + 'admin/widgets/delete/' + id;
  135. $.post(url, {}, function(response){
  136. if (response.status == 'success')
  137. {
  138. $item.slideUp(50, function(){
  139. $(this).remove();
  140. });
  141. }
  142. pyro.add_notification(response.message);
  143. }, 'json');
  144. pyro.widgets.$areas.accordion('resize');
  145. });
  146. $.extend(true, pyro.widgets, {
  147. $areas : $('#widget-areas-list'),
  148. $boxes : $('.widget-box'),
  149. selector : {
  150. instances : '.widget-list > ol',
  151. container : '.widget-area-content'
  152. }
  153. });
  154. // Widget Instances Sortable
  155. pyro.widgets.$areas.bind('accordioncreate', function(){
  156. pyro.widgets.$instances = $(pyro.widgets.selector.instances).sortable(pyro.widgets.ui_options.sortable);
  157. });
  158. // Widget Instances Droppable
  159. pyro.widgets.$areas.bind('accordioncreate', function(){
  160. pyro.widgets.$container = $(pyro.widgets.selector.container).droppable(pyro.widgets.ui_options.droppable);
  161. pyro.widgets.$areas.find('> section > header').droppable({
  162. accept: '.widget-box',
  163. addClasses: false,
  164. greedy: true,
  165. tolerance: 'pointer',
  166. over: function(){
  167. pyro.widgets.$areas.accordion('option', 'active', this);
  168. }
  169. });
  170. });
  171. // Widget Areas Accordion
  172. pyro.widgets.$areas.accordion(pyro.widgets.ui_options.accordion);
  173. // Widget Boxes Draggable
  174. pyro.widgets.$boxes.draggable(pyro.widgets.ui_options.draggable);
  175. // Create a slug
  176. pyro.generate_slug('input[name="name"]', 'input[name="slug"]');
  177. // MANAGE ------------------------------------------------------------------------------
  178. $('#widgets-list > tbody').livequery(function(){
  179. $(this).sortable({
  180. handle: 'span.move-handle',
  181. stop: function(){
  182. $('#widgets-list > tbody > tr').removeClass('alt');
  183. $('#widgets-list > tbody > tr:nth-child(even)').addClass('alt');
  184. var order = [];
  185. $('#widgets-list > tbody > tr input[name="action_to\[\]"]').each(function(){
  186. order.push(this.value);
  187. });
  188. order = order.join(',');
  189. $.post(SITE_URL + 'widgets/ajax/update_order/widget', { order: order });
  190. }
  191. });
  192. });
  193. },
  194. handle_area_form: function(anchor)
  195. {
  196. var $loading = $('#cboxLoadingOverlay, #cboxLoadingGraphic'),
  197. $cbox = $('#cboxLoadedContent'),
  198. $submit = $cbox.find('button[value=save]'),
  199. $cancel = $cbox.find('.button.cancel'),
  200. $form = $cbox.find('form'),
  201. url = $(anchor).attr('href');
  202. $cancel.click(function(e){
  203. e.preventDefault();
  204. $.colorbox.close();
  205. });
  206. $submit.click(function(e){
  207. e.preventDefault();
  208. var data = $form.slideUp().serialize();
  209. $loading.show();
  210. $.post(url, data, function(response){
  211. var callback = false;
  212. if (response.status == 'success')
  213. {
  214. if (response.title)
  215. {
  216. // editing replace area title
  217. }
  218. if (response.html)
  219. {
  220. pyro.widgets.$areas
  221. .html(response.html)
  222. .accordion('refresh');
  223. if (response.active)
  224. {
  225. pyro.widgets.$areas.accordion('option', 'active', response.active);
  226. }
  227. }
  228. url.match(/create/) && $form.get(0).reset();
  229. callback = function(){
  230. $.colorbox.resize();
  231. $.colorbox.close();
  232. };
  233. }
  234. else
  235. {
  236. callback = $.colorbox.resize;
  237. }
  238. $loading.hide();
  239. $form.slideDown();
  240. pyro.add_notification(response.message, {ref: $cbox, method: 'prepend'}, callback);
  241. }, 'json');
  242. });
  243. $.colorbox.resize();
  244. },
  245. update_area: function(){
  246. var url = SITE_URL + 'admin/widgets/areas';
  247. pyro.widgets.$areas.load(url, function(){
  248. $(this).accordion('refresh');
  249. });
  250. },
  251. prep_instance_form: function(item, container, action)
  252. {
  253. action || (action = 'add');
  254. var key = (action == 'add') ? $(item).attr('id').replace(/^widget-/, '') : $(container).attr('id').replace(/^instance-/, ''),
  255. url = (action == 'add') ? SITE_URL + 'admin/widgets/create/' + key : $(item).attr('href');
  256. // known? receive the action form
  257. if (key in pyro.cache.widget_forms[action])
  258. {
  259. // next step
  260. return pyro.widgets.add_instance_form(pyro.cache.widget_forms[action][key], container, action, key);
  261. }
  262. $.get(url, function(response){
  263. response = '<li id="' + action + '-instance-box" class="box widget-instance no-sortable">' +
  264. response + '</li>';
  265. // write action form into cache
  266. pyro.cache.widget_forms[action][key] = response;
  267. pyro.widgets.add_instance_form(response, container, action, key);
  268. }, 'html');
  269. },
  270. add_instance_form: function(item, container, action, key)
  271. {
  272. var widget = {
  273. $item: $(item),
  274. $container: $(container)
  275. }, method = 'appendTo';
  276. if (action === 'edit')
  277. {
  278. widget.$container.parent().children('li.empty-drop-item.clone').slideUp(50, function(){
  279. $(this).remove();
  280. });
  281. method = 'insertAfter';
  282. }
  283. else
  284. {
  285. widget.$container.children('li.empty-drop-item').hide().removeClass('loading');
  286. }
  287. pyro.widgets.handle_instance_form(widget.$item[method](widget.$container).slideDown(200, function(){
  288. pyro.widgets.$boxes.draggable('disable');
  289. pyro.widgets.$areas.accordion('resize');
  290. }).children('form'), action, key);
  291. },
  292. handle_instance_form: function(form, action, key)
  293. {
  294. var $form = $(form),
  295. $submit = $form.find('#instance-actions button[value=save]'),
  296. $cancel = $form.find('#instance-actions a.cancel')
  297. area_id = $form.parents('.js-widget-area').attr('data-id'),
  298. url = $form.attr('action');
  299. if ($form.data('has_events'))
  300. {
  301. return;
  302. }
  303. $form.data('has_events', true);
  304. $cancel.click(function(e){
  305. e.preventDefault();
  306. var callback = action === 'edit' ? function(){
  307. $('li#instance-'+key).slideDown(function(){
  308. pyro.widgets.$areas.accordion('resize');
  309. });
  310. } : false;
  311. pyro.widgets.rm_instance_form($form, action, key, callback);
  312. });
  313. $submit.click(function(e){
  314. e.preventDefault();
  315. var data = $form.serialize() + (action === 'add' ? '&widget_area_id=' + area_id : '');
  316. $.post(url, data, function(response){
  317. var callback = false,
  318. options = {};
  319. if (response.status == 'success')
  320. {
  321. callback = function(){
  322. pyro.widgets.rm_instance_form($form, action, key, function(){
  323. pyro.widgets.update_area();
  324. var $active = pyro.widgets.$areas.find('> section > header:eq('+pyro.widgets.$areas.accordion('option', 'active')+')').parent();
  325. if (response.active && response.active !== ('#' + $active.attr('id') + ' header'))
  326. {
  327. pyro.widgets.$areas.accordion('option', 'active', response.active);
  328. }
  329. });
  330. }
  331. }
  332. else
  333. {
  334. options = {
  335. ref: $form.children('header:eq(0)')
  336. }
  337. }
  338. pyro.add_notification(response.message, options, callback);
  339. }, 'json');
  340. });
  341. },
  342. rm_instance_form: function(form, action, key, callback)
  343. {
  344. $(form).parent().slideUp(50, function(){
  345. action === 'add'
  346. ? $(this).remove()
  347. : key
  348. ? pyro.cache.widget_forms[action][key] = $(this).detach()
  349. : pyro.cache.widget_forms[action] = {};
  350. pyro.widgets.$boxes.draggable('enable');
  351. pyro.widgets.$areas.accordion('resize');
  352. callback && callback();
  353. });
  354. }
  355. // ,scroll_to: function(ele){
  356. // $('html, body').animate({
  357. // scrollTop: $(ele).offset().top
  358. // }, 1000);
  359. // }
  360. };
  361. pyro.widgets.init();
  362. // Slide toggle for widget codes
  363. $(".instance-code").livequery('click', function(){
  364. $('#'+$(this).attr('id')+'-wrap').toggle();
  365. });
  366. // Select code
  367. $(".widget-code").focus(function(){$(this).select()});
  368. $(".widget-code").mouseup(function(e){e.preventDefault();});
  369. });