/
newComment.js
200 lines (165 loc) · 6.33 KB
/
newComment.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
var $ = require('ep_etherpad-lite/static/js/rjquery').$;
var commentL10n = require('ep_comments_page/static/js/commentL10n');
// Easier access to outer pad
var padOuter;
var getPadOuter = function() {
padOuter = padOuter || $('iframe[name="ace_outer"]').contents();
return padOuter;
}
// Easier access to new comment container
var newCommentContainer;
var getNewCommentContainer = function() {
newCommentContainer = newCommentContainer || getPadOuter().find('#newComments');
return newCommentContainer;
}
// Insert a comment node
var createNewCommentForm = function(comment) {
var container = getNewCommentContainer();
comment.commentId = "";
var content = $('#newCommentTemplate').tmpl(comment);
content.prependTo(container);
return content;
};
// Create a comment object with data filled on the given form
var buildCommentFrom = function(form) {
var text = form.find('.comment-content').val();
var changeFrom = form.find('.comment-suggest-from').val();
var changeTo = form.find('.comment-suggest-to').val() || null;
var comment = {};
comment.text = text;
if(changeTo){
comment.changeFrom = changeFrom;
comment.changeTo = changeTo;
}
return comment;
}
// Callback for new comment Cancel
var cancelNewComment = function(){
hideNewCommentForm();
}
// Callback for new comment Submit
var submitNewComment = function(form, callback) {
var index = 0;
var text = form.find('.comment-content').val();
var commentTextIsNotEmpty = text.length !== 0;
var comment = buildCommentFrom(form);
if (commentTextIsNotEmpty) {
hideNewCommentForm();
callback(comment, index);
}
return false;
}
var fixFlyingToobarOnIOS = function() {
if (browser.ios) {
var shouldPlaceMenuRightOnBottom = $(".toolbar ul.menu_right").css('bottom') !== "auto";
getNewCommentContainer().find('input, textarea')
.on("focus", function() {
fixToolbarPosition();
if (shouldPlaceMenuRightOnBottom) placeMenuRightOnBottom();
})
.on("blur", function() {
revertFixToToolbarPosition();
if (shouldPlaceMenuRightOnBottom) revertPlacingMenuRightOnBottom();
});
// When user changes orientation, we need to re-position menu_right
if (shouldPlaceMenuRightOnBottom) {
waitForResizeToFinishThenCall(500, function() {
var needToUpdateTop = $(".toolbar ul.menu_right").css("top") !== "";
if (needToUpdateTop) placeMenuRightOnBottom();
});
}
}
}
var fixToolbarPosition = function() {
$(".toolbar ul.menu_left, .toolbar ul.menu_right").css("position", "absolute");
}
var revertFixToToolbarPosition = function() {
$(".toolbar ul.menu_left, .toolbar ul.menu_right").css("position", "");
}
var placeMenuRightOnBottom = function() {
$(".toolbar ul.menu_right").css("top", $(document).outerHeight());
}
var revertPlacingMenuRightOnBottom = function() {
$(".toolbar ul.menu_right").css("top", "");
}
/* ***** Public methods: ***** */
var localizeNewCommentForm = function() {
var newCommentForm = getNewCommentContainer().find('#newComment');
if (newCommentForm.length !== 0) commentL10n.localize(newCommentForm);
};
// Create container to hold new comment form
var insertContainers = function(target) {
target.prepend('<div id="newComments"></div>');
// Listen for include suggested change toggle
getNewCommentContainer().on("change", '#suggestion-checkbox', function() {
if($(this).is(':checked')) {
getPadOuter().find('.suggestion').show();
} else {
getPadOuter().find('.suggestion').hide();
}
});
}
// Insert new Comment Form
var insertNewCommentFormIfDontExist = function(comment, callback) {
var newCommentForm = getNewCommentContainer().find('#newComment');
var formDoesNotExist = newCommentForm.length === 0;
if (formDoesNotExist) {
newCommentForm = createNewCommentForm(comment);
localizeNewCommentForm();
// Listen to cancel
newCommentForm.find('#comment-reset').on('click', function() {
cancelNewComment();
});
// Hack to avoid "flying" toolbars on iOS
fixFlyingToobarOnIOS();
} else {
// Reset form to make sure it is all clear
newCommentForm.get(0).reset();
// Detach current "submit" handler to be able to call the updated callback
newCommentForm.off("submit");
}
// Listen to comment confirmation (needs to be outside of if/else to be able to update the callback)
newCommentForm.submit(function() {
var form = $(this);
return submitNewComment(form, callback);
});
return newCommentForm;
};
var showNewCommentForm = function() {
getNewCommentContainer().addClass("active");
// we need to set a timeout otherwise the animation to show #newComment won't be visible
window.setTimeout(function() {
getPadOuter().find('.suggestion').hide(); // Hides suggestion in case of a cancel
getNewCommentContainer().find('#newComment').removeClass("hidden").addClass("visible");
}, 0);
// mark selected text, so it is clear to user which text range the comment is being applied to
pad.plugins.ep_comments_page.preCommentMarker.markSelectedText();
}
var hideNewCommentForm = function() {
getNewCommentContainer().find('#newComment').removeClass("visible").addClass("hidden");
// force focus to be lost, so virtual keyboard is hidden on mobile devices
getNewCommentContainer().find(':focus').blur();
// we need to give some time for the animation of #newComment to finish
window.setTimeout(function() {
getNewCommentContainer().removeClass("active");
}, 500);
// unmark selected text, as now there is no text being commented
pad.plugins.ep_comments_page.preCommentMarker.unmarkSelectedText();
}
// Some browsers trigger resize several times while resizing the window, so
// we need to make sure resize is done to avoid calling the callback multiple
// times.
// Based on: https://css-tricks.com/snippets/jquery/done-resizing-event/
var waitForResizeToFinishThenCall = function(timeout, callback){
var resizeTimer;
$(window).on("resize", function() {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(callback, timeout);
});
}
exports.localizeNewCommentForm = localizeNewCommentForm;
exports.insertNewCommentFormIfDontExist = insertNewCommentFormIfDontExist;
exports.showNewCommentForm = showNewCommentForm;
exports.hideNewCommentForm = hideNewCommentForm;
exports.insertContainers = insertContainers;
exports.waitForResizeToFinishThenCall = waitForResizeToFinishThenCall;