PageRenderTime 46ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/skins/ovidentia/templates/widgets.acl.jquery.js

https://bitbucket.org/cantico/widgets
JavaScript | 688 lines | 428 code | 201 blank | 59 comment | 92 complexity | c01c5b53f18b38dbe3c90ca786a5e65a MD5 | raw file
  1. window.babAddonWidgets.aclButtonClicked = function() {
  2. var input = jQuery(this).closest('.widget-acl-frame').prev('input');
  3. // open dialog
  4. var page = jQuery('<div class="widget-acl-dialog"><div class="toolbar"></div><ul class="root"></ul></div>');
  5. page.attr('title', input.attr('title'));
  6. page.dialog({
  7. 'width': jQuery(window).width() - 80,
  8. 'height': jQuery(window).height() - 80,
  9. 'modal': true,
  10. 'buttons':
  11. [{
  12. text: "Ok",
  13. click: function() {
  14. // serialize checked groups
  15. window.babAddonWidgets.updateGroups(input, jQuery(this));
  16. jQuery(this).dialog("close");
  17. }
  18. }]
  19. });
  20. window.babAddonWidgets.fillGroups(input, page.find('ul.root'), '', 2);
  21. window.babAddonWidgets.addToolbar(input, page);
  22. return false;
  23. }
  24. window.babAddonWidgets.addGroupSelection = function(domNode)
  25. {
  26. jQuery(domNode).find('.widget-acl').each(function(index, widget) {
  27. widget = jQuery(widget);
  28. var meta = window.babAddonWidgets.getMetadata(widget.attr('id'));
  29. var frame = jQuery('<div class="widget-acl-frame"><strong></strong></div>');
  30. frame.find('strong').text(widget.attr('title'));
  31. var button = jQuery('<button><img src="'+meta.setGroupsImage+'" /></button>');
  32. button.attr('title', meta.buttonText);
  33. frame.append(button);
  34. var list = jQuery('<ul></ul>');
  35. if (meta.maxHeight)
  36. {
  37. list.css('max-height', meta.maxHeight+meta.maxHeightUnit);
  38. }
  39. frame.append(list);
  40. frame.insertAfter(widget);
  41. // window.babAddonWidgets.updateList(widget, true);
  42. button.click(window.babAddonWidgets.aclButtonClicked);
  43. });
  44. }
  45. /**
  46. * OPen group, the group must be visible
  47. *
  48. * @param input
  49. * @param page
  50. */
  51. window.babAddonWidgets.openGroup = function(input, page, id_group)
  52. {
  53. var plus = page.find('input[name="group_tree[]"][value="'+id_group+'"]').closest('li').find('strong');
  54. if (plus.hasClass('plus'))
  55. {
  56. window.babAddonWidgets.deployGroup(plus, input, false);
  57. }
  58. }
  59. /**
  60. * Toolbar
  61. *
  62. * @param input
  63. * @param page
  64. */
  65. window.babAddonWidgets.addToolbar = function(input, page)
  66. {
  67. // groups stored in the input
  68. var val = input.attr('value').replace(/^[,0]+/g,'').replace(/[,0]+$/g,'');
  69. if ('' != val)
  70. {
  71. var toolbar = page.find('.toolbar');
  72. var meta = window.babAddonWidgets.getMetadata(input.attr('id'));
  73. var button = jQuery('<button></button>');
  74. toolbar.append(button);
  75. button.text(meta.deployButton);
  76. button.click(function() {
  77. // add the groups checked by user but not allready saved in the input
  78. page.find('input:checked').each(function(index, checkbox) {
  79. val += ','+jQuery(checkbox).attr('value');
  80. });
  81. jQuery.ajax({
  82. url: meta.selfpage,
  83. data: 'tg=addon/widgets/groups&idx=checked&groups='+encodeURIComponent(val)+'&uid='+encodeURIComponent(meta.uid),
  84. success: function(response) {
  85. eval('var lists = '+response+';');
  86. var id_group = null;
  87. // make sure the first node is open
  88. window.babAddonWidgets.openGroup(input, page, 0);
  89. for(var l = 0; l < lists.length; l++)
  90. {
  91. for(var g = 0; g < lists[l].length; g++)
  92. {
  93. id_group = lists[l][g];
  94. window.babAddonWidgets.openGroup(input, page, id_group);
  95. }
  96. }
  97. }
  98. });
  99. });
  100. }
  101. }
  102. /**
  103. *
  104. * @param widget
  105. * @param groups
  106. */
  107. window.babAddonWidgets.processGroups = function(widget, groups, real)
  108. {
  109. var ul = widget.next('div').find('ul');
  110. var meta = window.babAddonWidgets.getMetadata(widget.attr('id'));
  111. for(var i = 0; i < groups.length; i++)
  112. {
  113. if (groups[i].withChildNodes)
  114. {
  115. g = groups[i].id+'a';
  116. } else {
  117. g = groups[i].id+'b';
  118. }
  119. if (ul.find('li.widget-acl-group-'+g).length > 0)
  120. {
  121. // allready inserted
  122. ul.find('li.widget-acl-group-'+g).removeClass('deleted');
  123. continue;
  124. }
  125. var li = jQuery('<li><div class="group"><span></span><br /><em></em><br /></div><div class="delete"></div></li>');
  126. li.data('group', groups[i]);
  127. li.addClass('widget-acl-group-'+g);
  128. li.find('span').text(groups[i].name);
  129. if (groups[i].withChildNodes)
  130. {
  131. li.find('em').text(meta.withChildNodes);
  132. }
  133. if (groups[i].setOfGroups)
  134. {
  135. li.find('em').text(meta.setOfGroups);
  136. }
  137. if (!real)
  138. {
  139. li.addClass('inserted');
  140. }
  141. ul.append(li);
  142. li.find('.delete').attr('title', meta.deleteTitle);
  143. li.find('.delete').click(function() {
  144. var id_group = jQuery(this).closest('li').data('group').id;
  145. var input = jQuery(this).closest('.widget-acl-frame').prev('input');
  146. var value = input.attr('value').split(',');
  147. var newvalue = new Array;
  148. for(var i = 0; i < value.length; i++)
  149. {
  150. if (value[i] != id_group && value[i] != id_group+'+')
  151. {
  152. newvalue.push(value[i]);
  153. }
  154. }
  155. input.attr('value', newvalue.join(','));
  156. // jQuery(this).closest('li').remove();
  157. jQuery(this).closest('li').addClass('deleted');
  158. });
  159. }
  160. }
  161. /**
  162. * Update the main list of groups
  163. * @param jQuery widgets
  164. * @param bool real real update or update with additional CSS classes to show the future changes
  165. * for real=false, classes are deleted, inserted
  166. * @returns {String}
  167. */
  168. window.babAddonWidgets.updateList = function(widgets, real)
  169. {
  170. var url_uid = '';
  171. widgets.each(function(index, widget) {
  172. widget = jQuery(widget);
  173. var groups = widget.attr('value');
  174. if ('' == groups)
  175. {
  176. return;
  177. }
  178. var meta = window.babAddonWidgets.getMetadata(widget.attr('id'));
  179. url_uid += '&groups['+encodeURIComponent(meta.uid)+']='+encodeURIComponent(groups);
  180. var ul = widget.next('div').find('ul');
  181. ul.addClass('loading');
  182. });
  183. if ('' == url_uid)
  184. {
  185. return;
  186. }
  187. var meta = window.babAddonWidgets.getMetadata(jQuery(widgets[0]).attr('id'));
  188. var controller = 'tg=addon/widgets/groups';
  189. // var controller = 'addon=widgets.groups';
  190. jQuery.ajax({
  191. url: meta.selfpage,
  192. data: controller+'&idx=infos'+url_uid,
  193. success: function(response) {
  194. if ('{' == response.substr(0,1)) {
  195. eval('var items = '+response+';');
  196. for(var id in items)
  197. {
  198. var groups = items[id];
  199. var widget = jQuery('#'+id);
  200. var ul = widget.next('div').find('ul');
  201. var g = null;
  202. ul.removeClass('loading');
  203. ul.find('li').map(function()
  204. {
  205. var li = jQuery(this);
  206. remove = true;
  207. for(var i = 0; i < groups.length; i++)
  208. {
  209. if (groups[i].id == li.data('group').id && groups[i].withChildNodes == li.data('group').withChildNodes)
  210. {
  211. // li found in response, do not remove li
  212. remove = false;
  213. break;
  214. }
  215. }
  216. if (remove)
  217. {
  218. if (real)
  219. {
  220. li.remove();
  221. } else {
  222. li.addClass('deleted');
  223. }
  224. }
  225. });
  226. window.babAddonWidgets.processGroups(widget, groups, real);
  227. }
  228. } else {
  229. if (response.length != 0)
  230. {
  231. if(response.length < 2048) {
  232. alert(response);
  233. } else {
  234. alert('error, you should check the installation status of Widget library');
  235. }
  236. }
  237. }
  238. }
  239. });
  240. }
  241. window.babAddonWidgets.isChecked = function(input, id_group, withChildNodes)
  242. {
  243. if (withChildNodes)
  244. {
  245. id_group = id_group+'+';
  246. }
  247. var groups = input.attr('value').split(',');
  248. for(var i=0; i<groups.length; i++)
  249. {
  250. if (groups[i] == id_group)
  251. {
  252. return true;
  253. }
  254. }
  255. return false;
  256. }
  257. window.babAddonWidgets.updateGroups = function(input, dialog)
  258. {
  259. // do not remove from input the id not processed in dialog
  260. var groups = input.attr('value').split(',');
  261. var indialog = new Array();
  262. var checked = new Array();
  263. var groupid = null;
  264. dialog.find('input').each(function(index, input) {
  265. input = jQuery(input);
  266. if (input.attr('name').indexOf('tree') != -1)
  267. {
  268. groupid = input.attr('value')+'+';
  269. } else {
  270. groupid = input.attr('value');
  271. }
  272. indialog.push(groupid);
  273. if (input.is(':checked') && !input.is(':disabled'))
  274. {
  275. checked.push(groupid);
  276. }
  277. });
  278. // remove processed item
  279. groups = jQuery.map(groups, function(g) {
  280. if ('' == g)
  281. {
  282. return null;
  283. }
  284. for(var i=0; i<indialog.length; i++)
  285. {
  286. if (indialog[i] == g)
  287. {
  288. return null;
  289. }
  290. }
  291. return g;
  292. });
  293. // add checked items
  294. for(var i=0; i<checked.length; i++)
  295. {
  296. groups.push(checked[i]);
  297. }
  298. if (groups.length == 0)
  299. {
  300. input.attr('value', '');
  301. } else {
  302. input.attr('value', groups.join(','));
  303. }
  304. window.babAddonWidgets.updateList(input, false);
  305. }
  306. /**
  307. * get group ID from LI
  308. */
  309. window.babAddonWidgets.getGroupId = function(li)
  310. {
  311. var id = jQuery(li.find('input')[0]).attr('value');
  312. return id;
  313. }
  314. /**
  315. * deploy group
  316. * @returns
  317. */
  318. window.babAddonWidgets.deployGroup = function(plus, input, async) {
  319. var li = plus.closest('li');
  320. if (plus.hasClass('plus'))
  321. {
  322. if (!li.next('ul,li').is('ul'))
  323. {
  324. plus.attr('class','loading');
  325. var ul = jQuery('<ul></ul>');
  326. ul.insertAfter(li);
  327. window.babAddonWidgets.fillGroups(input, ul, window.babAddonWidgets.getGroupId(li), 1, async);
  328. } else {
  329. li.next('ul').show();
  330. plus.attr('class','minus');
  331. }
  332. } else {
  333. li.next('ul').hide();
  334. plus.attr('class','plus');
  335. }
  336. }
  337. window.babAddonWidgets.fillGroupsLevel = function(input, ul, id_parent, groups, parent)
  338. {
  339. for(var i=0; i<groups.length; i++)
  340. {
  341. var li = jQuery('<li><strong></strong><div class="list-item"><input type="checkbox" name="group_tree[]" value="'+groups[i].id+'" /><span></span></div> <div class="eol"><input type="checkbox" name="group[]" value="'+groups[i].id+'" /> </div></li>');
  342. li.find('span').text(groups[i].name);
  343. var group_tree = li.find('[name="group_tree[]"]'); // left checkbox
  344. var group = li.find('[name="group[]"]'); // right checkbox
  345. if (null != groups[i].groupTreeDisabled && groups[i].groupTreeDisabled)
  346. {
  347. group_tree.prop('disabled', true);
  348. }
  349. if (null != groups[i].groupDisabled && groups[i].groupDisabled)
  350. {
  351. group.prop('disabled', true);
  352. }
  353. if (window.babAddonWidgets.isChecked(input, groups[i].id, true))
  354. {
  355. group_tree.prop('checked',true);
  356. group.prop('checked',true);
  357. group.prop('disabled',true);
  358. }
  359. if (window.babAddonWidgets.isChecked(input, groups[i].id, false))
  360. {
  361. group.prop('checked',true);
  362. }
  363. if (parent.is(':checked'))
  364. {
  365. group_tree.prop('checked',true);
  366. group.prop('checked',true);
  367. group_tree.prop('disabled',true);
  368. group.prop('disabled',true);
  369. }
  370. if (groups[i].setOfGroups)
  371. {
  372. group_tree.css({"visibility":"hidden"});
  373. }
  374. ul.append(li);
  375. group_tree.click(function() {
  376. var c = jQuery(this).closest('li').next('ul').find('input[type=checkbox]');
  377. c.push(jQuery(this).closest('div').nextAll('.eol').find('input[type=checkbox]')[0]);
  378. if (jQuery(this).is(':checked'))
  379. {
  380. c.prop('disabled', true);
  381. c.prop('checked', true);
  382. } else {
  383. c.prop('disabled', false);
  384. c.prop('checked', false);
  385. }
  386. });
  387. // add a + if the group have child nodes
  388. if (groups[i].hasChildNodes)
  389. {
  390. var plus = li.find('strong');
  391. plus.attr('class','plus');
  392. plus.click(function() {
  393. window.babAddonWidgets.deployGroup(jQuery(this), input, true);
  394. });
  395. }
  396. }
  397. }
  398. /**
  399. * @param jQuery input input text field
  400. * @param jQuery ul target ul to insert nodes
  401. * @param int id_parent parent group
  402. * @param int levels number of levels to get in request
  403. */
  404. window.babAddonWidgets.fillGroups = function(input, ul, id_parent, levels, async)
  405. {
  406. // get groups from id_parent
  407. var meta = window.babAddonWidgets.getMetadata(input.attr('id'));
  408. if (null == async)
  409. {
  410. async = true;
  411. }
  412. jQuery.ajax({
  413. async: async,
  414. url: meta.selfpage,
  415. data: 'tg=addon/widgets/groups&idx=get&id_parent='+id_parent+'&uid='+meta.uid+'&levels='+levels+'&id_delegation='+meta.id_delegation,
  416. success: function(response){
  417. if ('[' == response.substr(0,1)) {
  418. eval('var result = '+response+';');
  419. for(var item = 0; item < result.length; item++)
  420. {
  421. var level_id_parent = result[item].id_parent;
  422. var level_groups = result[item].items;
  423. // apply filter
  424. for(var i = 0; i < level_groups.length; i++)
  425. {
  426. if (meta.disabledGroups && meta.disabledGroups[level_groups[i]['id']])
  427. {
  428. level_groups[i].groupDisabled = true;
  429. level_groups[i].groupTreeDisabled = true;
  430. }
  431. }
  432. var parent = ul.parent().find('[name="group_tree[]"][value="'+level_id_parent+'"]');
  433. if (parent.length != 0)
  434. {
  435. ul = parent.closest('li').next('ul');
  436. if (ul.length == 0)
  437. {
  438. ul = jQuery('<ul></ul>');
  439. ul.insertAfter(parent.closest('li'));
  440. }
  441. }
  442. //console.debug(level_id_parent);
  443. //console.debug(level_groups);
  444. window.babAddonWidgets.fillGroupsLevel(input, ul, level_id_parent, level_groups, parent);
  445. // set the minus class on parent checkbox
  446. if (!ul.hasClass('root'))
  447. {
  448. ul.prev('li').find('strong').attr('class', 'minus');
  449. }
  450. }
  451. } else {
  452. if(response.length < 2048) {
  453. alert(response);
  454. } else {
  455. alert('error');
  456. }
  457. }
  458. }
  459. });
  460. }
  461. window.babAddonWidgets.initForm = function(form)
  462. {
  463. widgets = form.find('.widget-acl');
  464. domNode = form.get(0);
  465. var acl = widgets;
  466. // hide default field
  467. acl.hide();
  468. // add group selection
  469. window.babAddonWidgets.addGroupSelection(domNode);
  470. window.babAddonWidgets.updateList(widgets, true);
  471. }
  472. window.babAddonWidgets.initAcl = function(domNode)
  473. {
  474. var widgets = jQuery(domNode).find('.widget-acl').not('.widget-init-done');
  475. if (widgets.length > 0) {
  476. widgets.addClass('widget-init-done');
  477. // process full form only
  478. var form = widgets.closest('form').not('.widget-init-acl-done');
  479. if (form.length > 0) {
  480. form.addClass('widget-init-acl-done');
  481. form.ready(function() {
  482. window.babAddonWidgets.initForm(form);
  483. });
  484. }
  485. }
  486. }
  487. window.bab.addInitFunction(window.babAddonWidgets.initAcl);