/
lqt.js
1638 lines (1325 loc) · 47.7 KB
/
lqt.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
/**
* LiquidThreads core javascript library.
*
* Exposes global object `liquidThreads`.
* Exposes static method `jQuery.getCSS`.
*
* FIXME: This module uses deprecated jQuery.browser.
*/
/*global liquidThreads, alert, wgWikiEditorPreferences, mwSetupToolbar */
( function ( mw, $ ) {
window.wgWikiEditorIconVersion = 0;
$.getCSS = function ( url, media ) {
$( '<link>' ).attr( {
href: url,
media: media || 'screen',
type: 'text/css',
rel: 'stylesheet'
} ).appendTo( 'head' );
};
window.liquidThreads = {
currentReplyThread : null,
currentToolbar : null,
'handleReplyLink' : function ( e ) {
if ( e.preventDefault ) {
e.preventDefault();
}
var target = this;
if ( !this.className && e.target) {
target = $( e.target );
}
var container = $( target ).closest( '.lqt_thread' )[0];
var thread_id = $( this ).data( 'thread-id' );
// hide the form for this thread if it's currently being shown
if ( thread_id === liquidThreads.currentReplyThread && $( '#wpTextbox1' ).is( ':visible' ) ) {
liquidThreads.cancelEdit( {} );
return;
}
var params = { 'method' : 'reply', 'thread' : thread_id };
var repliesElement = $( container ).contents().filter( '.lqt-thread-replies' );
var replyDiv = repliesElement.contents().filter( '.lqt-reply-form' );
replyDiv = replyDiv.add( $( container ).contents().filter( '.lqt-reply-form' ) );
if ( !replyDiv.length ) {
// Create a div for it
replyDiv = $( '<div class="lqt-reply-form lqt-edit-form"/>' );
// Try to find a place for it
if ( !repliesElement.length ) {
repliesElement = liquidThreads.getRepliesElement( $( container ) );
}
repliesElement.find( '.lqt-replies-finish' ).before( replyDiv );
}
replyDiv.show();
replyDiv = replyDiv[0];
liquidThreads.injectEditForm( params, replyDiv, e.preload );
liquidThreads.currentReplyThread = thread_id;
},
'getRepliesElement' : function ( thread /* a .lqt_thread */ ) {
var repliesElement = thread.contents().filter( '.lqt-thread-replies' );
if ( !repliesElement.length ) {
repliesElement = $( '<div class="lqt-thread-replies"/>' );
var finishDiv = $( '<div class="lqt-replies-finish"/>' );
finishDiv.append( $( '<div class="lqt-replies-finish-corner"/>' ) );
finishDiv.contents().html( ' ' );
repliesElement.append( finishDiv );
var repliesFinishElement = thread.contents().filter( '.lqt-replies-finish' );
if ( repliesFinishElement.length ) {
repliesFinishElement.before( repliesElement );
} else {
thread.append( repliesElement );
}
}
return repliesElement;
},
'checkEmptyReplies' : function ( element, action ) {
var contents = element.contents();
contents = contents.not( '.lqt-replies-finish,.lqt-post-sep,.lqt-edit-form' );
if ( !contents.length ) {
if ( action === undefined || action === 'remove' ) {
element.remove();
} else {
element.hide();
}
}
},
'handleNewLink' : function ( e ) {
e.preventDefault();
var talkpage = $( this ).attr( 'lqt_talkpage' );
var params = { 'talkpage' : talkpage, 'method' : 'talkpage_new_thread' };
var container = $( '.lqt-new-thread' );
container.data( 'lqt-talkpage', talkpage );
liquidThreads.injectEditForm( params, container );
liquidThreads.currentReplyThread = 0;
},
'handleEditLink' : function ( e ) {
e.preventDefault();
// Grab the container.
var parent = $( this ).closest( '.lqt-post-wrapper' );
var container = $( '<div/>' ).addClass( 'lqt-edit-form' );
parent.contents().fadeOut();
parent.append( container );
var params = { 'method' : 'edit', 'thread' : parent.data( 'thread-id' ) };
liquidThreads.injectEditForm( params, container );
},
'injectEditForm' : function ( params, container, preload ) {
var page = $( container ).closest( '.lqt-thread-topmost' )
.find( '.lqt-thread-talkpage-metadata' ).val();
if ( !page ) {
page = $( container ).data( 'lqt-talkpage' );
}
liquidThreads.cancelEdit( container );
var isIE7 = $.client.test( { 'msie': [['>=', 7], ['<', 8]] },
$.client.profile(), true );
var loadSpinner = $( '<div class="mw-ajax-loader lqt-loader"/>' );
$( container ).before( loadSpinner );
var finishShow = function () {
// Scroll to the textbox
var targetOffset = $( container ).offset().top;
var windowHeight = $( window ).height();
var editBoxHeight = $( container ).height();
var scrollOffset;
if ( windowHeight < editBoxHeight ) {
scrollOffset = targetOffset;
} else {
scrollOffset = targetOffset - windowHeight + editBoxHeight;
}
$( 'html, body' ).animate( { scrollTop: scrollOffset }, 'slow' );
// Auto-focus and set to auto-grow as well
$( container ).find( '#wpTextbox1' ).focus();//.autogrow();
// Focus the subject field if there is one. Overrides previous line.
$( container ).find( '#lqt_subject_field' ).focus();
// Update signature editor
$( container ).find( 'input[name=wpLqtSignature]' ).hide();
$( container ).find( '.lqt-signature-preview' ).show();
var editLink = $( '<a class="lqt-signature-edit-button"/>' );
editLink.text( mw.msg( 'lqt-edit-signature' ) );
editLink.click( liquidThreads.handleEditSignature );
editLink.attr( 'href', '#' );
$( container ).find( '.lqt-signature-preview' ).after( editLink );
editLink.before( ' ' );
};
var finishSetup = function () {
// Kill the loader.
$( '.lqt-loader' ).remove();
if ( preload ) {
$( 'textarea', container )[0].value = preload;
}
if ( isIE7 ) {
setTimeout( finishShow, 500 );
} else {
$( container ).slideDown( 'slow', finishShow );
}
var cancelButton = $( container ).find( '#mw-editform-cancel' );
cancelButton.click( liquidThreads.cancelEdit );
$( container ).find( '#wpTextbox1' ).attr( 'rows', 12 );
$( container ).find( '#wpDiff' ).hide();
if ( $.fn.wikiEditor && $.wikiEditor.isSupported( $.wikiEditor.modules.toolbar ) ) {
var useDialogs;
if ( typeof wgWikiEditorPreferences !== 'undefined' ) {
useDialogs = wgWikiEditorPreferences.toolbar.dialogs;
} else {
useDialogs = mw.user.options.get( 'usebetatoolbar-cgd' );
}
if ( useDialogs && $.wikiEditor.isSupported( $.wikiEditor.modules.dialogs ) ) {
$( '#wpTextbox1' ).addClass( 'toolbar-dialogs' );
}
// Add wikiEditor toolbar
$( '#wpTextbox1' ).wikiEditor( 'addModule', { 'toolbar': liquidThreads.toolbar.config, 'dialogs': liquidThreads.toolbar.dialogs } );
// cleanup unnecessary things from the old toolbar
$( '#editpage-specialchars' ).remove();
$( '#wpTextbox1' ).focus();
} else {
// Add old toolbar
mwSetupToolbar(); // Fix it. Can't locate this.
}
var currentFocused = $( container ).find( '#wpTextbox1' );
mw.hook( 'ext.lqt.textareaCreated' ).fire( currentFocused );
$( container ).find( '#wpTextbox1,#wpSummary' ).focus( function () {
currentFocused = this;
} );
};
mw.loader.using( ['mediawiki.action.edit'],
function () {
if ( isIE7 ) {
$( container ).empty().show();
}
liquidThreads.loadInlineEditForm( params, container, function () {
mw.loader.using(
[ 'ext.wikiEditor', 'user.options',
'jquery.wikiEditor.toolbar', 'jquery.wikiEditor.dialogs',
'jquery.async', 'jquery.cookie' ],
finishSetup
);
} );
} );
},
'loadInlineEditForm' : function ( params, container, callback ) {
params.action = 'threadaction';
params.threadaction = 'inlineeditform';
params.token = mw.user.tokens.get( 'editToken' );
( new mw.Api() ).post( params ).done( function ( result ) {
$( container ).empty().append( $( result.threadaction.inlineeditform.html ).contents() );
callback();
} );
},
//From http://clipmarks.com/clipmark/CEFC94CB-94D6-4495-A7AA-791B7355E284/
'insertAtCursor' : function ( myField, myValue ) {
//IE support
if ( document.selection ) {
myField.focus();
var sel = document.selection.createRange();
sel.text = myValue;
}
//MOZILLA/NETSCAPE support
else if ( myField.selectionStart || myField.selectionStart === '0' ) {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring( 0, startPos ) + myValue + myField.value.substring( endPos, myField.value.length );
} else {
myField.value += myValue;
}
},
'getSelection' : function () {
if ( window.getSelection ) {
return window.getSelection().toString();
} else if ( document.selection ) {
return document.selection.createRange().text;
} else if ( document.getSelection ) {
return document.getSelection();
} else {
return '';
}
},
'cancelEdit' : function ( e ) {
if ( e !== undefined && e.preventDefault ) {
e.preventDefault();
}
// XXX: Should this be e.target instead of e?
$( '.lqt-edit-form' ).not( e ).each(
function () {
var repliesElement = $( this ).closest( '.lqt-thread-replies' );
$( this ).fadeOut( 'slow',
function () {
$( this ).empty();
if ( $( this ).parent().is( '.lqt-post-wrapper' ) ) {
$( this ).parent().contents().fadeIn();
$( this ).remove();
}
liquidThreads.checkEmptyReplies( repliesElement );
} );
} );
liquidThreads.currentReplyThread = null;
},
'setupMenus' : function () {
var post = $( this );
var toolbar = post.contents().filter( '.lqt-thread-toolbar' );
var threadID = post.data( 'thread-id' );
var menu = post.find( '.lqt-thread-toolbar-command-list' );
var menuContainer = post.find( '.lqt-thread-toolbar-menu' );
menu.remove().appendTo( menuContainer );
menuContainer.find( '.lqt-thread-toolbar-command-list' ).hide();
// Add handler for reply link
var replyLink = toolbar.find( '.lqt-command-reply > a' );
replyLink.data( 'thread-id', threadID );
replyLink.click( liquidThreads.handleReplyLink );
// Add "Drag to new location" to menu
var dragLI = $( '<li class="lqt-command-drag lqt-command" />' );
var dragLink = $( '<a/>' ).text( mw.msg( 'lqt-drag-activate' ) );
dragLink.attr( 'href', '#' );
dragLI.append( dragLink );
dragLink.click( liquidThreads.activateDragDrop );
menu.append( dragLI );
// Remove split and merge
menu.contents().filter( '.lqt-command-split,.lqt-command-merge' ).remove();
var trigger = menuContainer.find( '.lqt-thread-actions-trigger' );
trigger.show();
menu.hide();
// FIXME: After a drag-and-drop, this stops working on the thread and its replies
trigger.click(
function ( e ) {
e.stopImmediatePropagation();
e.preventDefault();
// Hide the other menus
$( '.lqt-thread-toolbar-command-list' ).not( menu ).hide( 'fast' );
menu.toggle( 'fast' );
var windowHeight = $( window ).height();
var toolbarOffset = toolbar.offset().top;
var scrollPos = $( window ).scrollTop();
var menuBottom = ( toolbarOffset + 150 - scrollPos );
if ( menuBottom > windowHeight ) {
// Switch to an upwards menu.
menu.css( 'bottom', toolbar.height() );
} else {
menu.css( 'bottom', 'auto' );
}
} );
},
'setupThreadMenu' : function ( menu, id ) {
if ( menu.find( '.lqt-command-edit-subject' ).length ||
menu.closest( '.lqt_thread' ).is( '.lqt-thread-uneditable' )
) {
return;
}
var editSubjectField = $( '<li/>' );
var editSubjectLink = $( '<a href="#"/>' );
editSubjectLink.text( mw.msg( 'lqt-change-subject' ) );
editSubjectField.append( editSubjectLink );
editSubjectField.click( liquidThreads.handleChangeSubject );
editSubjectField.data( 'thread-id', id );
editSubjectField.addClass( 'lqt-command-edit-subject' );
// appending a space first to prevent cursive script character joining across elements
menu.append( ' ', editSubjectField );
},
'handleChangeSubject' : function ( e ) {
e.preventDefault();
$( this ).closest( '.lqt-command-edit-subject' ).hide();
// Grab the h2
var threadId = $( this ).data( 'thread-id' );
var header = $( '#lqt-header-' + threadId );
var headerText = header.find( "input[name='raw-header']" ).val();
var textbox = $( '<input type="textbox" />' ).val( headerText );
textbox.attr( 'id', 'lqt-subject-input-' + threadId );
textbox.attr( 'size', '75' );
textbox.val( headerText );
var saveText = mw.msg( 'lqt-save-subject' );
var saveButton = $( '<input type="button" />' );
saveButton.val( saveText );
saveButton.click( liquidThreads.handleSubjectSave );
var cancelButton = $( '<input type="button" />' );
cancelButton.val( mw.msg( 'lqt-cancel-subject-edit' ) );
cancelButton.click( function () {
var form = $( this ).closest( '.mw-subject-editor' );
var header = form.closest( '.lqt_header' );
header.contents().filter( '.mw-headline' ).show();
header.next().find( '.lqt-command-edit-subject' ).show();
form.remove();
} );
header.contents().filter( 'span.mw-headline' ).hide();
var subjectForm = $( '<span class="mw-subject-editor"/>' );
subjectForm.append( textbox );
subjectForm.append( ' ' );
subjectForm.append( saveButton );
subjectForm.append( ' ' );
subjectForm.append( cancelButton );
subjectForm.data( 'thread-id', threadId );
header.append( subjectForm );
},
handleSubjectSave: function () {
var button = $( this );
var subjectForm = button.closest( '.mw-subject-editor' );
var header = subjectForm.closest( '.lqt_header' );
var threadId = subjectForm.data( 'thread-id' );
var textbox = $( '#lqt-subject-input-'+threadId );
var newSubject = $.trim( textbox.val() );
if ( !newSubject ) {
alert( mw.msg( 'lqt-ajax-no-subject' ) );
return;
}
// Add a spinner
var spinner = $( '<div class="mw-ajax-loader"/>' );
header.append( spinner );
subjectForm.hide();
var request = {
action: 'threadaction',
threadaction: 'setsubject',
subject: $.trim( newSubject ),
thread: threadId,
token: mw.user.tokens.get( 'editToken' )
};
// Set new subject through API.
( new mw.Api() ).post( request ).done( function ( reply ) {
var result;
try {
result = reply.threadaction.thread.result;
} catch ( err ) {
result = 'error';
}
if ( result === 'success' ) {
spinner.remove();
header.next().find( '.lqt-command-edit-subject' ).show();
liquidThreads.doReloadThread( $( '#lqt_thread_id_' + threadId ) );
} else {
var code, description;
try {
code = reply.error.code;
description = reply.error.info;
if ( code === 'invalid-subject' ) {
alert( mediaWiki.msg( 'lqt-ajax-invalid-subject' ) );
}
subjectForm.show();
spinner.remove();
} catch ( err ) {
alert( mediaWiki.msg( 'lqt-save-subject-error-unknown' ) );
subjectForm.remove();
spinner.remove();
header.contents().filter( '.mw-headline' ).show();
header.next().find( '.lqt-command-edit-subject' ).show();
}
}
} );
},
handleDocumentClick: function () {
// Collapse all menus
$( '.lqt-thread-toolbar-command-list' ).hide( 'fast' );
},
'checkForUpdates' : function () {
var threadModifiedTS = {};
var threads = [];
$( '.lqt-thread-topmost' ).each( function () {
var tsField = $( this ).find( '.lqt-thread-modified' );
if ( tsField.length ) {
var oldTS = tsField.val();
// Prefix is lqt-thread-modified-
var threadID = tsField.attr( 'id' ).substr( 'lqt-thread-modified-'.length );
threadModifiedTS[threadID] = oldTS;
threads.push( threadID );
}
} );
// Optimisation: if no threads are to be checked, do not check.
if ( !threads.length ) {
return;
}
( new mw.Api() ).get( {
'action': 'query',
'list' : 'threads',
'thid' : threads.join( '|' ),
'thprop': 'id|subject|parent|modified'
} ).done ( function ( data ) {
var threads = data.query.threads;
$.each( threads, function ( i, thread ) {
var threadID = thread.id;
var threadModified = thread.modified;
if ( threadModified !== threadModifiedTS[threadID] ) {
liquidThreads.showUpdated( threadID );
}
} );
} );
},
'showUpdated' : function ( id ) {
// Check if there's already an updated marker here
var threadObject = $( '#lqt_thread_id_' + id );
if ( threadObject.find( '.lqt-updated-notification' ).length ) {
return;
}
var notifier = $( '<div/>' );
notifier.text( mw.msg( 'lqt-ajax-updated' ) + ' ' );
notifier.addClass( 'lqt-updated-notification' );
var updateButton = $( '<a href="#"/>' );
updateButton.text( mw.msg( 'lqt-ajax-update-link' ) );
updateButton.addClass( 'lqt-update-link' );
updateButton.click( liquidThreads.updateThread );
notifier.append( updateButton );
threadObject.prepend( notifier );
},
'updateThread' : function ( e ) {
e.preventDefault();
var thread = $( this ).closest( '.lqt_thread' );
liquidThreads.doReloadThread( thread );
},
'doReloadThread' : function ( thread /* The .lqt_thread */ ) {
var post = thread.find( 'div.lqt-post-wrapper' )[0];
post = $( post );
var threadId = thread.data( 'thread-id' );
var loader = $( '<div class="mw-ajax-loader"/>' );
var header = $( '#lqt-header-' + threadId );
thread.prepend( loader );
// Build an AJAX request
( new mw.Api() ).get( {
action : 'query',
list : 'threads',
thid : threadId,
thrender: 1
} ).done( function ( data ) {
// Load data from JSON
var html = data.query.threads[threadId].content;
var newContent = $( html );
// Clear old post and header.
thread.empty();
thread.hide();
header.empty();
header.hide();
// Replace post content
var newThread = newContent.filter( 'div.lqt_thread' );
var newThreadContent = newThread.contents();
thread.append( newThreadContent );
thread.attr( 'class', newThread.attr( 'class' ) );
// Set up thread.
thread.find( '.lqt-post-wrapper' ).each( function () {
liquidThreads.setupThread( $( this ) );
} );
header.fadeIn();
thread.fadeIn();
// Scroll to the updated thread.
var targetOffset = $( thread ).offset().top;
$( 'html, body' ).animate( { scrollTop: targetOffset }, 'slow' );
} );
},
'setupThread' : function ( threadContainer ) {
var prefixLength = 'lqt_thread_id_'.length;
// Add the interruption class if it needs it
// FIXME: misses a lot of cases
var $parentWrapper = $( threadContainer )
.closest( '.lqt-thread-wrapper' ).parent().closest( '.lqt-thread-wrapper' );
if ( $parentWrapper.next( '.lqt-thread-wrapper' ).length > 0 ) {
$parentWrapper
.find( '.lqt-thread-replies' )
.addClass( 'lqt-thread-replies-interruption' );
}
// Update reply links
var threadWrapper = $( threadContainer ).closest( '.lqt_thread' )[0];
var threadId = threadWrapper.id.substring( prefixLength );
$( threadContainer ).data( 'thread-id', threadId );
$( threadWrapper ).data( 'thread-id', threadId );
// Set up reply link
var replyLinks = $( threadWrapper ).find( '.lqt-add-reply' );
replyLinks.click( liquidThreads.handleReplyLink );
replyLinks.data( 'thread-id', threadId );
// Hide edit forms
$( threadContainer ).find( 'div.lqt-edit-form' ).each(
function () {
if ( $( this ).find( '#wpTextbox1' ).length ) {
return;
}
this.style.display = 'none';
} );
// Update menus
$( threadContainer ).each( liquidThreads.setupMenus );
// Update thread-level menu, if appropriate
if ( $( threadWrapper ).hasClass( 'lqt-thread-topmost' ) ) {
// To perform better, check the 3 elements before the top-level thread container before
// scanning the whole document
var menu,
threadLevelCommandSelector = '#lqt-threadlevel-commands-'+threadId,
traverseElement = $( threadWrapper );
for ( var i = 0; i < 3 && menu === undefined; ++i ) {
traverseElement = traverseElement.prev();
if ( traverseElement.is( threadLevelCommandSelector ) ) {
menu = traverseElement;
}
}
if ( typeof menu === 'undefined' ) {
menu = $( threadLevelCommandSelector );
}
liquidThreads.setupThreadMenu( menu, threadId );
}
},
'showReplies' : function ( e ) {
e.preventDefault();
// Grab the closest thread
var thread = $( this ).closest( '.lqt_thread' ).find( 'div.lqt-post-wrapper' )[0];
thread = $( thread );
var threadId = thread.data( 'thread-id' );
var replies = thread.parent().find( '.lqt-thread-replies' );
var loader = $( '<div class="mw-ajax-loader"/>' );
var sep = $( '<div class="lqt-post-sep"> </div>' );
replies.empty();
replies.hide();
replies.before( loader );
( new mw.Api() ).get( {
action : 'query',
list : 'threads',
thid : threadId,
thrender: '1',
thprop : 'id'
} ).done( function ( data ) {
// Interpret
if ( typeof data.query.threads[threadId] !== 'undefined' ) {
var content = data.query.threads[threadId].content;
content = $( content ).find( '.lqt-thread-replies' )[0];
// Inject
replies.empty().append( $( content ).contents() );
// Remove post separator, if it follows the replies element
if ( replies.next().is( '.lqt-post-sep' ) ) {
replies.next().remove();
}
// Set up
replies.find( 'div.lqt-post-wrapper' ).each( function () {
liquidThreads.setupThread( $( this ) );
} );
replies.before( sep );
// Show
loader.remove();
replies.fadeIn( 'slow' );
}
} );
},
'showMore' : function ( e ) {
e.preventDefault();
// Add spinner
var loader = $( '<div class="mw-ajax-loader"/>' );
$( this ).after( loader );
// Grab the appropriate thread
var thread = $( this ).closest( '.lqt_thread' ).find( 'div.lqt-post-wrapper' )[0];
thread = $( thread );
var threadId = thread.data( 'thread-id' );
// Find the hidden field that gives the point to start at.
var startAtField = $( this ).siblings().filter( '.lqt-thread-start-at' );
var startAt = startAtField.val();
startAtField.remove();
( new mw.Api() ).get( {
action : 'query',
list : 'threads',
thid : threadId,
thrender : '1',
thprop : 'id',
threnderstartrepliesat: startAt
} ).done( function ( data ) {
var content = data.query.threads[threadId].content;
content = $( content ).find( '.lqt-thread-replies' )[0];
content = $( content ).contents();
content = content.not( '.lqt-replies-finish' );
if ( $( content[0] ).is( '.lqt-post-sep' ) ) {
content = content.not( $( content[0] ) );
}
// Inject loaded content.
content.hide();
loader.after( content );
content.find( 'div.lqt-post-wrapper' ).each( function () {
liquidThreads.setupThread( $( this ) );
} );
content.fadeIn();
loader.remove();
} );
$( this ).remove();
},
'asyncWatch' : function ( e ) {
var button = $( this ),
tlcOffset = 'lqt-threadlevel-commands-'.length,
oldButton = button.clone();
// Find the title of the thread
var threadLevelCommands = button.closest( '.lqt_threadlevel_commands' );
var title = $( '#lqt-thread-title-' + threadLevelCommands.attr( 'id' ).substring( tlcOffset ) ).val();
// Check if we're watching or unwatching.
var action = '';
if ( button.hasClass( 'lqt-command-watch' ) ) {
button.removeClass( 'lqt-command-watch' ).addClass( 'lqt-command-unwatch' );
button.find( 'a' ).attr( 'href', button.find( 'a' ).attr( 'href' ).replace( 'watch', 'unwatch' ) ).text( mw.msg( 'unwatch' ) );
action = 'watch';
} else if ( button.hasClass( 'lqt-command-unwatch' ) ) {
button.removeClass( 'lqt-command-unwatch' ).addClass( 'lqt-command-watch' );
action = 'unwatch';
button.find( 'a' ).attr( 'href', button.find( 'a' ).attr( 'href' ).replace( 'unwatch', 'watch' ) ).text( mw.msg( 'watch' ) );
}
// Replace the watch link with a spinner
var spinner = $( '<li/>' ).html( ' ' ).addClass( 'mw-small-spinner' );
button.replaceWith( spinner );
// Check if we're watching or unwatching.
var api = new mw.Api(),
success = function () {
spinner.replaceWith( button );
},
error = function () {
// FIXME: Use a better i18n way to show this
alert( 'failed to connect.. Please try again!' );
spinner.replaceWith( oldButton );
};
if ( action === 'unwatch' ) {
api.unwatch( title ).done( success ).fail( error );
} else if ( action === 'watch' ) {
api.watch( title ).done( success ).fail( error );
}
e.preventDefault();
},
'showThreadLinkWindow' : function ( e ) {
e.preventDefault();
var thread = $( this ).closest( '.lqt_thread' );
var linkTitle = thread.find( '.lqt-thread-title-metadata' ).val();
var linkURL = mw.util.getUrl( linkTitle );
linkURL = mw.config.get( 'wgServer' ) + linkURL;
if ( linkURL.substr( 0, 2 ) === '//' ) {
linkURL = window.location.protocol + linkURL;
}
liquidThreads.showLinkWindow( linkTitle, linkURL );
},
'showSummaryLinkWindow' : function ( e ) {
e.preventDefault();
var linkURL = mw.config.get( 'wgServer' ) + $( this ).attr( 'href' );
if ( linkURL.substr( 0, 2 ) === '//' ) {
linkURL = window.location.protocol + linkURL;
}
var linkTitle = $( this ).parent().find( 'input[name=summary-title]' ).val();
liquidThreads.showLinkWindow( linkTitle, linkURL );
},
'showLinkWindow' : function ( linkTitle, linkURL ) {
linkTitle = '[['+linkTitle+']]';
// Build dialog
var urlLabel = $( '<th/>' ).text( mw.msg( 'lqt-thread-link-url' ) );
var urlField = $( '<td/>' ).addClass( 'lqt-thread-link-url' );
urlField.text( linkURL );
var urlRow = $( '<tr/>' ).append( urlLabel ).append( urlField );
var titleLabel = $( '<th/>' ).text( mw.msg( 'lqt-thread-link-title' ) );
var titleField = $( '<td/>' ).addClass( 'lqt-thread-link-title' );
titleField.text( linkTitle );
var titleRow = $( '<tr/>' ).append( titleLabel ).append( titleField );
var table = $( '<table><tbody></tbody></table>' );
table.find( 'tbody' ).append( urlRow ).append( titleRow );
var dialog = $( '<div/>' ).append( table );
$( 'body' ).prepend( dialog );
dialog.dialog( { 'width' : 600 } );
},
'handleAJAXSave' : function ( e ) {
var editform = $( this ).closest( '.lqt-edit-form' );
var type = editform.find( 'input[name=lqt_method]' ).val();
var wikiEditorContext = editform.find( '#wpTextbox1' ).data( 'wikiEditor-context' );
var text;
if ( !wikiEditorContext || typeof( wikiEditorContext ) === 'undefined' ||
!wikiEditorContext.$iframe ) {
text = editform.find( '#wpTextbox1' ).val();
} else {
text = wikiEditorContext.$textarea.textSelection( 'getContents' );
}
if ( $.trim( text ).length === 0 ) {
alert( mw.msg( 'lqt-empty-text' ) );
return;
}
var summary = editform.find( '#wpSummary' ).val();
var signature;
if ( editform.find( 'input[name=wpLqtSignature]' ).length ) {
signature = editform.find( 'input[name=wpLqtSignature]' ).val();
} else {
signature = undefined;
}
// Check if summary is undefined
if ( summary === undefined ) {
summary = '';
}
var subject = editform.find( '#lqt_subject_field' ).val();
var replyThread = editform.find( 'input[name=lqt_operand]' ).val();
var bumpBox = editform.find( '#wpBumpThread' );
var bump = bumpBox.length === 0 || bumpBox.is( ':checked' );
var spinner = $( '<div class="mw-ajax-loader"/>' );
editform.prepend( spinner );
var replyCallback = function ( data ) {
var $parent = $( '#lqt_thread_id_' + data.threadaction.thread['parent-id'] );
var $html = $( data.threadaction.thread.html );
var $newThread = $html.find( '#lqt_thread_id_' + data.threadaction.thread['thread-id'] );
$parent.find( '.lqt-thread-replies:first' ).append( $newThread );
$parent.closest( '.lqt-thread-topmost' )
.find( 'input.lqt-thread-modified' )
.val( data.threadaction.thread.modified );
liquidThreads.setupThread( $newThread.find( '.lqt-post-wrapper' ) );
$( 'html,body' ).animate( { scrollTop: $newThread.offset().top }, 'slow');
};
var newCallback = function ( data ) {
var $newThread = $( data.threadaction.thread.html );
$( '.lqt-threads' ).prepend( $newThread );
// remove the no threads message if it's on the page
$( '.lqt-no-threads' ).remove();
liquidThreads.setupThread( $newThread.find( '.lqt-post-wrapper' ) );
$( 'html,body' ).animate( { scrollTop: $newThread.offset().top }, 'slow' );
};
var editCallback = function () {
liquidThreads.doReloadThread( editform.closest( '.lqt-thread-topmost' ) );
};
var errorCallback = function () {
// Create a hidden field to mimic the save button, and
// submit it normally, so they'll get a real error message.
var saveHidden = $( '<input/>' );
saveHidden.attr( 'type', 'hidden' );
saveHidden.attr( 'name', 'wpSave' );
saveHidden.attr( 'value', 'Save' );
var form = editform.find( '#editform' );
form.append( saveHidden );
form.parent().data( 'non-ajax-submit', true ); // To avoid edit form open warning
form.submit();
};
var doneCallback = function ( data ) {
var result;
try {
result = data.threadaction.thread.result;
} catch ( err ) {
result = 'error';
}
if ( result !== 'Success' ) {
errorCallback();
return;
}
var callback;
if ( type === 'reply' ) {
callback = replyCallback;
}
if ( type === 'talkpage_new_thread' ) {
callback = newCallback;
}
if ( type === 'edit' ) {
callback = editCallback;
}
editform.empty().hide();
callback( data );
// Load the new TOC
liquidThreads.reloadTOC();
};
if ( type === 'reply' ) {
liquidThreads.doReply( replyThread, text, summary,
doneCallback, bump, signature, errorCallback );
e.preventDefault();
} else if ( type === 'talkpage_new_thread' ) {
var page = editform.closest( '.lqt-new-thread' ).data( 'lqt-talkpage' );
if ( !page ) {
page = $( $( '[lqt_talkpage]' )[0] ).attr( 'lqt_talkpage' ); // A couple of elements have this attribute, it doesn't matter which
}
liquidThreads.doNewThread( page, subject, text, summary,
doneCallback, bump, signature, errorCallback );
e.preventDefault();
} else if ( type === 'edit' ) {
liquidThreads.doEditThread( replyThread, subject, text, summary,
doneCallback, bump, signature, errorCallback );
e.preventDefault();
}