cck_phone.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. (function ($) {
  2. Drupal.PhoneNumber = Drupal.PhoneNumber || {};
  3. /**
  4. * Filters checkboxes based on their label.
  5. * This code is shamelessly taken from checkbox_filter
  6. */
  7. Drupal.PhoneNumber.filter = function() {
  8. var field = $(this);
  9. var checkboxes = field.parent().parent().find('.form-checkboxes .form-item');
  10. var found = false;
  11. var label = "";
  12. var option = null;
  13. for (var i = 0; i < checkboxes.length; i++) {
  14. option = checkboxes.eq(i);
  15. label = Drupal.PhoneNumber.trim(option.text());
  16. if (label.toUpperCase().indexOf(field.val().toUpperCase()) < 0) {
  17. option.hide();
  18. } else {
  19. option.show();
  20. }
  21. }
  22. };
  23. /**
  24. * Trims whitespace from strings
  25. */
  26. Drupal.PhoneNumber.trim = function(str) {
  27. var str = str.replace(/^\s\s*/, ''),
  28. ws = /\s/,
  29. i = str.length;
  30. while (ws.test(str.charAt(--i)));
  31. return str.slice(0, i + 1);
  32. };
  33. /**
  34. * Check/Uncheck all checkboxes
  35. */
  36. Drupal.PhoneNumber.checkall = function(e) {
  37. var field = $(this);
  38. var checkboxes = $('.form-checkboxes .form-item:visible .form-checkbox', field.parent().parent());
  39. var checked = (field.text() == Drupal.t('Select all'));
  40. if (checked) {
  41. checkboxes.attr('checked', true);
  42. field.text(Drupal.t('Deselect all'));
  43. }
  44. else {
  45. checkboxes.attr('checked', false);
  46. Drupal.PhoneNumber.checkDefault();
  47. field.text(Drupal.t('Select all'));
  48. }
  49. };
  50. /**
  51. * Country selection should include default country code by default.
  52. */
  53. Drupal.PhoneNumber.checkDefault = function(e) {
  54. var defaultCC = $('#edit-instance-settings-default-country').val();
  55. var span = $('<span class="default-cc"></span>').append(Drupal.t('Default'));
  56. $('.cck-phone-settings .form-checkboxes').find('.form-checkbox').each(function() {
  57. if ($(this).val() == defaultCC) {
  58. $('.cck-phone-default-country')
  59. .removeClass('cck-phone-default-country')
  60. .find('span.default-cc').remove();
  61. // TODO: check for "Enable default country code" only set the checkbox
  62. $(this)
  63. // .attr('checked', 'checked')
  64. .parents('.form-item:first')
  65. .addClass('cck-phone-default-country')
  66. .append(span);
  67. }
  68. });
  69. };
  70. /**
  71. * Attach a filtering textfield to checkboxes.
  72. */
  73. Drupal.behaviors.PhoneNumber = {
  74. attach: function(context) {
  75. // Ensure the new default country is checked
  76. $('#edit-instance-settings-default-country, .cck-phone-settings .form-checkboxes').bind('change', Drupal.PhoneNumber.checkDefault);
  77. $('#edit-instance-settings-default-country').trigger('change');
  78. $('form#field-ui-field-edit-form').submit(Drupal.PhoneNumber.checkDefault);
  79. // Filter for countries
  80. var form = '<div class="form-item container-inline">'
  81. + ' <label>' + Drupal.t('Filter') + ':</label> '
  82. + ' <input class="cck-phone-filter form-text" type="text" size="30" />'
  83. + ' <a class="cck-phone-check" style="margin-left: 1em;" href="javascript://">' + Drupal.t('Select all') + '</a>'
  84. + '</div>';
  85. $('.cck-phone-settings .form-checkboxes', context).before(form);
  86. $('input.cck-phone-filter').bind('keyup', Drupal.PhoneNumber.filter);
  87. $('a.cck-phone-check').bind('click', Drupal.PhoneNumber.checkall);
  88. }
  89. };
  90. })(jQuery);