6f73e4a0b5243c8357aab94ff94ad8912d00e38c
[nit.git] / share / nitdoc / js / plugins / github / commentbox.js
1 /* This file is part of NIT ( http://www.nitlanguage.org ).
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 */
15
16 /*
17 * CommentBox allows user to edit comments then preview, commit or cancel the changes
18 */
19 define([
20 "jquery",
21 "jQueryUI"
22 ], function($) {
23 var Location = function(location) {
24 var parts = location.split(":");
25 this.origin = location;
26 this.path = parts[0];
27 this.lstart = parseInt(parts[1].split("--")[0].split(",")[0]);
28 this.tabpos = parseInt(parts[1].split("--")[0].split(",")[1]);
29 this.lend = parseInt(parts[1].split("--")[1].split(",")[0]);
30 this.toString = function() {
31 return this.path + ":" + this.lstart + "," + this.tabpos + "--" + this.lend + ",0";
32 }
33 }
34
35 $.widget("nitdoc.commentbox", {
36
37 options: {
38 previewTxt: "preview",
39 commitTxt: "Commit",
40 cancelTxt: "Cancel",
41 commentboxTitle: "Edit comment",
42 messageTxt: "Commit message"
43 },
44
45 _create: function() {
46 this._id = $(".nitdoc-github-commentbox").length
47 this._oldComment = this.element.val();
48 this._namespace = this.element.data("comment-namespace");
49 this._location = new Location(this.element.data("comment-location"));
50 this.commentBox = $("<div/>")
51 .hide()
52 .addClass("nitdoc-github-commentbox")
53 .append(
54 $("<h3/>")
55 .text(this.options.commentboxTitle)
56 )
57 .append(
58 $("<dl/>")
59 .addClass("nitdoc-github-commentbox-fields")
60 .append(
61 $("<dd/>")
62 .append(
63 $("<textarea/>")
64 .attr("id", "nitdoc-github-commentbox-comment" + this._id)
65 .addClass("nitdoc-github-commentarea")
66 .keyup($.proxy(this._doKeyUp, this))
67 .keydown($.proxy(this._doKeyDown, this))
68 )
69 )
70 .append(
71 $("<dt/>")
72 .append(
73 $("<label/>")
74 .attr("for", "nitdoc-github-commentbox-message" + this._id)
75 .text(this.options.messageTxt + ":")
76 )
77 )
78 .append(
79 $("<dd/>")
80 .append(
81 $("<textarea/>")
82 .attr("id", "nitdoc-github-commentbox-message" + this._id)
83 .keyup($.proxy(this._doKeyUp, this))
84 .keydown($.proxy(this._doKeyDown, this))
85 )
86 )
87 .append(
88 $("<dt/>")
89 .append(
90 $("<input/>")
91 .attr({
92 id: "nitdoc-github-commentbox-signedoff" + this._id,
93 type: "checkbox"
94 })
95 .change($.proxy(this._doSignedChange, this))
96 )
97 .append(
98 $("<label/>")
99 .attr({
100 "id": "nitdoc-github-commentbox-signedoff-label" + this._id,
101 "for": "nitdoc-github-commentbox-signedoff" + this._id
102 })
103 )
104 )
105 )
106 this._buildButtonBar();
107 this.element.after(this.commentBox);
108 },
109
110 _buildButtonBar: function() {
111 this.commentBox.append(
112 $("<div/>")
113 .addClass("nitdoc-github-commentbox-buttons")
114 .append(
115 $("<a/>")
116 .addClass("nitdoc-github-preview")
117 .html(this.options.previewTxt)
118 .click($.proxy(this._doPreviewClick, this))
119 )
120 .append(
121 $("<button/>")
122 .addClass("nitdoc-github-button")
123 .addClass("nitdoc-github-commit")
124 .attr("disabled", "disabled")
125 .html(this.options.commitTxt)
126 .click($.proxy(this._doCommitClick, this))
127 )
128 .append(
129 $("<button/>")
130 .addClass("nitdoc-github-button")
131 .addClass("nitdoc-github-cancel")
132 .html(this.options.cancelTxt)
133 .click($.proxy(this._doCancelClick, this))
134 )
135 );
136 },
137
138 /* public actions */
139
140 open: function(user, requestID) {
141 this._requestID = requestID;
142 var value = this.element.val();
143 var isNew = !value;
144 var message = "doc: " + (isNew ? "added" : "modified") + " comment for " + this._namespace;
145 this._setMessage(message);
146 this._setSignedOff("Signed-off-by: " + user.signedOff);
147 this._setComment(value);
148 this.commentBox.show();
149 this.commentBox.find("textarea").width(this.commentBox.innerWidth() - 45)
150 $("#nitdoc-github-commentbox-comment" + this._id).focus();
151 $("#nitdoc-github-commentbox-comment" + this._id).trigger("keyup");
152 $("#nitdoc-github-commentbox-message" + this._id).trigger("keyup");
153 this._trigger("_open", null, {commentBox: this});
154 },
155
156 close: function() {
157 this.commentBox.hide();
158 this._trigger("_close", null, {commentBox: this});
159 },
160
161 /* internals */
162
163 _setComment: function(value) {
164 $("#nitdoc-github-commentbox-comment" + this._id).val(value);
165 },
166
167 _getComment: function() {
168 return $("#nitdoc-github-commentbox-comment" + this._id).val();
169 },
170
171 _getMessage: function() {
172 return $("#nitdoc-github-commentbox-message" + this._id).val();
173 },
174
175 _setMessage: function(message) {
176 $("#nitdoc-github-commentbox-message" + this._id).val(message);
177 },
178
179 _getSignedOff: function() {
180 return $("#nitdoc-github-commentbox-signedoff" + this._id).val();
181 },
182
183 _setSignedOff: function(signedoff) {
184 $("#nitdoc-github-commentbox-signedoff" + this._id).val(signedoff);
185 $("#nitdoc-github-commentbox-signedoff-label" + this._id).text(signedoff);
186 },
187
188 /* events */
189
190 _doKeyUp: function(event) {
191 $(event.target).height($(event.target).val().split(/\r|\n/).length * 16);
192 },
193
194 _doKeyDown: function(event) {
195 if(event.keyCode == 13){
196 $(event.target).css("height", ($(event.target).outerHeight() + 6) + "px");
197 }
198 },
199
200 _doSignedChange: function(event) {
201 if ($(event.currentTarget).is(':checked')) {
202 this.commentBox.find("button.nitdoc-github-commit").removeAttr("disabled");
203 } else {
204 this.commentBox.find("button.nitdoc-github-commit").attr("disabled", "disabled");
205 }
206 },
207
208 _doPreviewClick: function(event) {
209 this._trigger("_preview", event, {value: this._getComment()});
210 },
211
212 _doCommitClick: function() {
213 this._trigger("_commit", event, {
214 requestID: this._requestID,
215 location: this._location,
216 namespace: this._namespace,
217 oldComment: this._oldComment,
218 newComment: this._getComment(),
219 title: this._getMessage(),
220 message: this._getMessage() + "\n\n" + this._getSignedOff()
221 });
222 },
223
224 _doCancelClick: function() {
225 this.close();
226 }
227 });
228 });