1
+ /*
2
+ Copyright (C) 2013 EllisLab, Inc.
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in
12
+ all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17
+ ELLISLAB, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
+
21
+ Except as contained in this notice, the name of EllisLab, Inc. shall not be
22
+ used in advertising or otherwise to promote the sale, use or other dealings
23
+ in this Software without prior written authorization from EllisLab, Inc.
24
+ */
25
+
26
+ $ . fn . CommentEditor = function ( options ) {
27
+
28
+ var OPT ;
29
+
30
+ OPT = $ . extend ( {
31
+ url : "{exp:comment:ajax_edit_url}" ,
32
+ comment_body : '.comment_body' ,
33
+ showEditor : '.edit_link' ,
34
+ hideEditor : '.cancel_edit' ,
35
+ saveComment : '.submit_edit' ,
36
+ closeComment : '.mod_link'
37
+ } , options ) ;
38
+
39
+ var view_elements = [ OPT . comment_body , OPT . showEditor , OPT . closeComment ] . join ( ',' ) ,
40
+ edit_elements = '.editCommentBox' ,
41
+ hash = '{XID_HASH}' ;
42
+
43
+ return this . each ( function ( ) {
44
+ var id = this . id . replace ( 'comment_' , '' ) ,
45
+ parent = $ ( this ) ;
46
+
47
+ parent . find ( OPT . showEditor ) . click ( function ( ) { showEditor ( id ) ; return false ; } ) ;
48
+ parent . find ( OPT . hideEditor ) . click ( function ( ) { hideEditor ( id ) ; return false ; } ) ;
49
+ parent . find ( OPT . saveComment ) . click ( function ( ) { saveComment ( id ) ; return false ; } ) ;
50
+ parent . find ( OPT . closeComment ) . click ( function ( ) { closeComment ( id ) ; return false ; } ) ;
51
+ } ) ;
52
+
53
+ function showEditor ( id ) {
54
+ $ ( "#comment_" + id )
55
+ . find ( view_elements ) . hide ( ) . end ( )
56
+ . find ( edit_elements ) . show ( ) . end ( ) ;
57
+ }
58
+
59
+ function hideEditor ( id ) {
60
+ $ ( "#comment_" + id )
61
+ . find ( view_elements ) . show ( ) . end ( )
62
+ . find ( edit_elements ) . hide ( ) ;
63
+ }
64
+
65
+ function closeComment ( id ) {
66
+ var data = { status : "close" , comment_id : id , XID : hash } ;
67
+
68
+ $ . post ( OPT . url , data , function ( res ) {
69
+ if ( res . error ) {
70
+ return $ . error ( 'Could not moderate comment.' ) ;
71
+ }
72
+
73
+ hash = res . XID ;
74
+ $ ( 'input[name=XID]' ) . val ( hash ) ;
75
+ $ ( '#comment_' + id ) . hide ( ) ;
76
+ } ) ;
77
+ }
78
+
79
+ function saveComment ( id ) {
80
+ var content = $ ( "#comment_" + id ) . find ( '.editCommentBox' + ' textarea' ) . val ( ) ,
81
+ data = { comment : content , comment_id : id , XID : hash } ;
82
+
83
+ $ . post ( OPT . url , data , function ( res ) {
84
+ if ( res . error ) {
85
+ hideEditor ( id ) ;
86
+ return $ . error ( 'Could not save comment.' ) ;
87
+ }
88
+
89
+ hash = res . XID ;
90
+ $ ( 'input[name=XID]' ) . val ( hash ) ;
91
+ $ ( "#comment_" + id ) . find ( '.comment_body' ) . html ( res . comment ) ;
92
+ hideEditor ( id ) ;
93
+ } ) ;
94
+ }
95
+ } ;
96
+
97
+
98
+ $ ( function ( ) { $ ( '.comment' ) . CommentEditor ( ) ; } ) ;
0 commit comments