PluginProbe
bbPress / 2.6.17
bbPress v2.6.17
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 2.2.1 All 72 releases
bbpress / templates / default / js / reply.js

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

240 lines 5.0 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 /* Allow click to go through */
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 cancelling 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 ), // jshint ignore:line
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 /* jshint ignore:start */
133 distance = ( true === negative )
134 ? ( position - destination )
135 : ( destination - position );
136
137 /* Do some math to compute the animation steps */
138 var vast = ( distance > 800 ),
139 speed_step = vast ? 30 : 20,
140 speed = Math.min( 12, Math.round( distance / speed_step ) ),
141 step = Math.round( distance / speed_step ),
142 steps = [],
143 timer = 0;
144
145 /* Scroll up */
146 if ( true === negative ) {
147 while ( position > destination ) {
148 position -= step;
149
150 if ( position < destination ) {
151 position = destination;
152 }
153
154 steps.push( position - offset );
155
156 setTimeout( function() {
157 window.scrollTo( 0, steps.shift() );
158 }, timer * speed );
159
160 timer++;
161 }
162
163 /* Scroll down */
164 } else {
165 while ( position < destination ) {
166 position += step;
167
168 if ( position > destination ) {
169 position = destination;
170 }
171
172 steps.push( position - offset );
173
174 setTimeout( function() {
175 window.scrollTo( 0, steps.shift() );
176 }, timer * speed );
177
178 timer++;
179 }
180 }
181 /* jshint ignore:end */
182 },
183
184 /**
185 * Get an element by ID
186 *
187 * @since 2.6.2
188 * @param {string} e
189 * @returns {HTMLElement} Element
190 */
191 getElement: function (e) {
192 return document.getElementById(e);
193 },
194
195 /**
196 * Remove the Editor
197 *
198 * @since 2.6.2
199 * @returns {void}
200 */
201 removeEditor: function () {
202
203 /* Bail to avoid error */
204 if ( typeof ( tinyMCE ) === 'undefined' ) {
205 return;
206 }
207
208 var tmce = tinyMCE.get( 'bbp_reply_content' );
209
210 if ( tmce && ! tmce.isHidden() ) {
211 this.mode = 'tmce';
212 tmce.remove();
213
214 } else {
215 this.mode = 'html';
216 }
217 },
218
219 /**
220 * Add the Editor
221 *
222 * @since 2.6.2
223 * @returns {void}
224 */
225 addEditor: function () {
226
227 /* Bail to avoid error */
228 if ( typeof ( tinyMCE ) === 'undefined' ) {
229 return;
230 }
231
232 if ( 'tmce' === this.mode ) {
233 switchEditors.go( 'bbp_reply_content', 'tmce' );
234
235 } else if ( 'html' === this.mode ) {
236 switchEditors.go( 'bbp_reply_content', 'html' );
237 }
238 }
239 };
240