/media/overrider/js/overrider.js

https://github.com/pjwiseman/joomla-cms · JavaScript · 235 lines · 152 code · 29 blank · 54 comment · 14 complexity · 31342e39a31d0b6422bbf09e6ab6f274 MD5 · raw file

  1. /**
  2. * @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
  3. * @license GNU General Public License version 2 or later; see LICENSE.txt
  4. */
  5. /**
  6. * Some state variables for the overrider
  7. */
  8. Joomla.overrider = {
  9. states: {
  10. refreshing : false,
  11. refreshed : false,
  12. counter : 0,
  13. searchstring: '',
  14. searchtype : 'value'
  15. }
  16. };
  17. /**
  18. * Method for refreshing the database cache of known language strings via Ajax
  19. *
  20. * @return void
  21. *
  22. * @since 2.5
  23. */
  24. Joomla.overrider.refreshCache = function()
  25. {
  26. var $ = jQuery.noConflict(), self = this;
  27. this.states.refreshing = true;
  28. $('#refresh-status').slideDown().css('display', 'block');
  29. $.ajax(
  30. {
  31. type: "POST",
  32. url: 'index.php?option=com_languages&task=strings.refresh&format=json',
  33. dataType: 'json'
  34. }).done(function (r)
  35. {
  36. if (r.error && r.message)
  37. {
  38. alert(r.message);
  39. }
  40. if (r.messages)
  41. {
  42. Joomla.renderMessages(r.messages);
  43. }
  44. $('#refresh-status').slideUp().hide();
  45. self.states.refreshing = false;
  46. }).fail(function (xhr)
  47. {
  48. alert(Joomla.JText._('COM_LANGUAGES_VIEW_OVERRIDE_REQUEST_ERROR'));
  49. $('#refresh-status').slideUp().hide();
  50. });
  51. };
  52. /**
  53. * Method for searching known language strings via Ajax
  54. *
  55. * @param more Determines the limit start of the results
  56. *
  57. * @return void
  58. *
  59. * @since 2.5
  60. */
  61. Joomla.overrider.searchStrings = function(more)
  62. {
  63. var $ = jQuery.noConflict(), self = this;
  64. // Prevent searching if the cache is refreshed at the moment
  65. if (this.states.refreshing)
  66. {
  67. return;
  68. }
  69. // Only update the used searchstring and searchtype if the search button
  70. // was used to start the search (that will be the case if 'more' is null)
  71. if (!more)
  72. {
  73. this.states.searchstring = $('#jform_searchstring').val();
  74. this.states.searchtype = $('#jform_searchtype') !== null ? $('#jform_searchtype').val() : 'value';
  75. }
  76. if (!this.states.searchstring)
  77. {
  78. $('#jform_searchstring').addClass('invalid');
  79. return;
  80. }
  81. if (more)
  82. {
  83. // If 'more' is greater than 0 we have already displayed some results for
  84. // the current searchstring, so display the spinner at the more link
  85. $('#more-results').addClass('overrider-spinner');
  86. }
  87. else
  88. {
  89. // Otherwise it is a new searchstring and we have to remove all previous results first
  90. $('#more-results').hide();
  91. var $children = $('#results-container div.language-results');
  92. $children.remove();
  93. $('#results-container').addClass('overrider-spinner').slideDown().css('display', 'block');
  94. }
  95. $.ajax(
  96. {
  97. type: "POST",
  98. url: 'index.php?option=com_languages&task=strings.search&format=json',
  99. data: 'searchstring=' + self.states.searchstring + '&searchtype=' + self.states.searchtype + '&more=' + more,
  100. dataType: 'json'
  101. }).done(function (r)
  102. {
  103. if (r.error && r.message)
  104. {
  105. alert(r.message);
  106. }
  107. if (r.messages)
  108. {
  109. Joomla.renderMessages(r.messages);
  110. }
  111. if (r.data)
  112. {
  113. if (r.data.results)
  114. {
  115. self.insertResults(r.data.results);
  116. }
  117. if (r.data.more)
  118. {
  119. // If there are more results than the sent ones
  120. // display the more link
  121. self.states.more = r.data.more;
  122. $('#more-results').slideDown().css('display', 'block');
  123. }
  124. else
  125. {
  126. $('#more-results').hide();
  127. }
  128. }
  129. $('#results-container').removeClass('overrider-spinner');
  130. $('#more-results').removeClass('overrider-spinner');
  131. }).fail(function (xhr)
  132. {
  133. alert(Joomla.JText._('COM_LANGUAGES_VIEW_OVERRIDE_REQUEST_ERROR'));
  134. $('#results-container').removeClass('overrider-spinner');
  135. $('#more-results').removeClass('overrider-spinner');
  136. });
  137. };
  138. /**
  139. * Method inserting the received results into the results container
  140. *
  141. * @param results An array of search result objects
  142. *
  143. * @return void
  144. *
  145. * @since 2.5
  146. */
  147. Joomla.overrider.insertResults = function(results)
  148. {
  149. var $ = jQuery.noConflict();
  150. // For creating an individual ID for each result we use a counter
  151. this.states.counter = this.states.counter + 1;
  152. // Create a container into which all the results will be inserted
  153. var $results_div = $('<div>', {
  154. id : 'language-results' + this.states.counter,
  155. class : 'language-results',
  156. style : 'display:none;'
  157. });
  158. // Create some elements for each result and insert it into the container
  159. Array.each(results, function(item, index) {
  160. var $div = $('<div>', {
  161. class: 'result row' + index % 2,
  162. onclick: 'Joomla.overrider.selectString(' + this.states.counter + index + ');'
  163. });
  164. var $key = $('<div>', {
  165. id: 'override_key' + this.states.counter + index,
  166. class: 'result-key',
  167. html: item.constant,
  168. title: item.file
  169. });
  170. var $string = $('<div>',{
  171. id: 'override_string' + this.states.counter + index,
  172. class: 'result-string',
  173. html: item.string
  174. });
  175. $key.appendTo($div);
  176. $string.appendTo($div);
  177. $div.appendTo($results_div);
  178. }, this);
  179. // If there aren't any results display an appropriate message
  180. if (!results.length)
  181. {
  182. var $noresult = $('<div>',{
  183. html: Joomla.JText._('COM_LANGUAGES_VIEW_OVERRIDE_NO_RESULTS')
  184. });
  185. $noresult.appendTo($results_div);
  186. }
  187. // Finally insert the container afore the more link and reveal it
  188. $('#more-results').before($results_div);
  189. $('#language-results' + this.states.counter).slideDown().css('display','block');
  190. };
  191. /**
  192. * Inserts a specific constant/value pair into the form and scrolls the page back to the top
  193. *
  194. * @param id The ID of the element which was selected for insertion
  195. *
  196. * @return void
  197. *
  198. * @since 2.5
  199. */
  200. Joomla.overrider.selectString = function(id)
  201. {
  202. var $ = jQuery.noConflict();
  203. $('#jform_key').val($('#override_key' + id).html());
  204. $('#jform_override').val($('#override_string' + id).html());
  205. $(window).scrollTop(0);
  206. };