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