tripal.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Using the closure to map jQuery to $.
  2. (function ($) {
  3. // Store our function as a property of Drupal.behaviors.
  4. Drupal.behaviors.tripal = {
  5. attach: function (context, settings) {
  6. $('.tripal-entity-unattached .field-items').replaceWith('<div class="field-items">Loading... <img src="' + tripal_path + '/theme/images/ajax-loader.gif"></div>');
  7. $('.tripal-entity-unattached').each(function () {
  8. var id = $(this).attr('id');
  9. if (id) {
  10. var field = new AjaxField(id);
  11. field.load();
  12. }
  13. });
  14. }
  15. };
  16. /**
  17. * AjaxField Constructor.
  18. *
  19. * @param id
  20. * @constructor
  21. */
  22. function AjaxField(id) {
  23. this.id = id;
  24. this.hidePaneTitle();
  25. }
  26. /**
  27. * Hide pane title if the content of the pane has only ajax fields.
  28. */
  29. AjaxField.prototype.hidePaneTitle = function () {
  30. var id = this.id;
  31. var field = $('#' + id);
  32. var classes = field.parents('.tripal_pane').first().attr('class').split(' ');
  33. var pane_id = this.extractPaneID(classes);
  34. if (pane_id) {
  35. // Check if the fieldset has children that are not AJAX fields
  36. var has_children = $('.tripal_pane-fieldset-' + pane_id)
  37. .first()
  38. .children()
  39. .not('.tripal_pane-fieldset-buttons')
  40. .not('.field-group-format-title')
  41. .not('.tripal-entity-unattached')
  42. .not('#' + id).length > 0;
  43. // If there are no children, hide the pane title
  44. if (!has_children) {
  45. $('#' + pane_id).hide(0);
  46. }
  47. }
  48. };
  49. /**
  50. * Load the fields content from the server.
  51. */
  52. AjaxField.prototype.load = function () {
  53. $.ajax({
  54. url : baseurl + '/bio_data/ajax/field_attach/' + this.id,
  55. dataType: 'json',
  56. type : 'GET',
  57. success : this.handleSuccess.bind(this)
  58. });
  59. };
  60. /**
  61. * Add the content of the field to its pane.
  62. *
  63. * @param data
  64. */
  65. AjaxField.prototype.handleSuccess = function (data) {
  66. var content = data['content'];
  67. var id = data['id'];
  68. var field = $('#' + id);
  69. var classes = field.parents('.tripal_pane').first().attr('class').split(' ');
  70. var pane_id = this.extractPaneID(classes);
  71. $('#' + id + ' .field-items').replaceWith(content);
  72. // If the field has no content, check to verify the pane is empty
  73. // then remove it.
  74. if (content.trim().length === 0) {
  75. // Remove the field since it's empty
  76. field.remove();
  77. if (pane_id) {
  78. var pane = $('#' + pane_id);
  79. // If the pane has only the title and close button, we can
  80. // remove it
  81. var has_children = $('.tripal_pane-fieldset-' + pane_id)
  82. .first()
  83. .children()
  84. .not('.tripal_pane-fieldset-buttons')
  85. .not('.field-group-format-title')
  86. .not('#' + id).length > 0;
  87. if (!has_children) {
  88. pane.remove();
  89. }
  90. }
  91. }
  92. else {
  93. if (pane_id) {
  94. $('#' + pane_id).show(0);
  95. }
  96. }
  97. };
  98. /**
  99. * Extract the pane id from parent classes.
  100. *
  101. * @param classes
  102. * @return {String|null}
  103. */
  104. AjaxField.prototype.extractPaneID = function (classes) {
  105. var sub_length = 'tripal_pane-fieldset-'.length;
  106. var pane_id = null;
  107. classes.map(function (cls) {
  108. if (cls.indexOf('tripal_pane-fieldset-') > -1) {
  109. pane_id = cls.substring(sub_length, cls.length);
  110. }
  111. });
  112. return pane_id;
  113. };
  114. })(jQuery);
  115. // Used for ajax update of fields by links in a pager.
  116. function tripal_navigate_field_pager(id, page) {
  117. jQuery(document).ajaxStart(function () {
  118. jQuery('#' + id + '-spinner').show();
  119. }).ajaxComplete(function () {
  120. jQuery('#' + id + '-spinner').hide();
  121. });
  122. jQuery.ajax({
  123. type : 'GET',
  124. url : Drupal.settings['basePath'] + 'bio_data/ajax/field_attach/' + id,
  125. data : {'page': page},
  126. success: function (response) {
  127. jQuery('#' + id + ' .field-items').replaceWith(response['content']);
  128. }
  129. });
  130. }