You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

code_review.js 10 kB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. # Code Review plugin for Redmine
  3. # Copyright (C) 2009-2013 Haruyuki Iida
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. var topZindex = 1000;
  20. var action_type = '';
  21. var rev = '';
  22. var rev_to = '';
  23. var path = '';
  24. var urlprefix = '';
  25. var review_form_dialog = null;
  26. var add_form_title = null;
  27. var review_dialog_title = null;
  28. var repository_id = null;
  29. var filenames = [];
  30. var ReviewCount = function(total, open, progress){
  31. this.total = total;
  32. this.open = open;
  33. this.closed = total - open;
  34. this.progress = progress
  35. };
  36. var CodeReview = function(id) {
  37. this.id = id;
  38. this.path = '';
  39. this.line = 0;
  40. this.url = '';
  41. this.is_closed = false;
  42. };
  43. var review_counts = new Array();
  44. var code_reviews_map = new Array();
  45. var code_reviews_dialog_map = new Array();
  46. function UpdateRepositoryView(title) {
  47. var header = $("table.changesets thead tr:first");
  48. var th = $('<th></th>');
  49. th.html(title);
  50. header.append(th);
  51. $('tr.changeset td.id a').each(function(i){
  52. var revision = this.getAttribute("href");
  53. revision = revision.substr(revision.lastIndexOf("/") + 1);
  54. var review = review_counts['revision_' + revision];
  55. var td = $('<td/>',{
  56. 'class':'progress'
  57. });
  58. td.html(review.progress);
  59. $(this.parentNode.parentNode).append(td);
  60. });
  61. }
  62. //add function $.down
  63. if(! $.fn.down)
  64. (function($) {
  65. $.fn.down = function() {
  66. var el = this[0] && this[0].firstChild;
  67. while (el && el.nodeType != 1)
  68. el = el.nextSibling;
  69. return $(el);
  70. };
  71. })(jQuery);
  72. function UpdateRevisionView() {
  73. $('li.change').each(function(){
  74. var li = $(this);
  75. if (li.hasClass('folder')) return;
  76. var a = li.down('a');
  77. if (a.size() == 0) return;
  78. var path = a.attr('href').replace(urlprefix, '').replace(/\?.*$/, '');
  79. var reviewlist = code_reviews_map[path];
  80. if (reviewlist == null) return;
  81. var ul = $('<ul></ul>');
  82. for (var j = 0; j < reviewlist.length; j++) {
  83. var review = reviewlist[j];
  84. var icon = review.is_closed? 'icon-closed-review': 'icon-review';
  85. var item = $('<li></li>', {
  86. 'class': 'icon ' + icon + ' code_review_summary'
  87. });
  88. item.html(review.url);
  89. ul.append(item);
  90. }
  91. li.append(ul);
  92. });
  93. }
  94. function setAddReviewButton(url, change_id, image_tag, is_readonly, is_diff, attachment_id){
  95. var filetables = [];
  96. var j = 0;
  97. $('table').each(function(){
  98. if($(this).hasClass('filecontent')){
  99. filetables[j++] = this;
  100. }
  101. });
  102. j = 0;
  103. $('table.filecontent th.filename').each(function(){
  104. filenames[j] = $.trim($(this).text());
  105. j++;
  106. });
  107. addReviewUrl = url + '?change_id=' + change_id + '&action_type=' + action_type +
  108. '&rev=' + rev + '&rev_to=' + rev_to +
  109. '&attachment_id=' + attachment_id + '&repository_id=' + encodeURIComponent(repository_id);
  110. if (path != null && path.length > 0) {
  111. addReviewUrl = addReviewUrl + '&path=' + encodeURIComponent(path);
  112. }
  113. var num = 0;
  114. if (is_diff) {
  115. num = 1;
  116. }
  117. var i, l, tl;
  118. for (i = 0, tl = filetables.length; i < tl; i++) {
  119. var table = filetables[i];
  120. var trs = table.getElementsByTagName('tr');
  121. for (j = 0,l = trs.length; j < l; j++) {
  122. var tr = trs[j];
  123. var ths = tr.getElementsByTagName('th');
  124. var th = ths[num];
  125. if (th == null) {
  126. continue;
  127. }
  128. var th_html = th.innerHTML;
  129. var line = th_html.match(/[0-9]+/);
  130. if (line == null) {
  131. continue;
  132. }
  133. var span_html = '<span white-space="nowrap" id="review_span_' + line + '_' + i + '">';
  134. if (!is_readonly) {
  135. span_html += image_tag;
  136. }
  137. span_html += '</span>';
  138. th.innerHTML = th_html + span_html;
  139. var img = th.getElementsByTagName('img')[0];
  140. if (img != null ) {
  141. img.id = 'add_revew_img_' + line + '_' + i;
  142. $(img).click(clickPencil);
  143. }
  144. }
  145. }
  146. }
  147. function clickPencil(e)
  148. {
  149. // alert('$(e.target).attr("id") = ' + $(e.target).attr("id"));
  150. var result = $(e.target).attr("id").match(/([0-9]+)_([0-9]+)/);
  151. var line = result[1];
  152. var file_count = eval(result[2]);
  153. var url = addReviewUrl + '&line=' + line + '&file_count=' + file_count;
  154. if (path == null || path.length == 0) {
  155. url = url + '&path=' + encodeURIComponent(filenames[file_count]) + '&diff_all=true';
  156. }
  157. addReview(url);
  158. formPopup(e.pageX, e.pageY);
  159. e.preventDefault();
  160. }
  161. var addReviewUrl = null;
  162. var showReviewUrl = null;
  163. var showReviewImageTag = null;
  164. var showClosedReviewImageTag = null;
  165. function setShowReviewButton(line, review_id, is_closed, file_count) {
  166. //alert('file_count = ' + file_count);
  167. var span = $('#review_span_' + line + '_' + file_count);
  168. if (span.size() == 0) {
  169. return;
  170. }
  171. var innerSpan = $('<span></span>',{id: 'review_' + review_id});
  172. span.append(innerSpan);
  173. innerSpan.html(is_closed? showClosedReviewImageTag : showReviewImageTag);
  174. var div = $('<div></div>', {
  175. 'class':'draggable',
  176. id: 'show_review_' + review_id
  177. });
  178. $('#code_review').append(div);
  179. innerSpan.down('img').click(function(e) {
  180. var review_id = $(e.target).parent().attr('id').match(/[0-9]+/)[0];
  181. var span = $('#review_' + review_id); // span element of view review button
  182. var pos = span.offset();
  183. showReview(showReviewUrl, review_id, pos.left + 10 + 5, pos.top + 25);
  184. });
  185. }
  186. function popupReview(review_id) {
  187. var span = $('#review_' + review_id); // span element of view review button
  188. var pos = span.offset();
  189. $('html,body').animate({ scrollTop: pos.top },
  190. {duration: 'fast',
  191. complete: function(){showReview(showReviewUrl, review_id, pos.left + 10 + 5, pos.top)}});
  192. // position and show popup dialog
  193. // create popup dialog
  194. //var win = showReview(showReviewUrl, review_id, pos.left + 10 + 5, pos.top);
  195. // win.toFront();
  196. }
  197. function showReview(url, review_id, x, y) {
  198. if (code_reviews_dialog_map[review_id] != null) {
  199. var cur_win = code_reviews_dialog_map[review_id];
  200. cur_win.hide();
  201. code_reviews_dialog_map[review_id] = null;
  202. }
  203. $('#show_review_' + review_id).load(url, {review_id: review_id});
  204. var review = getReviewObjById(review_id);
  205. var win = $('#show_review_' + review_id).dialog({
  206. show: {effect:'scale'},// ? 'top-left'
  207. //position: [x, y + 5],
  208. width:640,
  209. zIndex: topZindex,
  210. title: review_dialog_title
  211. });
  212. // win.getContent().style.color = "#484848";
  213. // win.getContent().style.background = "#ffffff";
  214. topZindex++;
  215. code_reviews_dialog_map[review_id] = win;
  216. return win
  217. }
  218. function getReviewObjById(review_id) {
  219. for (var reviewlist in code_reviews_map) {
  220. for (var i = 0; i < reviewlist.length; i++) {
  221. var review = reviewlist[i];
  222. if (review.id == review_id) {
  223. return review;
  224. }
  225. }
  226. }
  227. return null;
  228. }
  229. function formPopup(x, y){
  230. //@see http://docs.jquery.com/UI/Effects/Scale
  231. var win = $('#review-form-frame').dialog({
  232. show: {effect:'scale', direction: 'both'},// ? 'top-left'
  233. // position: [x, y + 5],
  234. width:640,
  235. zIndex: topZindex,
  236. title: add_form_title
  237. });
  238. // win.getContent().style.background = "#ffffff";
  239. if (review_form_dialog != null) {
  240. review_form_dialog.destroy();
  241. review_form_dialog = null;
  242. }
  243. review_form_dialog = win;
  244. topZindex += 10;
  245. return false;
  246. }
  247. function hideForm() {
  248. if (review_form_dialog == null) {
  249. return;
  250. }
  251. review_form_dialog.dialog('close');
  252. review_form_dialog = null;
  253. $('#review-form').html('');
  254. }
  255. function addReview(url) {
  256. $('#review-form').load(url);
  257. }
  258. function deleteReview(review_id) {
  259. $('show_review_' + review_id).remove();
  260. $('review_' + review_id).remove();
  261. }
  262. function changeImage(review_id, is_closed) {
  263. var span = $('review_' + review_id);
  264. var new_image = null;
  265. var dummy = new Element('span');
  266. if (is_closed) {
  267. dummy.insert(showClosedReviewImageTag);
  268. }
  269. else {
  270. dummy.insert(showReviewImageTag);
  271. }
  272. new_image = dummy.down().getAttribute('src');
  273. //alert(new_image);
  274. span.down('img').setAttribute('src', new_image);
  275. }
  276. function make_addreview_link(project, link) {
  277. var alist = $('#content p a');
  278. if (alist == null) {
  279. return;
  280. }
  281. var a = alist[0];
  282. var p = a.parentNode;
  283. p.innerHTML = p.innerHTML + " | " + link;
  284. }
  285. function call_update_revisions(url) {
  286. var changeset_ids = '';
  287. var links = $$('table.changesets tbody tr.changeset td.id a');
  288. for (var i = 0; i < links.length; i++) {
  289. var link = links[i];
  290. var href = link.getAttribute('href');
  291. var id = href.replace(/^.*\/revisions\//, '');
  292. if (i > 0) {
  293. changeset_ids += ',';
  294. }
  295. changeset_ids += id;
  296. }
  297. new Ajax.Updater('code_review_revisions', url,
  298. {
  299. evalScripts:true,
  300. method:'get',
  301. parameters: 'changeset_ids=' + encodeURI(changeset_ids)
  302. });
  303. }
  304. $.fn.serialize2json = function()
  305. {
  306. var o = {};
  307. var a = this.serializeArray();
  308. $.each(a, function() {
  309. if (o[this.name]) {
  310. if (!o[this.name].push) {
  311. o[this.name] = [o[this.name]];
  312. }
  313. o[this.name].push(this.value || '');
  314. } else {
  315. o[this.name] = this.value || '';
  316. }
  317. });
  318. return o;
  319. };

软件技术创新是当今国家整体创新能力的重要基础和发展引擎。近年来,大众参与的软件创新与创业活动已经成为网络时代软件开发和应用的新形态,正在快速改变着全球软件创新模式和软件产业格局。 ...