nitdoc: migrate github commitbox to jQuery.UI widget
[nit.git] / share / nitdoc / js / plugins / github / commitbox.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 * CommitBox allows user to add a message and sign its commit
18 */
19 define([
20 "jquery",
21 "jQueryUI"
22 ], function($) {
23 $.widget("nitdoc.commitbox", {
24
25 options: {
26 title: "Commit changes"
27 },
28
29 _create: function() {
30 this._fade = this._buildFade();
31 $("body").append(this._fade);
32 this._commitBox = $("<div/>")
33 .hide()
34 .attr("id", "nitdoc-github-commitBox")
35 .addClass("nitdoc-github-modal")
36 .append(
37 $("<a/>")
38 .addClass("nitdoc-github-close")
39 .attr("title", "Close")
40 .append("x")
41 .click($.proxy(this.close, this))
42 )
43 .append("<h3>" + this.options.title + "</h3>")
44 .append(
45 $("<div/>")
46 .append(
47 $("<label/>")
48 .attr("for", "nitdoc-github-commit-message")
49 )
50 .append($("<br/>"))
51 .append(
52 $("<textarea/>")
53 .attr("id", "nitdoc-github-commit-message")
54 )
55 .append($("<br/>"))
56 .append(
57 $("<input/>")
58 .attr({
59 id: "nitdoc-github-commit-signedoff",
60 type: "checkbox"
61 })
62 .change($.proxy(this._doSignedChange, this))
63 )
64 .append(
65 $("<label/>")
66 .attr({
67 "id": "nitdoc-github-commit-signedoff-label",
68 "for": "nitdoc-github-commit-signedoff"
69 })
70 )
71 ).append(
72 $("<div/>")
73 .addClass("nitdoc-github-buttons")
74 .append(
75 $("<button/>")
76 .attr({
77 id: "nitdoc-github-commit-button",
78 disabled: "disabled"
79 })
80 .addClass("nitdoc-github-button")
81 .append(
82 $(document.createElement("img"))
83 .attr("src", "resources/icons/github-icon.png")
84 )
85 .append("Commit")
86 .click($.proxy(this._doCommitClick, this))
87 )
88 );
89 $("body").append(this._commitBox);
90 },
91
92 open: function(namespace, user, isNew) {
93 var message = "doc: " + (isNew ? "added" : "modified") + " comment for " + namespace;
94 this._setMessage(message);
95 this._setSignedOff(user.signedOff);
96 this._fade.show();
97 this._commitBox.show();
98 this._commitBox.css({
99 top: "50%",
100 marginTop: -(this._commitBox.outerHeight() / 2) + "px",
101 left: "50%",
102 marginLeft: -(this._commitBox.outerWidth() / 2) + "px"
103 })
104 .find("#nitdoc-github-commit-message").focus();
105 },
106
107 close: function() {
108 this._commitBox.hide();
109 this._fade.hide();
110 },
111
112 /* internals */
113
114 _buildFade: function() {
115 return $("<div/>")
116 .hide()
117 .attr("id", "nitdoc-github-commitBox-fade")
118 .addClass("nitdoc-github-fade")
119 },
120
121 _getMessage: function() {
122 return $("#nitdoc-github-commit-message").val();
123 },
124
125 _setMessage: function(message) {
126 $("#nitdoc-github-commit-message").val(message);
127 },
128
129 _getSignedOff: function() {
130 return $("#nitdoc-github-commit-message").val();
131 },
132
133 _setSignedOff: function(signedoff) {
134 $("#nitdoc-github-commit-signedoff").val(signedoff);
135 $("#nitdoc-github-commit-signedoff-label").text(signedoff);
136 },
137
138 /* events */
139
140 _doSignedChange: function(event) {
141 if ($(event.currentTarget).is(':checked')) {
142 $("#nitdoc-github-commit-button").removeAttr("disabled");
143 } else {
144 $("#nitdoc-github-commit-button").attr("disabled", "disabled");
145 }
146 },
147
148 _doCommitClick: function(event) {
149 $(event.target).text("Commiting...");
150 $(event.target).attr("disabled", "disabled");
151 this._trigger("_commit", event, {
152 message: this._getMessage(),
153 signedoff: this._getSignedOff()
154 });
155 },
156
157 _doCancelClick: function(event) {
158 this._trigger("_cancel", event);
159 }
160 });
161 });