/src/applications/differential/render/DifferentialChangesetOneUpRenderer.php

https://github.com/dannysu/phabricator · PHP · 275 lines · 229 code · 42 blank · 4 comment · 31 complexity · 482c9da0a9690f1a34ce2dd0761601b6 MD5 · raw file

  1. <?php
  2. final class DifferentialChangesetOneUpRenderer
  3. extends DifferentialChangesetHTMLRenderer {
  4. public function isOneUpRenderer() {
  5. return true;
  6. }
  7. protected function getRendererTableClass() {
  8. return 'diff-1up';
  9. }
  10. public function getRendererKey() {
  11. return '1up';
  12. }
  13. protected function renderColgroup() {
  14. return phutil_tag('colgroup', array(), array(
  15. phutil_tag('col', array('class' => 'num')),
  16. phutil_tag('col', array('class' => 'num')),
  17. phutil_tag('col', array('class' => 'copy')),
  18. phutil_tag('col', array('class' => 'unified')),
  19. ));
  20. }
  21. public function renderTextChange(
  22. $range_start,
  23. $range_len,
  24. $rows) {
  25. $primitives = $this->buildPrimitives($range_start, $range_len);
  26. return $this->renderPrimitives($primitives, $rows);
  27. }
  28. protected function renderPrimitives(array $primitives, $rows) {
  29. list($left_prefix, $right_prefix) = $this->getLineIDPrefixes();
  30. $no_copy = phutil_tag('td', array('class' => 'copy'));
  31. $no_coverage = null;
  32. $column_width = 4;
  33. $hidden = new PHUIDiffRevealIconView();
  34. $out = array();
  35. foreach ($primitives as $k => $p) {
  36. $type = $p['type'];
  37. switch ($type) {
  38. case 'old':
  39. case 'new':
  40. case 'old-file':
  41. case 'new-file':
  42. $is_old = ($type == 'old' || $type == 'old-file');
  43. $o_hidden = array();
  44. $n_hidden = array();
  45. for ($look = $k + 1; isset($primitives[$look]); $look++) {
  46. $next = $primitives[$look];
  47. switch ($next['type']) {
  48. case 'inline':
  49. $comment = $next['comment'];
  50. if ($comment->isHidden()) {
  51. if ($next['right']) {
  52. $n_hidden[] = $comment;
  53. } else {
  54. $o_hidden[] = $comment;
  55. }
  56. }
  57. break;
  58. default:
  59. break 2;
  60. }
  61. }
  62. $cells = array();
  63. if ($is_old) {
  64. if ($p['htype']) {
  65. $class = 'left old';
  66. } else {
  67. $class = 'left';
  68. }
  69. if ($type == 'old-file') {
  70. $class = "{$class} differential-old-image";
  71. }
  72. if ($left_prefix) {
  73. $left_id = $left_prefix.$p['line'];
  74. } else {
  75. $left_id = null;
  76. }
  77. $line = $p['line'];
  78. if ($o_hidden) {
  79. $line = array($hidden, $line);
  80. }
  81. $cells[] = phutil_tag(
  82. 'th',
  83. array(
  84. 'id' => $left_id,
  85. 'class' => $class,
  86. ),
  87. $line);
  88. $cells[] = phutil_tag('th', array('class' => $class));
  89. $cells[] = $no_copy;
  90. $cells[] = phutil_tag('td', array('class' => $class), $p['render']);
  91. $cells[] = $no_coverage;
  92. } else {
  93. if ($p['htype']) {
  94. $class = 'right new';
  95. $cells[] = phutil_tag('th', array('class' => $class));
  96. } else {
  97. $class = 'right';
  98. if ($left_prefix) {
  99. $left_id = $left_prefix.$p['oline'];
  100. } else {
  101. $left_id = null;
  102. }
  103. $oline = $p['oline'];
  104. if ($o_hidden) {
  105. $oline = array($hidden, $oline);
  106. }
  107. $cells[] = phutil_tag('th', array('id' => $left_id), $oline);
  108. }
  109. if ($type == 'new-file') {
  110. $class = "{$class} differential-new-image";
  111. }
  112. if ($right_prefix) {
  113. $right_id = $right_prefix.$p['line'];
  114. } else {
  115. $right_id = null;
  116. }
  117. $line = $p['line'];
  118. if ($n_hidden) {
  119. $line = array($hidden, $line);
  120. }
  121. $cells[] = phutil_tag(
  122. 'th',
  123. array(
  124. 'id' => $right_id,
  125. 'class' => $class,
  126. ),
  127. $line);
  128. $cells[] = $no_copy;
  129. $cells[] = phutil_tag('td', array('class' => $class), $p['render']);
  130. $cells[] = $no_coverage;
  131. }
  132. $out[] = phutil_tag('tr', array(), $cells);
  133. break;
  134. case 'inline':
  135. $inline = $this->buildInlineComment(
  136. $p['comment'],
  137. $p['right']);
  138. $out[] = $this->getRowScaffoldForInline($inline);
  139. break;
  140. case 'no-context':
  141. $out[] = phutil_tag(
  142. 'tr',
  143. array(),
  144. phutil_tag(
  145. 'td',
  146. array(
  147. 'class' => 'show-more',
  148. 'colspan' => $column_width,
  149. ),
  150. pht('Context not available.')));
  151. break;
  152. case 'context':
  153. $top = $p['top'];
  154. $len = $p['len'];
  155. $links = $this->renderShowContextLinks($top, $len, $rows);
  156. $out[] = javelin_tag(
  157. 'tr',
  158. array(
  159. 'sigil' => 'context-target',
  160. ),
  161. phutil_tag(
  162. 'td',
  163. array(
  164. 'class' => 'show-more',
  165. 'colspan' => $column_width,
  166. ),
  167. $links));
  168. break;
  169. default:
  170. $out[] = hsprintf('<tr><th /><th /><td>%s</td></tr>', $type);
  171. break;
  172. }
  173. }
  174. if ($out) {
  175. return $this->wrapChangeInTable(phutil_implode_html('', $out));
  176. }
  177. return null;
  178. }
  179. public function renderFileChange(
  180. $old_file = null,
  181. $new_file = null,
  182. $id = 0,
  183. $vs = 0) {
  184. // TODO: This should eventually merge into the normal primitives pathway,
  185. // but fake it for now and just share as much code as possible.
  186. $primitives = array();
  187. if ($old_file) {
  188. $primitives[] = array(
  189. 'type' => 'old-file',
  190. 'htype' => ($new_file ? 'new-file' : null),
  191. 'file' => $old_file,
  192. 'line' => 1,
  193. 'render' => $this->renderImageStage($old_file),
  194. );
  195. }
  196. if ($new_file) {
  197. $primitives[] = array(
  198. 'type' => 'new-file',
  199. 'htype' => ($old_file ? 'old-file' : null),
  200. 'file' => $new_file,
  201. 'line' => 1,
  202. 'oline' => ($old_file ? 1 : null),
  203. 'render' => $this->renderImageStage($new_file),
  204. );
  205. }
  206. // TODO: We'd like to share primitive code here, but buildPrimitives()
  207. // currently chokes on changesets with no textual data.
  208. foreach ($this->getOldComments() as $line => $group) {
  209. foreach ($group as $comment) {
  210. $primitives[] = array(
  211. 'type' => 'inline',
  212. 'comment' => $comment,
  213. 'right' => false,
  214. );
  215. }
  216. }
  217. foreach ($this->getNewComments() as $line => $group) {
  218. foreach ($group as $comment) {
  219. $primitives[] = array(
  220. 'type' => 'inline',
  221. 'comment' => $comment,
  222. 'right' => true,
  223. );
  224. }
  225. }
  226. $output = $this->renderPrimitives($primitives, 1);
  227. return $this->renderChangesetTable($output);
  228. }
  229. public function getRowScaffoldForInline(PHUIDiffInlineCommentView $view) {
  230. return id(new PHUIDiffOneUpInlineCommentRowScaffold())
  231. ->addInlineView($view);
  232. }
  233. }