PluginProbe
bbPress / trunk
bbPress vtrunk
2.6.18 2.6.17 trunk 2.0 2.0-beta-1 2.0-beta-2b 2.0-beta-3 2.0-beta-3b 2.0-rc-2 2.0-rc-3 2.0-rc-4 2.0-rc-5 2.0.1 2.0.2 2.0.3 2.1 2.1-beta-1 2.1-rc1 2.1-rc2 2.1-rc3 2.1-rc4 2.1.1 2.1.2 2.1.3 2.2 All 73 releases
bbpress / templates / default / js / reply.js

reply.js in bbPress trunk, at templates/default/js/reply.js

236 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 var addReply = {
2
3 /**
4 * Move the reply form when "Reply" is clicked.
5 *
6 * @since 2.6.2
7 * @param {string} replyId
8 * @param {string} parentId
9 * @param {string} respondId
10 * @param {string} postId
11 * @returns {undefined|Boolean}
12 */
13 moveForm: function ( replyId, parentId, respondId, postId ) {
14
15 /* Get initial elements */
16 var t = this,
17 reply = t.getElement( replyId ),
18 respond = t.getElement( respondId ),
19 cancel = t.getElement( 'bbp-cancel-reply-to-link' ),
20 parent = t.getElement( 'bbp_reply_to' ),
21 post = t.getElement( 'bbp_topic_id' );
22
23 /* Remove the editor, if its already been moved */
24 t.removeEditor();
25
26 /* Bail to avoid errors */
27 if ( ! reply || ! respond || ! cancel || ! parent ) {
28 return;
29 }
30
31 t.respondId = respondId;
32 postId = postId || false;
33
34 /* Setup a temporary div for relocating back when clicking cancel */
35 if ( ! t.getElement( 'bbp-temp-form-div' ) ) {
36 var div = document.createElement( 'div' );
37
38 div.id = 'bbp-temp-form-div';
39 div.style.display = 'none';
40
41 respond.parentNode.appendChild( div );
42 }
43
44 /* Relocate the element */
45 reply.parentNode.appendChild( respond );
46
47 if ( post && postId ) {
48 post.value = postId;
49 }
50
51 parent.value = parentId;
52 cancel.style.display = '';
53
54 /* Add the editor where it now belongs */
55 t.addEditor();
56
57 /**
58 * When canceling a Reply.
59 *
60 * @since 2.6.2
61 * @returns {void}
62 */
63 cancel.onclick = function () {
64 t.cancelForm( this );
65 };
66
67 t.scrollToForm();
68
69 /* Prevent click from going through */
70 return false;
71 },
72
73 /**
74 * Cancel the reply form.
75 *
76 * @since 2.6.6
77 * @returns {void}
78 */
79 cancelForm: function () {
80 var r = addReply,
81 temp = r.getElement( 'bbp-temp-form-div' ),
82 cancel = r.getElement( 'bbp-cancel-reply-to-link' ),
83 respond = r.getElement( r.respondId );
84
85 r.removeEditor();
86
87 /* Allow click to go through */
88 if ( ! temp || ! respond ) {
89 return;
90 }
91
92 r.getElement( 'bbp_reply_to' ).value = '0';
93
94 temp.parentNode.insertBefore( respond, temp );
95 temp.parentNode.removeChild( temp );
96
97 cancel.style.display = 'none';
98 cancel.onclick = null;
99
100 r.addEditor();
101 r.scrollToForm();
102
103 /* Prevent click from going through */
104 return false;
105 },
106
107 /**
108 * Scrolls to the top of the page.
109 *
110 * @since 2.6.2
111 * @return {void}
112 */
113 scrollToForm: function() {
114
115 /* Get initial variables to start computing boundaries */
116 var t = this,
117 form = t.getElement( 'new-post' ),
118 elemRect = form.getBoundingClientRect(),
119 position = (window.pageYOffset || document.scrollTop) - (document.clientTop || 0),
120 destination = ( position + elemRect.top ),
121 negative = ( destination < position ),
122 adminbar = t.getElement( 'wpadminbar'),
123 offset = 0,
124 distance = 0;
125
126 /* Offset by the adminbar */
127 if ( adminbar && ( typeof ( adminbar ) !== 'undefined' ) ) {
128 offset = adminbar.scrollHeight;
129 }
130
131 /* Compute the difference, depending on direction */
132 distance = true === negative ? ( position - destination ) : ( destination - position );
133
134 /* Do some math to compute the animation steps */
135 var vast = ( distance > 800 ),
136 speed_step = vast ? 30 : 20,
137 speed = Math.min( 12, Math.round( distance / speed_step ) ),
138 step = Math.round( distance / speed_step ),
139 steps = [],
140 timer = 0;
141
142 function scrollStep() {
143 window.scrollTo( 0, steps.shift() );
144 }
145
146 /* Scroll up */
147 if ( true === negative ) {
148 while ( position > destination ) {
149 position -= step;
150
151 if ( position < destination ) {
152 position = destination;
153 }
154
155 steps.push( position - offset );
156
157 setTimeout( scrollStep, timer * speed );
158
159 timer++;
160 }
161
162 /* Scroll down */
163 } else {
164 while ( position < destination ) {
165 position += step;
166
167 if ( position > destination ) {
168 position = destination;
169 }
170
171 steps.push( position - offset );
172
173 setTimeout( scrollStep, timer * speed );
174
175 timer++;
176 }
177 }
178 },
179
180 /**
181 * Get an element by ID
182 *
183 * @since 2.6.2
184 * @param {string} e
185 * @returns {HTMLElement} Element
186 */
187 getElement: function (e) {
188 return document.getElementById(e);
189 },
190
191 /**
192 * Remove the Editor
193 *
194 * @since 2.6.2
195 * @returns {void}
196 */
197 removeEditor: function () {
198
199 /* Bail to avoid error */
200 if ( typeof ( tinyMCE ) === 'undefined' ) {
201 return;
202 }
203
204 var tmce = tinyMCE.get( 'bbp_reply_content' );
205
206 if ( tmce && ! tmce.isHidden() ) {
207 this.mode = 'tmce';
208 tmce.remove();
209
210 } else {
211 this.mode = 'html';
212 }
213 },
214
215 /**
216 * Add the Editor
217 *
218 * @since 2.6.2
219 * @returns {void}
220 */
221 addEditor: function () {
222
223 /* Bail to avoid error */
224 if ( typeof ( tinyMCE ) === 'undefined' ) {
225 return;
226 }
227
228 if ( 'tmce' === this.mode ) {
229 switchEditors.go( 'bbp_reply_content', 'tmce' );
230
231 } else if ( 'html' === this.mode ) {
232 switchEditors.go( 'bbp_reply_content', 'html' );
233 }
234 }
235 };
236