add labels page
|
1 |
<% |
2 |
# API |
|
3 |
my $api = gitprep_api; |
|
4 |
|
|
5 |
# Parameters |
|
6 |
my $user_id = param('user'); |
|
7 |
my $project_id = param('project'); |
|
8 |
|
|
add label remove feature
|
9 |
# Can write access |
10 |
my $session_user_id = $api->session_user_id; |
|
11 |
my $can_write_access = $api->can_write_access($session_user_id, $user_id, $project_id); |
|
12 |
|
|
add create label
|
13 |
my $errors; |
14 |
if (lc $self->req->method eq 'post') { |
|
add label remove feature
|
15 |
unless ($can_write_access) { |
16 |
Carp::croak("Don't have write access"); |
|
17 |
} |
|
18 | ||
add create label
|
19 |
my $op = param('op') // ''; |
20 | ||
21 |
my $project_row_id = app->dbi->model('project')->select( |
|
22 |
'project.row_id', |
|
23 |
where => {'user.id' => $user_id, 'project.id' => $project_id} |
|
24 |
)->value; |
|
25 |
|
|
26 |
if ($op eq 'create') { |
|
27 |
my $id = param('label-id'); |
|
28 |
my $color = param('color'); |
|
29 |
|
|
30 |
my $vc = app->vc; |
|
31 |
my $validation = $vc->validation; |
|
32 |
|
|
33 |
if (!length $id) { |
|
34 |
$validation->add_failed(id => "Name can't be blank"); |
|
35 |
} |
|
36 |
elsif (length $id > 100) { |
|
37 |
$validation->add_failed(id => "Name is too long"); |
|
38 |
} |
|
39 |
else { |
|
40 |
my $label = app->dbi->model('label')->select( |
|
41 |
where => { |
|
42 |
project => $project_row_id, |
|
43 |
id => $id |
|
44 |
} |
|
45 |
)->one; |
|
46 |
|
|
47 |
if ($label) { |
|
48 |
$validation->add_failed(id => "Name has already been taken"); |
|
49 |
} |
|
50 |
} |
|
51 |
|
|
52 |
if (!length $color) { |
|
53 |
$validation->add_failed(color => "Color can't be blank"); |
|
54 |
} |
|
55 |
|
|
56 |
if ($validation->is_valid) { |
|
57 |
|
|
58 |
my $new_label = { |
|
59 |
id => $id, |
|
60 |
color => $color, |
|
61 |
project => $project_row_id |
|
62 |
}; |
|
63 |
|
|
64 |
app->dbi->model('label')->insert($new_label); |
|
65 |
|
|
66 |
$self->redirect_to; |
|
67 |
return; |
|
68 |
} |
|
69 |
else { |
|
70 |
$errors = $validation->messages; |
|
71 |
} |
|
72 |
} |
|
add label remove feature
|
73 |
elsif ($op eq 'api-delete') { |
add label update
|
74 |
my $row_id = param('row-id'); |
add label remove feature
|
75 |
|
76 |
app->dbi->model('label')->delete(where => {row_id => $row_id}); |
|
77 |
|
|
78 |
$self->render(json => {success => 1}); |
|
79 |
return; |
|
80 |
} |
|
add label update
|
81 |
elsif ($op eq 'api-update') { |
82 |
my $row_id = param('row-id'); |
|
83 |
my $id = param('id'); |
|
84 |
my $color = param('color'); |
|
85 |
|
|
86 |
my $vc = app->vc; |
|
87 |
my $validation = $vc->validation; |
|
88 |
|
|
89 |
if (!length $id) { |
|
90 |
$validation->add_failed(id => "Name can't be blank"); |
|
91 |
} |
|
92 |
elsif (length $id > 100) { |
|
93 |
$validation->add_failed(id => "Name is too long"); |
|
94 |
} |
|
95 |
else { |
|
96 |
my $label = app->dbi->model('label')->select( |
|
97 |
where => { |
|
98 |
project => $project_row_id, |
|
99 |
id => $id |
|
100 |
} |
|
101 |
)->one; |
|
102 |
|
|
103 |
if ($label && $id ne $label->{id}) { |
|
104 |
$validation->add_failed(id => "Name has already been taken"); |
|
105 |
} |
|
106 |
} |
|
107 |
|
|
108 |
if (!length $color) { |
|
109 |
$validation->add_failed(color => "Color can't be blank"); |
|
110 |
} |
|
111 |
|
|
112 |
if ($validation->is_valid) { |
|
113 |
app->dbi->model('label')->update({id => $id, color => $color}, where => {row_id => $row_id}); |
|
114 |
|
|
115 |
$self->render(json => {success => 1, id => $id, color => $color}); |
|
116 |
return; |
|
117 |
} |
|
118 |
else { |
|
119 |
$self->render(json => {success => 0, message => $validation->messages->[0]}); |
|
120 |
return; |
|
121 |
} |
|
122 |
} |
|
add create label
|
123 |
} |
124 |
|
|
show labels
|
125 |
my $labels = app->dbi->model('label')->select( |
126 |
{__MY__ => '*'}, |
|
127 |
where => {'project__user.id' => $user_id, 'project.id' => $project_id}, |
|
128 |
append => 'order by id' |
|
129 |
)->all; |
|
130 |
|
|
add color pallette
|
131 |
# Default color; |
132 |
my $default_color = "#" . sprintf('%02x', int rand 255) . sprintf('%02x', int rand 255) . sprintf('%02x', int rand 255); |
|
133 | ||
add labels page
|
134 |
layout 'common', title => "Labels - $user_id/$project_id"; |
135 |
%> |
|
136 | ||
cleanup new label
|
137 |
%= javascript begin |
138 |
$(document).ready(function () { |
|
139 |
$('.labels-new-btn').on('click', function () { |
|
140 |
$('.labels-create-panel').toggle(); |
|
141 |
}); |
|
implement edit area
|
142 | |
cleanup new label
|
143 |
$('.labels-create-cancel-btn').on('click', function () { |
144 |
$('.labels-create-panel').hide(); |
|
145 |
}); |
|
implement edit area
|
146 |
|
147 |
$('.labels-edit').on('click', function () { |
|
148 |
$(this).closest('li').find('.labels-display-area').hide(); |
|
149 |
$(this).closest('li').find('.labels-edit-area').show(); |
|
150 |
}); |
|
151 | ||
152 |
$('.labels-edit-cancel-btn').on('click', function () { |
|
153 |
$(this).closest('li').find('.labels-display-area').show(); |
|
154 |
$(this).closest('li').find('.labels-edit-area').hide(); |
|
155 |
}); |
|
156 |
|
|
add label remove feature
|
157 |
$('.labels-delete').on('click', function () { |
158 |
var li = $(this).closest('li'); |
|
add label update
|
159 |
var row_id = li.attr('row-id'); |
160 |
$.post('<%= url_for %>', {'row-id' : row_id, op : 'api-delete'}, function (result) { |
|
add label remove feature
|
161 |
if (result.success) { |
162 |
li.fadeOut(); |
|
163 |
} |
|
164 |
}); |
|
165 |
}); |
|
add label update
|
166 |
|
167 |
$('.labels-edit-save-btn').on('click', function () { |
|
168 |
var li = $(this).closest('li'); |
|
169 |
var row_id = li.attr('row-id'); |
|
170 |
var id = li.find('[name=label-id]').val(); |
|
171 |
var color = li.find('[name=color]').val(); |
|
172 |
|
|
173 |
$.post('<%= url_for %>', {'row-id' : row_id, op : 'api-update', id : id, color : color}, function (result) { |
|
174 |
|
|
175 |
if (result.success) { |
|
176 |
li.find('.labels-label-id').text(result.id); |
|
177 |
li.find('.labels-tag').css('background', result.color); |
|
178 | ||
179 |
li.find('.labels-display-area').show(); |
|
180 |
li.find('.labels-edit-area').hide(); |
|
181 |
} |
|
182 |
else { |
|
183 |
li.find('.labels-error').text(result.message); |
|
184 |
} |
|
185 |
}); |
|
186 |
}); |
|
cleanup new label
|
187 |
}); |
188 |
% end |
|
189 | ||
add labels page
|
190 |
%= include '/include/header'; |
191 | ||
192 |
<div class="container"> |
|
add create label
|
193 |
%= include '/include/errors', errors => $errors; |
add label remove feature
|
194 |
% if ($can_write_access) { |
195 |
<div class="labels-new-panel"> |
|
improve button design
|
196 |
<div class="labels-new-btn btn btn-green btn-new"> |
add label remove feature
|
197 |
New label |
198 |
</div> |
|
add labels delete edit new b...
|
199 |
</div> |
add label remove feature
|
200 |
% } |
201 |
|
|
fix label update padding
|
202 |
<form class="labels-create-panel" action="<%= url_for %>" method="post" style="display:none"> |
add create label
|
203 |
<%= hidden_field op => 'create' %> |
add create label form
|
204 |
<div class="labels-create-left"> |
205 |
<%= input_tag 'label-id', class => 'labels-create-label-id' %> |
|
add color pallette
|
206 |
<div class="labels-create-label-color-area"> |
207 |
<%= input_tag 'color' => $default_color, class => 'labels-create-label-color' %> |
|
208 |
<div class ="labels-create-label-color-palette" style="background:<%= $default_color %>;"></div> |
|
add create label
|
209 |
<div class="error"></div> |
add color pallette
|
210 |
</div> |
add create label form
|
211 |
</div> |
212 |
<div class="labels-create-right"> |
|
cleanup new label
|
213 |
<div class="labels-create-cancel-btn btn"> |
add create label form
|
214 |
Cancel |
215 |
</div> |
|
rename btn-success to btn-gr...
|
216 |
<input type="submit" value="Create label" class="labels-create-create-btn btn btn-green"> |
add create label form
|
217 |
</div> |
218 |
</form> |
|
show labels
|
219 |
<ul class="labels"> |
220 |
<li><%= @$labels %> labels</li> |
|
221 |
% for my $label (@$labels) { |
|
add label remove feature
|
222 |
<li row-id="<%= $label->{row_id} %>"> |
implement edit area
|
223 |
<div class="labels-display-area"> |
224 |
<div class="labels-left"> |
|
225 |
<div class="labels-tag" style="background:<%= $label->{color} %>;"> |
|
226 |
<i class="icon icon-tag"></i> |
|
add label update
|
227 |
<span class="labels-label-id"><%= $label->{id} %></span> |
implement edit area
|
228 |
</div> |
229 |
</div> |
|
230 |
<div class="labels-right"> |
|
add label remove feature
|
231 |
% if ($can_write_access) { |
232 |
<div class="labels-edit"> |
|
233 |
<a href="javascript:void(0)"><i class="icon icon-edit"></i> Edit</a> |
|
234 |
</div> |
|
235 |
<div class="labels-delete"> |
|
236 |
<a href="javascript:void(0)"><i class="icon icon-remove"></i> Delete</a> |
|
237 |
</div> |
|
238 |
% } |
|
add labels delete edit new b...
|
239 |
</div> |
240 |
</div> |
|
implement edit area
|
241 |
<div class="labels-edit-area" style="display:none"> |
242 |
<div class="labels-left"> |
|
243 |
<%= input_tag 'label-id' => $label->{id}, class => 'labels-edit-label-id' %> |
|
add color pallette
|
244 |
<div class="labels-edit-label-color-area"> |
245 |
<%= input_tag 'color' => $label->{color}, class => 'labels-edit-label-color' %> |
|
246 |
<div class ="labels-edit-label-color-palette" style="background:<%= $label->{color} %>;"></div> |
|
247 |
</div> |
|
add label update
|
248 |
<div class="labels-error" style="display:inline-block;color:red;"></div> |
add labels delete edit new b...
|
249 |
</div> |
implement edit area
|
250 |
<div class="labels-right"> |
251 |
<div class="labels-edit-cancel-btn btn"> |
|
252 |
Cancel |
|
253 |
</div> |
|
rename btn-success to btn-gr...
|
254 |
<div class="labels-edit-save-btn btn btn-green"> |
implement edit area
|
255 |
Save changes |
256 |
</div> |
|
add labels delete edit new b...
|
257 |
</div> |
show labels
|
258 |
</div> |
259 |
</li> |
|
260 |
% } |
|
261 |
</ul> |
|
add labels page
|
262 |
</div> |
263 | ||
264 |
%= include '/include/footer'; |