c5b8350ef4c6ce0dd501dfe057224b5ea9485eb6
[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 $.widget("nitdoc.commentbox", {
24
25 options: {
26 previewTxt: "preview",
27 commitTxt: "Commit...",
28 cancelTxt: "Cancel"
29 },
30
31 _create: function() {
32 this.commentBox = $("<div/>")
33 .addClass("nitdoc-github-commentbox")
34 .append(
35 $("<textarea/>")
36 .addClass("nitdoc-github-commentarea")
37 .keyup($.proxy(this._doKeyUp, this))
38 .keydown($.proxy(this._doKeyDown, this))
39 )
40 .append(
41 $("<a/>")
42 .addClass("nitdoc-github-preview")
43 .html(this.options.previewTxt)
44 .click($.proxy(this._doPreviewClick, this))
45 )
46 .append(
47 $("<button/>")
48 .addClass("nitdoc-github-button")
49 .addClass("nitdoc-github-commit")
50 .html(this.options.commitTxt)
51 .click($.proxy(this._doCommitClick, this))
52 )
53 .append(
54 $("<button/>")
55 .addClass("nitdoc-github-button")
56 .addClass("nitdoc-github-cancel")
57 .html(this.options.cancelTxt)
58 .click($.proxy(this._doCancelClick, this))
59 );
60
61 this.element.after(this.commentBox);
62 },
63
64 /* public actions */
65
66 open: function(value) {
67 this._originalValue = value;
68 this._setValue(value);
69 this._autosize();
70 this.commentBox.show();
71 var cbw = this.commentBox.innerWidth();
72 var taw = this._getArea().outerWidth();
73 this._getArea().width(cbw - (taw - cbw));
74 this.commentBox.find("textarea").trigger("keyup");
75 this.commentBox.find("textarea").focus();
76 this._trigger("_open", null, {commentBox: this});
77 },
78
79 close: function() {
80 this.commentBox.hide();
81 this._trigger("_close", null, {commentBox: this});
82 },
83
84 /* internals */
85
86 _autosize: function() {
87 this._getArea().height(this._getArea().val().split(/\r|\n/).length * 16);
88 },
89
90 _getArea: function() {
91 return this.commentBox.find("textarea.nitdoc-github-commentarea");
92 },
93
94 _setValue: function(value) {
95 this._getArea().val(value);
96 },
97
98 _getValue: function() {
99 return this._getArea().val();
100 },
101
102 /* events */
103
104 _doKeyUp: function() {
105 if(this._getValue() == this._originalValue) {
106 this.commentBox.find("button.nitdoc-github-commit").attr("disabled", "disabled");
107 } else {
108 this.commentBox.find("button.nitdoc-github-commit").removeAttr("disabled");
109 }
110 this._autosize();
111 },
112
113 _doKeyDown: function(event) {
114 if(event.keyCode == 13){
115 this._getArea().css("height", (this._getArea().outerHeight() + 6) + "px");
116 }
117 },
118
119 _doPreviewClick: function(event) {
120 this._trigger("_preview", event, {value: this._getValue()});
121 },
122
123 _doCommitClick: function() {
124 this._trigger("_commit", {value: this._getValue()});
125 },
126
127 _doCancelClick: function() {
128 this.close();
129 }
130 });
131 });