This commit is contained in:
Renzo 2016-07-30 03:31:35 -05:00
parent eb78266bee
commit 9632bd9283

View File

@ -2,7 +2,7 @@
class HistoryHelper extends Rails\ActionView\Helper
{
protected $att_options;
# :all: By default, some changes are not displayed. When displaying details
# for a single change, set :all=>true to display all changes.
#
@ -11,12 +11,12 @@ class HistoryHelper extends Rails\ActionView\Helper
{
return [ 'suppress_fields' => [] ];
}
public function get_attribute_options()
{
if ($this->att_options)
return $this->att_options;
$att_options = [
# :suppress_fields => If this attribute was changed, don't display changes to specified
# fields to the same object in the same change.
@ -52,16 +52,16 @@ class HistoryHelper extends Rails\ActionView\Helper
],
'never_obsolete' => ['cached_tags' => true] # tags handle obsolete themselves per-tag
],
'Pool' => [
'primary_order' => 0,
'fields' => [
'description' => [ 'primary_order' => 5 ] # we don't handle commas correctly if this isn't last
],
'never_obsolete' => [ 'description' => true ] # changes to description aren't obsolete just because the text has changed again
],
'PoolPost' => [
'fields' => [
'sequence' => [ 'max_to_display' => 5],
@ -73,14 +73,14 @@ class HistoryHelper extends Rails\ActionView\Helper
],
'cached_tags' => [ ],
],
'Tag' => [
],
'Note' => [
],
];
foreach (array_keys($att_options) as $classname) {
$att_options[$classname] = array_merge([
'fields' => [],
@ -88,20 +88,20 @@ class HistoryHelper extends Rails\ActionView\Helper
'never_obsolete' => [],
'force_show_initial' => []
], $att_options[$classname]);
$c = $att_options[$classname]['fields'];
foreach (array_keys($c) as $field) {
$c[$field] = array_merge($this->get_default_field_options(), $c[$field]);
}
}
}
public function format_changes($history, array $options = [])
{
$html = '';
$changes = $history->history_changes;
# Group the changes by class and field.
$change_groups = [];
foreach ($changes as $c) {
@ -111,9 +111,9 @@ class HistoryHelper extends Rails\ActionView\Helper
$change_groups[$c->table_name][$c->column_name] = [];
$change_groups[$c->table_name][$c->column_name][] = $c;
}
$att_options = $this->get_attribute_options();
# Number of changes hidden (not including suppressions):
$hidden = 0;
$parts = [];
@ -126,14 +126,14 @@ class HistoryHelper extends Rails\ActionView\Helper
$field_options = isset($table_options['fields']['field']) ? $table_options['fields']['field'] : $this->get_default_field_options();
$to_suppress = array_merge($to_suppress, $field_options['suppress_fields']);
}
foreach ($to_suppress as $suppress)
unset($fields[$suppress]);
foreach ($fields as $field => $group) {
$class_name = $group[0]->master_class();
$field_options = isset($table_options['fields']['field']) ? $table_options['fields']['field'] : $this->get_default_field_options();
# Check for entry limits.
if (empty($options['specific_history'])) {
$max = isset($field_options['max_to_display']) ? $field_options['max_to_display'] : null;
@ -142,29 +142,29 @@ class HistoryHelper extends Rails\ActionView\Helper
$group = array_slice($group, $max);
}
}
# Format the rest.
foreach ($group as $c) {
if (!$c->previous && $c->changes_to_default() && empty($table_options['force_show_initial']['field']))
continue;
$part = $this->format_change($history, $c, $options, $table_options);
if (!$part)
continue;
if (!empty($field_options['primary_order']))
$primary_order = $field_options['primary_order'];
elseif (!empty($table_options['primary_order']))
$primary_order = $table_options['primary_order'];
else
$primary_order = null;
$part = array_merge($part, ['primary_order' => $primary_order]);
$parts[] = $part;
}
}
}
usort($parts, function($a, $b) {
$comp = 0;
foreach (['primary_order', 'field', 'sort_key'] as $field) {
@ -174,55 +174,55 @@ class HistoryHelper extends Rails\ActionView\Helper
$comp = 0;
else
$comp = 1;
if ($comp != 0)
break;
}
return $comp;
});
foreach (array_keys($parts) as $idx) {
if (!$idx || $parts[$idx]['field'] == $parts[$idx - 1]['field'])
continue;
$parts[$idx-1]['html'] .= ', ';
}
$html = '';
if (empty($options['show_name']) && $history->group_by_table == 'tags') {
$tag = $history->history_changes[0]->obj();
$html .= $this->tag_link($tag->name);
$html .= ': ';
}
if (!empty($history->aux()->note_body)) {
$body = $history->aux()->note_body;
if (strlen($body) > 20)
$body = substr($body, 0, 20) . '...';
$html .= 'note ' . $this->h($body) . ' ';
}
$html .= implode(' ', array_map(function($part) { return $part['html']; }, $parts));
if ($hidden > 0) {
$html .= ' (' . $this->linkTo($hidden . ' more...', ['search' => 'change:' . $history->id]) . ')';
}
return $html;
}
public function format_change($history, $change, $options, $table_options)
{
$html = '';
$classes = [];
if (empty($table_options['never_obsolete'][$change->column_name]) && $change->is_obsolete()) {
$classes[] = 'obsolete';
}
$added = '<span class="added">+</span>';
$removed = '<span class="removed">-</span>';
$sort_key = $change->remote_id;
$primary_order = 1;
switch ($change->table_name) {
@ -237,7 +237,7 @@ class HistoryHelper extends Rails\ActionView\Helper
}
$html .= '</span>';
break;
case 'parent_id':
$html .= 'parent:';
if ($change->value) {
@ -250,7 +250,7 @@ class HistoryHelper extends Rails\ActionView\Helper
} else {
$html .= 'none';
}
if ($change->previous) {
$html .= '←';
if ($change->previous->value) {
@ -265,7 +265,7 @@ class HistoryHelper extends Rails\ActionView\Helper
}
}
break;
case 'source':
if ($change->previous) {
$html .= sprintf("source changed from <span class='name-change'>%s</span> to <span class='name-change'>%s</span>", $this->source_link($change->previous->value, false), $this->source_link($change->value, false));
@ -273,32 +273,38 @@ class HistoryHelper extends Rails\ActionView\Helper
$html .= sprintf("source: <span class='name-change'>%s</span>", $this->source_link($change->value, false));
}
break;
case 'frames_pending':
$html .= 'frames changed: ' . $this->h($change->value ?: '(none)');
break;
case 'is_rating_locked':
# Trueish: if a value equals true or 't'
$html .= $change->value || $change->value == 't' ? $added : $removed;
$html .= 'rating-locked';
break;
case 'is_note_locked':
# Trueish
$html .= $change->value || $change->value == 't' ? $added : $removed;
$html .= 'note-locked';
break;
case 'is_shown_in_index':
# Trueish
$html .= $change->value || $change->value == 't' ? $added : $removed;
$html .= 'shown';
break;
case 'cached_tags':
$previous = $change->previous;
$changes = Post::tag_changes($change, $previous, $change->latest());
$list = [];
$list[] = $this->tag_list($changes['added_tags'], ['obsolete' => $changes['obsolete_added_tags'], 'prefix' => '+', 'class' => 'added']);
$list[] = $this->tag_list($changes['removed_tags'], ['obsolete' => $changes['obsolete_removed_tags'], 'prefix' => '-', 'class' => 'removed']);
if (!empty($options['show_all_tags']))
$list[] = $this->tag_list($changes['unchanged_tags'], ['prefix' => '', 'class' => 'unchanged']);
$html .= trim(implode(' ', $list));
@ -308,7 +314,7 @@ class HistoryHelper extends Rails\ActionView\Helper
case 'pools':
$primary_order = 0;
switch ($change->column_name) {
case 'name':
if ($change->previous) {
@ -317,7 +323,7 @@ class HistoryHelper extends Rails\ActionView\Helper
$html .= sprintf("name: <span class='name-change'>%s</span>", $this->h($change->value));
}
break;
case 'description':
if ($change->value === '') {
$html .= 'description removed';
@ -328,7 +334,7 @@ class HistoryHelper extends Rails\ActionView\Helper
$html .= 'description added: ';
else
$html .= 'description changed: ';
# Show a diff if there's a previous description and it's not blank. Otherwise,
# just show the new text.
$show_diff = $change->previous && $change->previous->value !== '';
@ -336,11 +342,11 @@ class HistoryHelper extends Rails\ActionView\Helper
$text = Moebooru\Diff::generate($change->previous->value, $change->value);
else
$text = $this->h($change->value);
# If there's only one line in the output, just show it inline. Otherwise, show it
# as a separate block.
$multiple_lines = is_int(strpos($text, '<br>')) || is_int(strpos($text, '<br />'));
$show_in_detail = !empty($options['specific_history']) || !empty($options['specific_object']);
if (!$multiple_lines)
$display = $text;
@ -348,20 +354,20 @@ class HistoryHelper extends Rails\ActionView\Helper
$display = "<div class='diff text-block'>${text}</div>";
else
$display = "<div class='initial-diff text-block'>${text}</div>";
if ($multiple_lines && !$show_in_detail)
$html .= "<a onclick='$(this).hide(); $(this).next().show()' href='#'>(show changes)</a><div style='display: none;'>${display}</div>";
else
$html .= $display;
}
break;
case 'is_public':
# Trueish
$html .= $change->value || $change->value == 't' ? $added : $removed;
$html .= 'public';
break;
case 'is_active':
# Trueish
$html .= $change->value || $change->value == 't' ? $added : $removed;
@ -369,7 +375,7 @@ class HistoryHelper extends Rails\ActionView\Helper
break;
}
break;
case 'pools_posts':
# Sort the output by the post id.
$sort_key = $change->obj()->post->id;
@ -377,10 +383,10 @@ class HistoryHelper extends Rails\ActionView\Helper
case 'active':
# Trueish
$html .= $change->value || $change->value == 't' ? $added : $removed;
$html .= $this->linkTo('post #' . $change->obj()->post_id, ['post#show', 'id' => $change->obj()->post_id]);
break;
case 'sequence':
/**
* MI: For some reason the sequence is shown in the first HistoryChange created,
@ -388,7 +394,7 @@ class HistoryHelper extends Rails\ActionView\Helper
*/
if (!$change->previous)
return null;
$seq = 'order:' . $change->obj()->post_id . ':' . $change->value;
$seq .= '←' . $change->previous->value;
$html .= $this->linkTo($seq, ['post#show', 'id' => $change->obj()->post_id]);
@ -407,7 +413,7 @@ class HistoryHelper extends Rails\ActionView\Helper
$html .= '←<span class="tag-type-' . $tag_type . '">' . $tag_type . '</span>';
}
break;
case 'is_ambiguous':
# Trueish
$html .= $change->value || $change->value == 't' ? $added : $removed;
@ -415,7 +421,7 @@ class HistoryHelper extends Rails\ActionView\Helper
break;
}
break;
case 'notes':
switch($change->column_name) {
case 'body':
@ -425,14 +431,14 @@ class HistoryHelper extends Rails\ActionView\Helper
$html .= sprintf("body: <span class='name-change'>%s</span>", $this->h($change->value));
}
break;
case 'x':
case 'y':
case 'width':
case 'height':
$html .= $change->column_name . ':' . $this->h($change->value);
break;
case 'is_active':
# Trueish
if ($change->value || $change->value == 't') {
@ -447,33 +453,33 @@ class HistoryHelper extends Rails\ActionView\Helper
}
break;
}
$span = '<span class="' . implode(' ', $classes) . '">' . $html . '</span>';
return [
'html' => $span,
'field' => $change->column_name,
'sort_key' => $sort_key
];
}
public function tag_list($tags, array $options = [])
{
if (!$tags)
return '';
$html = '<span class="' . (!empty($options['class']) ? $options['class'] : '') . '">';
$tags_html = [];
foreach ($tags as $name) {
$tags_html[] = $this->tag_link($name, $options);
}
if (!$tags_html)
return '';
$html .= implode(' ', $tags_html);
$html .= '</span>';
return $html;
}
}
}