/
script.js
1218 lines (1054 loc) · 37.7 KB
/
script.js
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* These are the core JavaScript functions used on most pages generated by Wedge.
*
* Wedge (http://wedge.org)
* Copyright © 2010 René-Gilles Deberdt, wedge.org
* Portions are © 2011 Simple Machines.
* License: http://wedge.org/license/
*/
@language index;
var
oThought,
weEditors = [],
_formSubmitted,
_modalDone = false,
notload,
we_confirm = $txt['generic_confirm_request'],
we_loading = $txt['ajax_in_progress'],
we_cancel = $txt['form_cancel'],
we_delete = $txt['delete'],
we_submit = $txt['form_submit'],
we_ok = $txt['ok'],
// Basic browser detector. Do not trust it blindlessly. Still, it can be helpful.
ua = function (str) { return navigator.userAgent.toLowerCase().indexOf(str) != -1; },
// The Webkit ones. Oh my, that's a long list... Right now we're only supporting iOS and generic Android browsers.
is_webkit = ua('webkit'),
is_chrome = is_webkit && ua('chrome'),
is_ios = is_webkit && ua('(ip'),
is_android = is_webkit && ua('android'),
is_safari = is_webkit && !is_chrome && !is_android && !is_ios,
// This should allow us to catch more touch devices like smartphones and tablets...
is_touch = 'ontouchstart' in window || typeof window.Touch === 'object',
is_opera = ua('opera'),
// IE gets versioned, too. Do you have to ask why..?
is_ie = ua('msie') || ua('trident'), // ua('trident') seems to be the only way to detect IE 11+.
is_ie6 = is_ie && ua('msie 6'),
is_ie7 = is_ie && ua('msie 7'),
is_ie8 = is_ie && ua('msie 8'),
is_ie8down = is_ie6 || is_ie7 || is_ie8,
is_firefox = !is_webkit && !is_ie && ua('mozilla');
// Replace the default jQuery easing type for animations.
$.easing.swing2 = $.easing.swing;
$.easing.swing = function (x, t, b, c, d)
{
return b + c * Math.sqrt(1 - (t = t / d - 1) * t);
};
String.prototype.php_htmlspecialchars = function ()
{
return this.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
};
String.prototype.php_unhtmlspecialchars = function ()
{
return this.replace(/"/g, '"').replace(/>/g, '>').replace(/</g, '<').replace(/&/g, '&');
};
String.prototype.wereplace = function (oReplacements)
{
var sSearch, sResult = this;
// .replace() uses $ as a meta-character in replacement strings, so we need to convert it to $$ first.
for (sSearch in oReplacements)
sResult = sResult.replace(new RegExp('%' + sSearch + '%', 'g'), (oReplacements[sSearch] + '').split('$').join('$$'));
return sResult;
};
/*
A stylable alert() alternative.
@string string: HTML content to show.
[optional] @object e: the current event object, if any. Must be specified if the alert is called before moving to another page (e.g. submit).
[optional] @function callback: a function to call after the user clicked OK.
*/
function say(string, e, callback)
{
return _modalDone || reqWin('', 350, string, 2, callback || (e && !e.target ? e : 0), e && e.target ? e : 0);
}
/*
A stylable confirm() alternative.
@string string: HTML content to show.
[optional] @object e: the current event object, if any. Must be specified if the event handler uses ask() to cancel or proceed.
[optional] @function callback: a function to call after the user made their choice. function (answer) { if (answer) { They agreed. } }
*/
function ask(string, e, callback)
{
return _modalDone || reqWin('', 350, string, 1, callback || (e && !e.target ? e : 0), e && e.target ? e : 0);
}
/*
Open a new popup window. The first two params are for generic pop-ups, while the rest is for confirm/alert boxes.
@string from: specifies the URL to open. Use 'this' on a link to automatically use its href value.
@mixed desired_width: use custom width. Omit or set to 0 for default (480px). Height is always auto.
@string string: if provided, this is the contents. Otherwise, it will be retrieved through Ajax.
@int modal_type: is this a modal pop-up? (i.e. requires attention) 0 = no, 1 = confirm, 2 = alert
@function callback: if this is a modal pop-up (confirm, alert...), you can set a function to call with a boolean
param (true = OK, false = Cancel). You can force cancelling the event by returning false from it.
@object e: again, if this is a modal pop-up, we may need to re-call the original event.
*/
function reqWin(from, desired_width, string, modal_type, callback, e)
{
var
help_page = from && from.href ? from.href : from,
title = from && from.href ? $(from).text() : 0,
// window.innerHeight doesn't work in oldIE, and $(window).height() sometimes returns the body height in Chrome...
viewport_width = $(window).width(),
viewport_height = window.innerHeight || $(window).height(),
previous_target = $('#helf').data('src'),
close_window = function ()
{
$('#popup,#helf').removeClass('show');
setTimeout(function () { $('#popup').remove(); }, 300);
},
animate_popup = function ()
{
var $section = $('section', this);
// Ensure that the popup never goes past the viewport boundaries.
$section
.width(Math.min(desired_width || 480, viewport_width - 20))
.css({
maxWidth: viewport_width - 20 - $(this).width() + $section.width(),
maxHeight: viewport_height - 20 - $(this).height() + $section.height()
});
$(this).css({ left: (viewport_width - $(this).width()) / 2, top: (viewport_height - $(this).height()) / 2 }).ds();
$('#popup,#helf').addClass('show');
};
// Try and get the title for the current link.
if (!title)
{
var nextSib = from.nextSibling;
// Newlines are seen as stand-alone text nodes, so skip these...
while (nextSib && nextSib.nodeType == 3 && $.trim($(nextSib).text()) === '')
nextSib = nextSib.nextSibling;
// Get the final text, remove any dfn (description) tags, and trim the rest.
title = $.trim($(nextSib).clone().find('dfn').remove().end().text());
}
// Clicking the help icon twice should close the popup.
if ($('#popup').remove().length && previous_target && previous_target == help_page)
return false;
// We create the popup inside a dummy div to fix positioning in freakin' IE6.
$('body').append(
$('<div>')
.attr('id', 'popup')
.width(viewport_width)
.height(viewport_height)
.css('top', is_ie6 || is_ios ? $(window).scrollTop() : 0)
.append(
$('<div>')
.attr('id', 'helf')
.data('src', help_page)
)
);
if (modal_type)
$('#helf')
.html('<section class="nodrag confirm">' + string + '</section><footer><input type="button" class="submit'
+ (modal_type == 1 ? ' floatleft" /><input type="button" class="delete floatright" />' : '" />') + '</footer>')
.each(animate_popup)
.find('input')
.val(we_cancel)
.click(function () {
close_window();
if (callback && callback.call(e ? e.target : this, $(this).hasClass('submit')) === false)
return;
if (e && $(this).hasClass('submit'))
{
_modalDone = true;
// The location trick is required by non-HTML5 browsers.
// To save 4 bytes here, <a> tags only accept ask() through onclick.
if (e.target.href)
location = e.target.href;
else
$(e.target).trigger(e.type);
_modalDone = false;
}
})
.filter('.submit')
.val(we_ok);
else
$('#helf')
.load(help_page, { t: title }, animate_popup)
// Clicking anywhere on the page should close the popup.
.parent() // #popup
.click(function (e) {
// If we clicked somewhere in the popup, don't close it, because we may want to select text.
if (!$(e.target).closest('#helf').length)
close_window();
});
// Return false so the click won't follow the link ;)
return false;
}
// Only allow form submission ONCE.
function submitonce()
{
_formSubmitted = true;
// If there are any editors warn them submit is coming!
$.each(weEditors, function () { this.doSubmit(); });
}
function submitThisOnce(oControl)
{
$('textarea', oControl.form || oControl).attr('readOnly', true);
return !_formSubmitted;
}
// Checks for variable in an array.
function in_array(variable, theArray)
{
return $.inArray(variable, theArray) != -1;
}
// Invert all checkboxes at once by clicking a single checkbox.
function invertAll(oInvertCheckbox, oForm, sMask)
{
$.each(oForm, function () {
if (this.name && !this.disabled && (!sMask || this.name.indexOf(sMask) === 0|| this.id.indexOf(sMask) === 0))
this.checked = oInvertCheckbox.checked;
});
}
// Shorten all raw URLs in user content. The spans allow for the actual link to be retained when copying/pasting the page content.
function breakLinks()
{
$('.bbc_link').each(function () {
var link = $(this).text();
if (link == this.href && link.length > 50)
$(this).html(link.slice(0, 25) + '<span class="cut"><span>' + link.slice(25, -25) + '</span></span><wbr>' + link.slice(-25));
});
}
// Shows the page numbers by clicking the dots.
function expandPages(spanNode, firstPage, lastPage, perPage)
{
var i = firstPage, pageLimit = 50, baseURL = $(spanNode).data('href');
// Prevent too many pages from being loaded at once.
if ((lastPage - firstPage) / perPage > pageLimit)
{
var oldLastPage = lastPage;
lastPage = firstPage + perPage * pageLimit;
}
// Calculate the new pages.
for (; i < lastPage; i += perPage)
$(spanNode).before('<a href="' + baseURL.replace(/%1\$d/, i).replace(/%%/g, '%') + '">' + (1 + i / perPage) + '</a> ');
if (oldLastPage)
$(spanNode).off().click(function () { expandPages(this, lastPage, oldLastPage, perPage); });
else
$(spanNode).remove();
}
// Create the Ajax loading icon, and add a link to turn it off.
// 'where' should be empty to center on screen, or a DOM element to serve as the center point.
// 'exact' is an optional array of two numbers, indicating a top/left offset to be applied on top of where's.
// e.g., show_ajax($notif_button, [0, 20]) will show the Ajax popup 20 pixels below the center of the notifications button.
function show_ajax(where, exact)
{
// We're delaying the creation a bit, to account for super-fast AJAX (e.g. local server, caching, etc.)
window.ajax = setTimeout(function ()
{
var offs = $(where).offset();
$('<div id="ajax">')
.html('<a title="' + (we_cancel || '') + '"></a>' + we_loading)
.click(hide_ajax)
.addClass('anim')
.appendTo('body');
// window.innerWidth/Height, if set, holds the actual viewport size.
$('#ajax').offset({
left: (offs && offs.left || 0) + (exact && exact[0] || 0) + (where ? $(where).outerWidth() : Math.min(window.innerWidth || $(window).width(), $(window).width())) / 2 - $('#ajax').outerWidth() / 2,
top: (offs && offs.top || 0) + (exact && exact[1] || 0) + (where ? $(where).outerHeight() : Math.min(window.innerHeight || $(window).height(), $(window).height())) / 2 - $('#ajax').outerHeight() / 2 + (where ? 0 : $(window).scrollTop())
}).removeClass('anim');
}, 200);
}
function hide_ajax()
{
clearTimeout(window.ajax);
$('#ajax').addClass('anim');
setTimeout(function () { $('#ajax').remove(); }, 200);
}
// Rating boxes in Media area.
function ajaxRating()
{
show_ajax();
$.post(
$('#ratingF').attr('action'),
{ rating: $('#rating').val() },
function (new_contents) {
$('#ratingF').parent().html(new_contents);
$('#rating').sb();
hide_ajax();
}
);
}
// This function takes the script URL, and adds a question mark (or semicolon) so we can
// append a query string (url) to it. It also replaces the host name with the current one,
// which is sometimes required for security reasons.
function weUrl(url)
{
return we_script.replace(/:\/\/[^\/]+/g, '://' + location.host) + (we_script.indexOf('?') == -1 ? '?' : (we_script.search(/[?&;]$/) == -1 ? ';' : '')) + url;
}
// Get the text in a code tag.
function weSelectText(oCurElement)
{
// The place we're looking for is one div up, and next door - if it's auto detect.
var oCodeArea = oCurElement.parentNode.nextSibling, oCurRange;
if (!!oCodeArea)
{
// Start off with IE
if ('createTextRange' in document.body)
{
oCurRange = document.body.createTextRange();
oCurRange.moveToElementText(oCodeArea);
oCurRange.select();
}
// Firefox et al.
else if (window.getSelection)
{
var oCurSelection = window.getSelection();
// Safari is special!
if (oCurSelection.setBaseAndExtent)
{
var oLastChild = oCodeArea.lastChild;
oCurSelection.setBaseAndExtent(oCodeArea, 0, oLastChild, (oLastChild.innerText || oLastChild.textContent).length);
}
else
{
oCurRange = document.createRange();
oCurRange.selectNodeContents(oCodeArea);
oCurSelection.removeAllRanges();
oCurSelection.addRange(oCurRange);
}
}
}
return false;
}
/**
* Drag & slide plugin.
* You may set an area as non-draggable by adding the nodrag class to it.
* This way, you can drag the element, but still access UI elements within it.
*/
$.fn.ds = function ()
{
var origMouse, currentPos, currentDrag = 0;
// Updates the position during the dragging process
$(document)
.mousemove(function (e) {
if (currentDrag)
{
$(currentDrag).css({
left: currentPos.x + e.pageX - origMouse.x,
top: currentPos.y + e.pageY - origMouse.y
});
return false;
}
})
.mouseup(function () {
if (currentDrag)
return !!(currentDrag = 0);
});
this // 'return this' to make it chainable... Costs extra space.
.css('cursor', 'move')
// Start the dragging process
.mousedown(function (e) {
if ($(e.target).closest('.nodrag').length)
return true;
$(this).css('zIndex', 999);
origMouse = { x: e.pageX, y: e.pageY };
currentPos = { x: parseInt(this.offsetLeft, 10), y: parseInt(this.offsetTop, 10) };
currentDrag = this;
return false;
})
.find('.nodrag')
.css('cursor', 'default');
};
/**
* Dropdown menu in JS with CSS fallback, Nao style.
* May not show, but it took years to refine it.
*/
$.fn.mm = function ()
{
this.find('li')
// Entering a menu entry?
.on('mouseenter focus', function ()
{
// Start the animation on its child.
$('>ul', this).addClass('anim');
// Add hover effects to this menu entry's parent list. On top-level entries, add effect to the h4 tag only.
if (!$(this).parent().hasClass('menu') || !$('>h4', this).addClass('hove').length)
$(this).addClass('hove').parentsUntil('.menu>li').filter('li').addClass('hove');
$(this).siblings('li').children().andSelf().removeClass('hove anim');
})
// Leaving a menu entry? Then hide all of its children.
.on('mouseleave blur', function (e)
{
$(this).children().andSelf().removeClass('hove anim');
})
// Disable double clicks and text selection...
.mousedown(false)
// Clicking a link will immediately close the menu -- giving a feeling of responsiveness.
.find('>a,>h4>a')
.click(function () {
$('.hove').removeClass('hove');
$(this).parentsUntil('.menu').removeClass('anim');
});
// Make menu icons clickable by stealing the link from their neighbor.
$('.menu>li>span').each(function () { $(this).wrap($('<a/>').attr('href', $(this).next().find('a').attr('href'))); });
};
/**
* Mini-menu (mime) plugin. Yay.
* Supposed to be generic, but good luck using it in plugins...
*/
$.fn.mime = function (oList, oStrings)
{
this.each(function ()
{
if (this.className.indexOf('processed') >= 0 || $(this).addClass('processed').parent().hasClass('mime'))
return;
var
$mime = $(this),
id = $mime.data('id') || ($mime.closest('.msg').attr('id') || '').slice(3), // Extract the context id from the parent message
$men = $('<div class="mimenu"><ul class="actions"></ul></div>').hide(),
pms;
$.each(oList ? oList[id] || [] : [], function ()
{
pms = oStrings[this.slice(0, 2)];
$('<a>')
.html(pms[0].wereplace({ 1: id, 2: this.slice(3) }))
.attr('title', pms[1])
.attr('class', pms[3])
.attr('href', pms[2] ? (pms[2][0] == '?' ? $mime.attr('href') || '' : '') + pms[2].wereplace({ 1: id, 2: this.slice(3) }) : $mime.attr('href') || '')
.click(new Function('e', pms[4] ? pms[4].wereplace({ 1: id, 2: this.slice(3) }) : '')) // eval, bad! No user input, good!
.click(function () { $men.stop(true).animate(hiddenPosition, 200, function () { $men.hide(); }); }) // Close menu if clicked.
.wrap('<li>').parent()
.appendTo($men.find('ul'));
});
$mime.wrap('<span class="mime"></span>').after($men).parent().hover(
function () { $men.stop(true).show().animate(visiblePosition, 300, function () { $men.css('overflow', 'visible'); }); },
function () { $men.stop(true).animate(hiddenPosition, 200, function () { $men.hide(); }); }
);
var
wi = $men.width(),
he = $men.height(),
// If we start from halfway into it, the animation looks nicer.
hiddenPosition = {
opacity: 0,
width: wi / 2,
height: he / 2,
paddingTop: 0
},
visiblePosition = {
opacity: 1,
width: wi,
height: he,
paddingTop: $men.css('paddingTop')
};
$men.css(hiddenPosition).toggleClass('right', $mime.offset().left > $(window).width() / 2);
});
};
/**
* ready()
* This function is important. It loads as soon as the DOM is built, i.e. after everything's loaded.
*/
$(function ()
{
breakLinks();
// Transform existing select boxes into our super boxes.
$('select').sb();
// Save a copy of our untreated menu, for responsiveness.
$('#main_menu').clone().removeClass('css menu').attr('id', 'sidemenu').prependTo('#sidebar>div');
// Now replace our pure CSS menus with JS-powered versions.
$('.menu').removeClass('css').mm();
// Bind all delayed inline events to their respective DOM elements.
$('[data-eve]').each(function ()
{
var that = $(this);
$.each(that.attr('data-eve').split(' '), function () {
that.on(eves[this][0], eves[this][1]);
});
});
if (is_touch)
{
// Disable parent links on hover-impaired browsers.
$('.umme,.subsection>a,.menu>li:not(.nodrop)>h4>a').click(false);
// Disable zooming when focusing an input box or textarea.
var meta = $('meta[name=viewport]'), initial_meta = meta.attr('content');
$('input,textarea')
.focus(function () { meta.attr('content', initial_meta + ',maximum-scale=1,user-scalable=0'); })
.blur(function () { meta.attr('content', initial_meta); });
}
var
orig_sid,
orig_wid,
sidebar_on_right,
old_y = 0,
sidebar_shown = false,
do_hardware,
// $main is the first child of the sidebar container that isn't the sidebar.
// Usually, it should be #main or #offside.
$edge = $('#edge'),
$main = $edge.children().not('#sidebar').first(),
show_sidebar = function ()
{
// Don't show the sidebar if it's already visible.
if (sidebar_shown || $('#sideshow').is(':hidden'))
return true;
orig_wid = $main.width();
orig_sid = parseInt($edge.css('left')) || 0;
sidebar_shown = true;
$main.width(orig_wid);
$('#wedge').addClass('sliding');
$('#sidebar').css('display', $main.css('display'));
$('#sidebar>div').css('margin-top', $(window).scrollTop() > $('#sidebar').offset().top ? Math.min(
$('#sidebar').height() - $('#sidebar>div').height(),
$(window).scrollTop() - $('#sidebar').offset().top
) : 0);
$(document).on('mousedown.sw', function (e) {
// HTML implies user clicked on a scrollbar, or at least outside the HTML contents.
if (!$(e.target).closest('#sidebar').length && e.target.tagName != 'HTML')
hide_sidebar();
});
sidebar_on_right = $('#sidebar').offset().left > $main.offset().left;
$edge.css({ left: orig_sid - (sidebar_on_right ? 0 : $('#sidebar').width()) });
// Can't hard-scroll on skins that have the HTML for the side button inside the sidebar container.
if (!$('#sideshow').closest('#edge').length)
$edge.css({ transform: 'translate3d(' + (sidebar_on_right ? '-' : '') + $('#sidebar').width() + 'px,0,0)' });
// And can't hard-scroll on some browsers, like IE < 10.
do_hardware = $edge.css('transform') != 'none';
if (!do_hardware)
$edge.stop(true).animate({ left: orig_sid - (sidebar_on_right ? $('#sidebar').width() : 0) }, 500);
$(window).on('resize.sw', function () {
// Fix the sidebar position if the window has just been enlarged.
if (sidebar_shown && $('#sideshow').is(':hidden'))
hide_sidebar_for_real();
});
},
hide_sidebar_for_real = function ()
{
sidebar_shown = false;
// Remove the style attributes, reset everything and disable hardware acceleration.
$('#sidebar>div').attr('style', '');
$('#sidebar').attr('style', '');
$edge.attr('style', '');
$main.width('');
$(window).off('.sw');
$(document).off('.sw');
$('#wedge').removeClass('sliding');
},
hide_sidebar = function ()
{
if (do_hardware)
{
// Two alternatives, for completeness: (1) $edge.one('transitionend', hide_sidebar_for_real) is clean, but reportedly not supported by webOS.
// (2) the following is bad-ass, as it allows changing the value from within CSS, but it's also a bit overkill... Innit?
// setTimeout(hide_sidebar_for_real, (parseFloat($edge.css('transition-duration')) || .5) * ($edge.css('transition-duration').indexOf('m') == -1 ? 1000 : 1));
setTimeout(hide_sidebar_for_real, 500);
$edge.css({ transform: 'none' });
}
else
$edge.stop(true).animate({ left: orig_sid - (sidebar_on_right ? 0 : $('#sidebar').width())}, 500, hide_sidebar_for_real);
};
// Catch the wheel button (or middle-click), and if it's not attempting to open a link, toggle the sidebar.
$(document).on(is_firefox ? 'mouseup' : 'mousedown', function (e)
{
if (e.which == 2 && !$(e.target).closest('a').andSelf().filter('a').length && !$('#sideshow').is(':hidden') && !$.hasData(e.target))
{
sidebar_shown ? hide_sidebar() : show_sidebar();
e.preventDefault();
}
});
$('<div/>')
.attr('id', 'sideshow')
.attr('title', $txt['sideshow'])
.click(function () { sidebar_shown ? hide_sidebar() : show_sidebar(); })
.append('<div/><div/><div/>')
.prependTo('#top_section>div');
});
/**
* load()
* This is the list of non-critical functions that should be executed once the DOM is fully loaded, including images.
*/
$(window).load(function ()
{
$('#upshrink').attr('title', $txt['upshrink_description']);
// You may change the ID names for the elements in the index template,
// but you'll have to add_js() the code below, with the new IDs in place.
new weToggle({
isCollapsed: !!window.we_colhead,
aSwapContainers: ['upper_section'],
aSwapImages: ['upshrink'],
sOption: 'collapse_header'
});
// Show a pop-up with more options when focusing the quick search box.
var opened, $pop = $('<div class="mimenu right">').appendTo($('#search_form').addClass('mime'));
$('#search_form .search').focus(function ()
{
if (opened)
return;
$pop.load(weUrl('action=search' + (window.we_topic ? ';topic=' + we_topic : '') + (window.we_board ? ';board=' + we_board : '')), function () {
$pop.hide().css('top', 0).animate({
opacity: 'toggle',
top: '100%'
}).find('select').sb();
});
opened = true;
$(document).on('click.sf', function (e) {
if ($(e.target).closest('#search_form').length)
return;
opened = false;
$(document).off('.sf');
$pop.css('top', '100%').animate({
opacity: 'toggle',
top: '0%'
});
}).on('keyup.sf', function (e) {
// keydown target holds previous element, keyup holds next one. Found this by myself, eheh.
if (e.altKey || e.ctrlKey || e.keyCode != 9 || $(e.target).closest('#search_form').length)
return;
opened = false;
$(document).off('.sf');
$pop.css('top', '100%').animate({
opacity: 'toggle',
top: '0%'
});
});
});
});
/**
* UI for notifications.
* Thanks to Dragooon for the initial implementation!
*/
@if member
$(function ()
{
var
is_up_to_date = false,
is_pm_up_to_date = false,
is_opened = false,
is_pm_opened = false,
original_title = document.title,
$notif_button = $('.notifs.notif'),
$pm_button = $('.notifs.npm'),
$shade = $('<div/>').addClass('mimenu').appendTo($notif_button),
$pmshade = $('<div/>').addClass('mimenu').appendTo($pm_button),
toggle_me = function ()
{
is_opened = !is_opened;
$shade.toggleClass('open');
$notif_button.toggleClass('hover');
// Hide popup when clicking elsewhere.
$(document).off('click.no');
if (!is_opened) // Skipping the 'return' compresses badly. Sigh.
return;
$(document).on('click.no', function (e)
{
if ($(e.target).closest($notif_button).length)
return;
is_opened = !is_opened;
$shade.toggleClass('open');
$notif_button.toggleClass('hover');
$(document).off('click.no');
});
},
toggle_me_pm = function ()
{
is_pm_opened = !is_pm_opened;
$pmshade.toggleClass('open');
$pm_button.toggleClass('hover');
// Hide popup when clicking elsewhere.
$(document).off('click.no');
if (!is_pm_opened)
return;
$(document).on('click.no', function (e)
{
if ($(e.target).closest($pm_button).length)
return;
is_pm_opened = !is_pm_opened;
$pmshade.toggleClass('open');
$pm_button.toggleClass('hover');
$(document).off('click.no');
});
},
// url: URL to the page to load.
// toggle: set to false to skip the animation.
// $button: selector that once clicked, opens the popup.
// url_preview: URL to the page to load when requesting a preview.
// toggle_callback: false, or a function to run when toggling popups.
// preview_callback: false, or a function to run when toggling previews.
// is_generic_notification: always use false. For now.
show_notification = function (url, toggle, $button, $popup, url_preview, toggle_callback, preview_callback, is_generic_notification)
{
show_ajax($button, [0, 30]);
$popup.load(url, function (data)
{
hide_ajax();
// Opening the popup accomplished .note's mission, so we can remove that.
$popup.prev().addClass('notevoid').removeClass('note');
$(this).find('.n_container')
.css('max-height', ($(window).height() - $(this).find('.n_container').offset().top) * .9)
.closest('ul')
.css('max-width', $(window).width() * .95);
$(this).find('.n_item').each(function ()
{
var that = $(this), id = $(this).data('id');
$(this)
.hover(function () { $(this).toggleClass('windowbg3').find('.n_read').toggle(); })
.click(function ()
{
// Try to toggle the preview. If it doesn't exist, create it.
if (!that.next('.n_prev').stop(true, true).slideToggle(600).length && that.data('prev') != 'no')
{
show_ajax(this);
$.post(
weUrl(url_preview.wereplace({ id: id })),
function (doc) {
hide_ajax();
$('<div/>').addClass('n_prev').html(doc).insertAfter(that).hide().slideToggle(600);
if (preview_callback)
preview_callback.call(that, doc, id);
}
);
}
else if (preview_callback)
preview_callback.call(that, '', id);
});
if (is_generic_notification)
$(this)
.find('.n_read')
.hover(function () { $(this).toggleClass('windowbg'); })
.click(function (e)
{
var was_new = that.hasClass('n_new');
that.removeClass('n_new').next('.n_prev').andSelf().hide(300, function () { $(this).remove(); });
if (was_new)
{
we_notifs--;
$shade.prev().text(we_notifs);
document.title = (we_notifs > 0 ? '(' + we_notifs + ') ' : '') + original_title;
$.post(weUrl('action=notification;sa=markread;in=' + id));
}
// Cancel the implied click on the parent.
e.stopImmediatePropagation();
return false;
});
});
if (toggle && toggle_callback)
toggle_callback.call(this);
});
};
notload = function (url, toggle)
{
show_notification(
url,
toggle,
$notif_button,
$shade,
'action=notification;sa=preview;in=%id%',
toggle_me,
function (doc, id)
{
if (this.hasClass('n_new'))
{
this.removeClass('n_new');
we_notifs--;
$shade.prev().text(we_notifs);
document.title = (we_notifs > 0 ? '(' + we_notifs + ') ' : '') + original_title;
$.post(weUrl('action=notification;sa=markread;in=' + id));
}
},
true
);
};
$notif_button.click(function (e)
{
if (e.target != this)
return true;
if (is_pm_opened)
toggle_me_pm();
if (!is_up_to_date)
{
notload(weUrl('action=notification'), true);
is_up_to_date = true;
}
else
toggle_me();
});
$pm_button.click(function (e)
{
if (e.target != this)
return true;
if (is_opened)
toggle_me();
if (!is_pm_up_to_date)
{
// Multiple things go back to PMs via AJAX. We want to be explicit to keep the PM code simpler...
show_notification(
weUrl('action=pm;sa=ajax'),
true,
$pm_button,
$pmshade,
'action=pm;sa=ajax;preview=%id%',
toggle_me_pm,
false
);
is_pm_up_to_date = true;
}
else
toggle_me_pm();
});
// Update the notification count...
var auto_update = function ()
{
$.post(weUrl('action=notification;sa=unread'), function (count)
{
// The response is x;y where x = notifications, y = unread PMs
count = count.split(';');
if (count[0] !== '' && count[0] != window.we_notifs)
{
we_notifs = count[0];
is_up_to_date = false;
$shade.prev().text(we_notifs);
document.title = (we_notifs > 0 ? '(' + we_notifs + ') ' : '') + original_title;
}
if (count[1] !== '-1' && count[1] != window.we_pms)
{
we_pms = count[1];
is_pm_up_to_date = false;
$pmshade.prev().text(we_pms);
}
});
setTimeout(auto_update, document.hidden || document.webkitHidden || document.mozHidden || document.msHidden || is_ie8down ? 600000 : 60000);
};
// Call this every minute or 10 minutes, depending on page visibility. Note that old browsers will call every minute on every tab... Not good :-/
setTimeout(auto_update, document.hidden || document.webkitHidden || document.mozHidden || document.msHidden || is_ie8down ? 600000 : 60000);
});
@endif
// *** weToggle class.
function weToggle(opt)
{
var
that = this,
collapsed = false,
toggle_me = function () {
$(this).data('that').toggle();
this.blur();
return false;
};
// Change State - collapse or expand the section.
this.cs = function (bCollapse, bInit)
{
// Handle custom function hook before collapse.
if (!bInit && bCollapse && opt.onCollapse)
opt.onCollapse.call(this);
// Handle custom function hook before expand.
else if (!bInit && !bCollapse && opt.onExpand)
opt.onExpand.call(this);
// Loop through all the images that need to be toggled.
$.each(opt.aSwapImages || [], function () {
$('#' + this).toggleClass('fold', !bCollapse).attr('title', opt.title || $txt['upshrink_description']);
});
// Now go through all the sections to be collapsed.
$.each(opt.aSwapContainers || [], function () {
$('#' + this)[bCollapse ? 'slideUp' : 'slideDown'](bInit ? 0 : 250);
});
// Update the new state.
collapsed = +bCollapse;
@if guest
// Update the cookie, if desired.
if (opt.sOption)
document.cookie = opt.sOption + '=' + collapsed;
@else
// Set a theme option through JavaScript.
if (!bInit && opt.sOption)
$.post(weUrl('action=ajax;sa=opt;' + we_sessvar + '=' + we_sessid + (opt.sExtra || '')), { v: opt.sOption, val: collapsed });
@endif
};
// Reverse the current state.
this.toggle = function ()
{
this.cs(!collapsed);
};
// Note that this is only used in stats.js...
this.opt = opt;