]> git.uio.no Git - usit-rt.git/blame - share/html/NoAuth/js/util.js
Initial commit 4.0.5-3
[usit-rt.git] / share / html / NoAuth / js / util.js
CommitLineData
84fb5b46
MKG
1%# BEGIN BPS TAGGED BLOCK {{{
2%#
3%# COPYRIGHT:
4%#
5%# This software is Copyright (c) 1996-2012 Best Practical Solutions, LLC
6%# <sales@bestpractical.com>
7%#
8%# (Except where explicitly superseded by other copyright notices)
9%#
10%#
11%# LICENSE:
12%#
13%# This work is made available to you under the terms of Version 2 of
14%# the GNU General Public License. A copy of that license should have
15%# been provided with this software, but in any event can be snarfed
16%# from www.gnu.org.
17%#
18%# This work is distributed in the hope that it will be useful, but
19%# WITHOUT ANY WARRANTY; without even the implied warranty of
20%# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21%# General Public License for more details.
22%#
23%# You should have received a copy of the GNU General Public License
24%# along with this program; if not, write to the Free Software
25%# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26%# 02110-1301 or visit their web page on the internet at
27%# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
28%#
29%#
30%# CONTRIBUTION SUBMISSION POLICY:
31%#
32%# (The following paragraph is not intended to limit the rights granted
33%# to you to modify and distribute this software under the terms of
34%# the GNU General Public License and is only of importance to you if
35%# you choose to contribute your changes and enhancements to the
36%# community by submitting them to Best Practical Solutions, LLC.)
37%#
38%# By intentionally submitting any modifications, corrections or
39%# derivatives to this work, or any other work intended for use with
40%# Request Tracker, to Best Practical Solutions, LLC, you confirm that
41%# you are the copyright holder for those contributions and you grant
42%# Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
43%# royalty-free, perpetual, license to use, copy, create derivative
44%# works based on those contributions, and sublicense and distribute
45%# those contributions and any derivatives thereof.
46%#
47%# END BPS TAGGED BLOCK }}}
48/* Visibility */
49
50function show(id) { delClass( id, 'hidden' ) }
51function hide(id) { addClass( id, 'hidden' ) }
52
53function hideshow(id) { return toggleVisibility( id ) }
54function toggleVisibility(id) {
55 var e = jQuery('#' + id);
56
57 if ( e.hasClass('hidden') ) {
58 e.removeClass('hidden');
59 }
60 else {
61 e.addClass('hidden');
62 }
63
64 return false;
65}
66
67function setVisibility(id, visibility) {
68 if ( visibility ) show(id);
69 else hide(id);
70}
71
72function switchVisibility(id1, id2) {
73 // Show both and then hide the one we want
74 show(id1);
75 show(id2);
76 hide(id2);
77 return false;
78}
79
80/* Classes */
81function jQueryWrap( id ) {
82 return typeof id == 'object' ? jQuery(id) : jQuery('#'+id);
83}
84
85function addClass(id, value) {
86 jQueryWrap(id).addClass(value);
87}
88
89function delClass(id, value) {
90 jQueryWrap(id).removeClass(value);
91}
92
93/* Rollups */
94
95function rollup(id) {
96 var e = jQueryWrap(id);
97 var e2 = e.parent();
98
99 if (e.hasClass('hidden')) {
100 set_rollup_state(e,e2,'shown');
101 createCookie(id,1,365);
102 }
103 else {
104 set_rollup_state(e,e2,'hidden');
105 createCookie(id,0,365);
106 }
107 return false;
108}
109
110function set_rollup_state(e,e2,state) {
111 if (e && e2) {
112 if (state == 'shown') {
113 show(e);
114 delClass( e2, 'rolled-up' );
115 }
116 else if (state == 'hidden') {
117 hide(e);
118 addClass( e2, 'rolled-up' );
119 }
120 }
121}
122
123/* other utils */
124
125function focusElementById(id) {
126 var e = jQuery('#'+id);
127 if (e) e.focus();
128}
129
130function setCheckbox(form, name, val) {
131 var myfield = form.getElementsByTagName('input');
132 for ( var i = 0; i < myfield.length; i++ ) {
133 if ( myfield[i].type != 'checkbox' ) continue;
134 if ( name ) {
135 if ( name instanceof RegExp ) {
136 if ( ! myfield[i].name.match( name ) ) continue;
137 }
138 else {
139 if ( myfield[i].name != name ) continue;
140 }
141
142 }
143
144 myfield[i].checked = val;
145 }
146}
147
148/* apply callback to nodes or elements */
149
150function walkChildNodes(parent, callback)
151{
152 if( !parent || !parent.childNodes ) return;
153 var list = parent.childNodes;
154 for( var i = 0; i < list.length; i++ ) {
155 callback( list[i] );
156 }
157}
158
159function walkChildElements(parent, callback)
160{
161 walkChildNodes( parent, function(node) {
162 if( node.nodeType != 1 ) return;
163 return callback( node );
164 } );
165}
166
167/* shredder things */
168
169function showShredderPluginTab( plugin )
170{
171 var plugin_tab_id = 'shredder-plugin-'+ plugin +'-tab';
172 var root = jQuery('#shredder-plugin-tabs');
173
174 root.children(':not(.hidden)').addClass('hidden');
175 root.children('#' + plugin_tab_id).removeClass('hidden');
176
177 if( plugin ) {
178 show('shredder-submit-button');
179 } else {
180 hide('shredder-submit-button');
181 }
182}
183
184function checkAllObjects()
185{
186 var check = jQuery('#shredder-select-all-objects-checkbox').attr('checked');
187 var elements = jQuery('#shredder-search-form :checkbox[name=WipeoutObject]');
188
189 if( check ) {
190 elements.attr('checked', true);
191 } else {
192 elements.attr('checked', false);
193 }
194}
195
196function checkboxToInput(target,checkbox,val){
197 var tar = jQuery('#' + escapeCssSelector(target));
198 var box = jQuery('#' + escapeCssSelector(checkbox));
199 if(box.attr('checked')){
200 if (tar.val()==''){
201 tar.val(val);
202 }
203 else{
204 tar.val( val+', '+ tar.val() );
205 }
206 }
207 else{
208 tar.val(tar.val().replace(val+', ',''));
209 tar.val(tar.val().replace(val,''));
210 }
211 jQuery('#UpdateIgnoreAddressCheckboxes').val(true);
212}
213
214// ahah for back compatibility as plugins may still use it
215function ahah( url, id ) {
216 jQuery('#'+id).load(url);
217}
218
219// only for back compatibility, please JQuery() instead
220function doOnLoad( js ) {
221 jQuery(js);
222}
223
224jQuery(function() {
225 jQuery(".ui-datepicker:not(.withtime)").datepicker( {
226 dateFormat: 'yy-mm-dd',
227 constrainInput: false
228 } );
229
230 jQuery(".ui-datepicker.withtime").datepicker( {
231 dateFormat: 'yy-mm-dd',
232 constrainInput: false,
233 onSelect: function( dateText, inst ) {
234 // trigger timepicker to get time
235 var button = document.createElement('input');
236 button.setAttribute('type', 'button');
237 jQuery(button).width('5em');
238 jQuery(button).insertAfter(this);
239 jQuery(button).timepickr({val: '00:00'});
240 var date_input = this;
241
242 jQuery(button).blur( function() {
243 var time = jQuery(button).val();
244 if ( ! time.match(/\d\d:\d\d/) ) {
245 time = '00:00';
246 }
247 jQuery(date_input).val( dateText + ' ' + time + ':00' );
248 jQuery(button).remove();
249 } );
250
251 jQuery(button).focus();
252 }
253 } );
254});
255
256function textToHTML(value) {
257 return value.replace(/&/g, "&amp;")
258 .replace(/</g, "&lt;")
259 .replace(/>/g, "&gt;")
260 .replace(/-- \n/g,"--&nbsp;\n")
261 .replace(/\n/g, "\n<br />");
262};
263
264function ReplaceAllTextareas(encoded) {
265 var sAgent = navigator.userAgent.toLowerCase();
266 if (!CKEDITOR.env.isCompatible ||
267 sAgent.indexOf('iphone') != -1 ||
268 sAgent.indexOf('ipad') != -1 ||
269 sAgent.indexOf('android') != -1 )
270 return false;
271
272 // replace all content and signature message boxes
273 var allTextAreas = document.getElementsByTagName("textarea");
274
275 for (var i=0; i < allTextAreas.length; i++) {
276 var textArea = allTextAreas[i];
277 if (jQuery(textArea).hasClass("messagebox")) {
278 // Turn the original plain text content into HTML
279 if (encoded == 0) {
280 textArea.value = textToHTML(textArea.value);
281 }
282 // For this javascript
283 var CKeditorEncoded = document.createElement('input');
284 CKeditorEncoded.setAttribute('type', 'hidden');
285 CKeditorEncoded.setAttribute('name', 'CKeditorEncoded');
286 CKeditorEncoded.setAttribute('value', '1');
287 textArea.parentNode.appendChild(CKeditorEncoded);
288
289 // For fckeditor
290 var typeField = document.createElement('input');
291 typeField.setAttribute('type', 'hidden');
292 typeField.setAttribute('name', textArea.name + 'Type');
293 typeField.setAttribute('value', 'text/html');
294 textArea.parentNode.appendChild(typeField);
295
296
297 CKEDITOR.replace(textArea.name,{width:'100%',height:<% RT->Config->Get('MessageBoxRichTextHeight') |n,j%>});
298 CKEDITOR.basePath = <%RT->Config->Get('WebPath')|n,j%>+"/NoAuth/RichText/";
299
300 jQuery("#" + textArea.name + "___Frame").addClass("richtext-editor");
301 }
302 }
303};
304
305function toggle_addprincipal_validity(input, good, title) {
306 if (good) {
307 jQuery(input).nextAll(".warning").hide();
308 jQuery("#acl-AddPrincipal input[type=checkbox]").removeAttr("disabled");
309 } else {
310 jQuery(input).nextAll(".warning").css("display", "block");
311 jQuery("#acl-AddPrincipal input[type=checkbox]").attr("disabled", "disabled");
312 }
313
314 if (title == null)
315 title = jQuery(input).val();
316
317 update_addprincipal_title( title );
318}
319
320function update_addprincipal_title(title) {
321 var h3 = jQuery("#acl-AddPrincipal h3");
322 h3.html( h3.text().replace(/: .*$/,'') + ": " + title );
323}
324
325// when a value is selected from the autocompleter
326function addprincipal_onselect(ev, ui) {
327 // pass the item's value along as the title since the input's value
328 // isn't actually updated yet
329 toggle_addprincipal_validity(this, true, ui.item.value);
330}
331
332// when the input is actually changed, through typing or autocomplete
333function addprincipal_onchange(ev, ui) {
334 // if we have a ui.item, then they selected from autocomplete and it's good
335 if (!ui.item) {
336 var input = jQuery(this);
337 // Check using the same autocomplete source if the value typed would
338 // have been autocompleted and is therefore valid
339 jQuery.ajax({
340 url: input.autocomplete("option", "source"),
341 data: {
342 op: "=",
343 term: input.val()
344 },
345 dataType: "json",
346 success: function(data) {
347 if (data)
348 toggle_addprincipal_validity(input, data.length ? true : false );
349 else
350 toggle_addprincipal_validity(input, true);
351 }
352 });
353 } else {
354 toggle_addprincipal_validity(this, true);
355 }
356}
357
358
359function escapeCssSelector(str) {
360 return str.replace(/([^A-Za-z0-9_-])/g,'\\$1');
361}