checklistapi.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. (function ($) {
  2. "use strict";
  3. /**
  4. * Provides the summary information for the checklist form vertical tabs.
  5. */
  6. Drupal.behaviors.checklistapiFieldsetSummaries = {
  7. attach: function (context) {
  8. $('#checklistapi-checklist-form .vertical-tabs-panes > fieldset', context).drupalSetSummary(function (context) {
  9. var total = $(':checkbox.checklistapi-item', context).size(), args = {};
  10. if (total) {
  11. args['@complete'] = $(':checkbox.checklistapi-item:checked', context).size();
  12. args['@total'] = total;
  13. args['@percent'] = Math.round(args['@complete'] / args['@total'] * 100);
  14. return Drupal.t('@complete of @total (@percent%)', args);
  15. }
  16. });
  17. }
  18. };
  19. /**
  20. * Adds dynamic item descriptions toggling.
  21. */
  22. Drupal.behaviors.checklistapiCompactModeLink = {
  23. attach: function (context) {
  24. $('#checklistapi-checklist-form .compact-link a', context).click(function () {
  25. $(this).closest('#checklistapi-checklist-form').toggleClass('compact-mode');
  26. var is_compact_mode = $(this).closest('#checklistapi-checklist-form').hasClass('compact-mode');
  27. $(this)
  28. .text(is_compact_mode ? Drupal.t('Show item descriptions') : Drupal.t('Hide item descriptions'))
  29. .attr('title', is_compact_mode ? Drupal.t('Expand layout to include item descriptions.') : Drupal.t('Compress layout by hiding item descriptions.'));
  30. document.cookie = 'Drupal.visitor.checklistapi_compact_mode=' + (is_compact_mode ? 1 : 0);
  31. return false;
  32. });
  33. }
  34. };
  35. /**
  36. * Prompts the user if they try to leave the page with unsaved changes.
  37. *
  38. * Note: Auto-checked items are not considered unsaved changes for the purpose
  39. * of this feature.
  40. */
  41. Drupal.behaviors.checklistapiPromptBeforeLeaving = {
  42. getFormState: function () {
  43. return $('#checklistapi-checklist-form :checkbox.checklistapi-item').serializeArray().toString();
  44. },
  45. attach: function () {
  46. var beginningState = this.getFormState();
  47. $(window).bind('beforeunload', function () {
  48. var endingState = Drupal.behaviors.checklistapiPromptBeforeLeaving.getFormState();
  49. if (beginningState !== endingState) {
  50. return Drupal.t('Your changes will be lost if you leave the page without saving.');
  51. }
  52. });
  53. }
  54. };
  55. })(jQuery);