image ordering works

This commit is contained in:
John Bintz 2009-11-13 19:05:41 -05:00
parent 060b03ec24
commit 836bd13a89
6 changed files with 170 additions and 48 deletions

View File

@ -338,7 +338,7 @@ class ComicPressAdmin {
if (is_numeric($_POST['post_ID'])) {
if ($post = get_post($_POST['post_ID'])) {
$comic_post = new ComicPressComicPost($post);
$comic_post->change_comic_image_ordering($this->_json_decode(stripslashes($_POST['cp']['comic_order'])));
$comic_post->update_post_media_data($this->_json_decode(stripslashes($_POST['cp']['comic_order'])));
}
}
}

View File

@ -119,19 +119,21 @@ class ComicPressComicPost {
function update_post_media_data($info) {
$ordering = array();
foreach ($info as $image) {
$image = (array)$image;
if (isset($image['id'])) {
$data = array(
'enabled' => false
);
foreach ($image as $field => $value) {
switch ($field) {
case 'enabled': $data['enabled'] = true; break;
case 'enabled': $data['enabled'] = $value; break;
case 'children':
$any_entered = false;
foreach ($value as $type => $attached_id) {
if (!empty($attached_id)) { $any_entred = true; break; }
foreach ((array)$value as $type => $attached_id) {
if (!empty($attached_id)) {
if (!isset($data['children'])) { $data['children'] = array(); }
$data['children'][$type] = $attached_id;
}
}
if ($any_entered) { $data['children'] = $value; }
break;
}
}

View File

@ -11,42 +11,46 @@
</em></p>
<?php if (!empty($ordering)) { ?>
<div id="comic-ordering">
<?php foreach ($ordering as $id => $info) {
<?php foreach ($ordering as $id => $attachment_info) {
$result = ComicPressBackend::generate_from_id($id);
if (!empty($result)) {
$info = $result->get_info(); ?>
<div class="cp-comic-attachment" id="attachment_<?php echo $id ?>">
<img src="<?php echo $result->url() ?>" border="0" height="<?php echo $zoom_level ?>" />
<div class="cp-comic-info">
<p>
<input type="checkbox" name="cp[attachments][<?php echo $id ?>][visible]" value="yes" /> <?php _e('Allow this image to be shown', 'comicpress') ?>
</p>
<?php if (isset($info['file'])) { ?>
<p><strong><?php echo $result->source_name ?>:</strong> <?php echo basename($info['file']) ?></p>
<?php } ?>
<?php if (isset($info['width']) && isset($info['height'])) { ?>
<div style="overflow: hidden">
<img src="<?php echo $result->url() ?>" border="0" height="<?php echo $zoom_level ?>" />
<div class="cp-comic-info">
<p>
<strong><?php _e('Size:', 'comicpress') ?></strong>
<?php printf('%dx%d', $info['width'], $info['height'] ) ?>
<label>
<input type="checkbox" name="cp[attachments][<?php echo $id ?>][visible]" value="yes" <?php echo ($attachment_info['enabled']) ? 'checked="checked"' : '' ?>/> <?php _e('Allow this image to be shown', 'comicpress') ?>
</label>
</p>
<?php } ?>
<?php if (count($comicpress->comicpress_options['image_types']) > 1) { ?>
<a class="comic-ordering-show-associations" href="#"><?php _e('Edit associations', 'comicpress') ?></a>
<div class="comic-ordering-associations">
<ul>
<?php foreach ($comicpress->comicpress_options['image_types'] as $type => $info) { ?>
<?php if (!$info['default']) { ?>
<li>
<strong><?php echo $info['name'] ?>:</strong>
<select name="cp[attachments][<?php echo $id ?>][<?php echo $type ?>]"></select>
</li>
<?php if (isset($info['file'])) { ?>
<p><strong><?php echo $result->source_name ?>:</strong> <?php echo basename($info['file']) ?></p>
<?php } ?>
<?php if (isset($info['width']) && isset($info['height'])) { ?>
<p>
<strong><?php _e('Size:', 'comicpress') ?></strong>
<?php printf('%dx%d', $info['width'], $info['height'] ) ?>
</p>
<?php } ?>
<?php if (count($comicpress->comicpress_options['image_types']) > 1) { ?>
<a class="comic-ordering-show-associations" href="#"><?php _e('Edit associations', 'comicpress') ?></a>
<div class="comic-ordering-associations">
<ul>
<?php foreach ($comicpress->comicpress_options['image_types'] as $type => $info) { ?>
<?php if (!$info['default']) { ?>
<li>
<strong><?php echo $info['name'] ?>:</strong>
<select name="cp[attachments][<?php echo $id ?>][<?php echo $type ?>]"></select>
</li>
<?php } ?>
<?php } ?>
<?php } ?>
</ul>
</div>
<?php } ?>
</ul>
</div>
<?php } ?>
</div>
<br style="clear: both" />
</div>
<br style="clear: both" />
</div>
<?php } ?>
<?php } ?>

View File

@ -66,7 +66,15 @@
}
.cp-comic-attachment {
cursor: move
cursor: move;
background-color: #ffd7d7;
margin-bottom: 10px;
border-bottom: solid #aaa 1px;
padding-bottom: 10px
}
.enabled {
background-color: transparent
}
#ordering-refresh {
@ -122,4 +130,5 @@
.comic-ordering-associations {
border: solid #aaa 1px;
padding: 5px;
width: 100%
}

View File

@ -1,16 +1,56 @@
var ComicImageOrdering = {};
ComicImageOrdering.get_ordering = function() {
var ordering = {};
$('cp-comic-order').value = Object.toJSON(ordering);
};
ComicImageOrdering.ids_with_children = {};
ComicImageOrdering.build_dropdowns = function() {
$$('#comic-ordering select').each(function(sel) {
sel.innerHTML = '';
sel.appendChild(new Element('option', { value: '' }).update('-- default --'));
ComicImageOrdering.available_attachments.each(function(attachment) {
sel.appendChild(new Element('option', { value: attachment.id }).update(attachment.name));
$$('.cp-comic-attachment').each(function(a) {
a.show();
a[a.select('input[type=checkbox]').pop().checked ? 'addClassName' : 'removeClassName']('enabled');
});
var unavailable_associations = {};
$H(ComicImageOrdering.ids_with_children).each(function(pair) {
$H(pair.value).values().each(function(e) {
unavailable_associations[e] = true;
$('attachment_' + e).hide();
});
});
$$('#comic-ordering .cp-comic-attachment').each(function(att) {
var id = att.id.replace(/^attachment_/,'');
att.select('select').each(function(sel) {
var type = sel.name.replace(/^.*\[([^\]]+)\]$/, '$1');
var target_selected_index = 0;
sel.innerHTML = '';
sel.appendChild(new Element('option', { value: '' }).update('-- default --'));
var current_index = 1;
ComicImageOrdering.available_attachments.each(function(attachment, i) {
var ok = !(attachment.id == id);
if (ok) {
if (unavailable_associations[attachment.id]) {
ok = false;
if (ComicImageOrdering.ids_with_children[id][type]) {
if (ComicImageOrdering.ids_with_children[id][type] == attachment.id) {
ok = true; target_selected_index = current_index;
}
}
}
if (ok) {
if ($H(ComicImageOrdering.ids_with_children[attachment.id]).keys().length == 0) {
var o = { value: attachment.id };
var description = attachment.name;
if (attachment.attachment.width && attachment.attachment.height) {
description += ' - ' + attachment.attachment.width + 'x' + attachment.attachment.height
}
sel.appendChild(new Element('option', o).update(description));
current_index++;
}
}
}
});
sel.selectedIndex = target_selected_index;
});
});
};
@ -18,18 +58,73 @@ ComicImageOrdering.build_dropdowns = function() {
ComicImageOrdering.build_response = function() {
var output = [];
$('comic-ordering').select('.cp-comic-attachment').each(function(att) {
if (att.visible) {
var data = {};
data.id = att.id.replace(/^attachment_/,'');
data.enabled = att.select('input[type=checkbox]').pop().checked;
data.children = {};
att.select('select').each(function(sel) {
var type = sel.name.replace(/^.*\[([^\]]+)\]$/, '$1');
data.children[type] = $F(sel);
});
output.push(data);
}
});
$('cp-comic-order').value = Object.toJSON(output);
};
ComicImageOrdering.setup = function() {
Sortable.create($('comic-ordering'), {
tag: 'div',
handle: 'div',
onUpdate: ComicImageOrdering.get_ordering
onUpdate: function() {
ComicImageOrdering.build_dropdowns();
ComicImageOrdering.build_response();
}
});
ComicImageOrdering.available_attachments.each(function(a) {
ComicImageOrdering.ids_with_children[a.id] = (a.ordering.children) ? a.ordering.children : {};
});
$$('#comic-ordering .cp-comic-attachment').each(function(att) {
var id = att.id.replace(/^attachment_/,'');
att.select('select').each(function(sel) {
var type = sel.name.replace(/^.*\[([^\]]+)\]$/, '$1');
sel.observe('change', function(e) {
Event.stop(e);
var requested_child = $F(e.target);
if (requested_child) {
ComicImageOrdering.ids_with_children[id][type] = requested_child;
} else {
delete ComicImageOrdering.ids_with_children[id][type];
}
ComicImageOrdering.build_dropdowns();
ComicImageOrdering.build_response();
});
});
var associations_box = att.select('.comic-ordering-associations').pop();
associations_box[($H(ComicImageOrdering.ids_with_children[id]).keys().length == 0) ? 'hide' : 'show']();
att.select('.comic-ordering-show-associations').pop().observe('click', function(e) {
Event.stop(e);
associations_box.toggle();
});
});
$$('#comic-ordering input[type=checkbox]').each(function(ch) {
ch.observe('change', function() {
ComicImageOrdering.build_dropdowns();
ComicImageOrdering.build_response();
});
});
ComicImageOrdering.get_ordering();
ComicImageOrdering.build_dropdowns();
ComicImageOrdering.build_response();
};
Event.observe(window, 'load', function() {

View File

@ -76,14 +76,26 @@ class ComicPressComicPostTest extends PHPUnit_Framework_TestCase {
array(
array(
'id' => 'attachment-1',
'enabled' => 'yes',
'enabled' => true,
'children' => array('rss' => '', 'archive' => '')
)
),
array(
'attachment-1' => array('enabled' => true)
)
)
),
array(
array(
array(
'id' => 'attachment-1',
'enabled' => false,
'children' => array('rss' => 'attachment-2', 'archive' => '')
)
),
array(
'attachment-1' => array('enabled' => false, 'children' => array('rss' => 'attachment-2'))
)
),
);
}