rework bookmark widget

This commit is contained in:
John Bintz 2009-12-20 22:01:07 -05:00
parent 3b0c3cea0d
commit 64c94a292f
2 changed files with 119 additions and 65 deletions

View File

@ -1,12 +1,3 @@
var button_images = {
'clear-tag': {
'off': '3a.gif', 'on': '3.gif'
},
'goto-tag': {
'off': '2a.gif', 'on': '2.gif'
}
};
var BookmarkInfo = Class.create({
'def': {
'permalink': false
@ -32,41 +23,72 @@ var BookmarkInfo = Class.create({
}
});
Event.observe(window, 'load', function() {
var ComicBookmark = {};
ComicBookmark.setup = function(id, mode, url, elements) {
var bookmark_info = new BookmarkInfo();
var info = bookmark_info.read();
if ($('comic-bookmark-holder')) {
if ($(id)) {
var hrefs = {};
$$('#comic-bookmark-holder a').each(function(a) {
$$('#' + id + ' a').each(function(a) {
var name = $w(a.className).shift();
hrefs[name] = a;
});
switch (mode) {
case 'three-button':
var set_goto_tag = function(i) {
hrefs['goto-tag'].href = (i.permalink ? i.permalink : "#");
['goto-tag','clear-tag'].each(function(which) {
hrefs[which].select('img')[0].src = image_root + button_images[which][i.permalink ? "on" : "off"];
hrefs[which].innerHTML = elements[which + '-' + (i.permalink ? "on" : "off")];
});
};
hrefs['tag-page'].innerHTML = elements['tag-page'];
bookmark_info.onWrite = function(i) { set_goto_tag(i); }
set_goto_tag(info);
Event.observe(hrefs['tag-page'], 'click', function(e) {
hrefs['tag-page'].observe('click', function(e) {
Event.stop(e);
info.permalink = permalink;
info.permalink = url;
bookmark_info.write(info);
});
Event.observe(hrefs['clear-tag'], 'click', function(e) {
hrefs['goto-tag'].observe('click', function(e) {
if (hrefs['goto-tag'].href == "#") { Event.stop(e); }
});
hrefs['clear-tag'].observe('click', function(e) {
Event.stop(e);
info.permalink = false;
bookmark_info.write(info);
});
Event.observe(hrefs['goto-tag'], 'click', function(e) {
if (hrefs['goto-tag'].href == "#") { Event.stop(e); }
});
break;
case 'one-button':
var set_goto_tag = function(i) {
hrefs['bookmark-clicker'].href = (i.permalink ? i.permalink : "#");
hrefs['bookmark-clicker'].innerHTML = elements['bookmark-clicker-' + (i.permalink ? "on" : "off")];
};
bookmark_info.onWrite = function(i) { set_goto_tag(i); }
set_goto_tag(info);
hrefs['bookmark-clicker'].observe('click', function(e) {
var current_link = info.permalink;
info.permalink = (hrefs['bookmark-clicker'].href.match(/#$/)) ? url : false;
bookmark_info.write(info);
if (hrefs['bookmark-clicker'].href.match(/#$/) == null) {
hrefs['bookmark-clicker'].href = url;
Event.stop(e);
} else {
document.location.href = current_link;
Event.stop(e);
}
});
break;
}
}
};

View File

@ -33,11 +33,11 @@ class BookmarkWidget extends WP_Widget {
)
),
'one-button' => array(
'set-bookmark' => array(
'bookmark-clicker-off' => array(
'label' => __('Set bookmark', 'comicpress'),
'default' => __('+Bookmark', 'comicpress')
),
'use-bookmark' => array(
'bookmark-clicker-on' => array(
'label' => __('Use bookmark', 'comicpress'),
'default' => __('>>Bookmark', 'comicpress')
)
@ -145,11 +145,43 @@ class BookmarkWidget extends WP_Widget {
return $instance;
}
function widget($instance) {
?>
<div id="comic-bookmark-holder">
<a href="#" class="tag-page"><img src="<?php echo get_template_directory_uri() ?>/images/1.gif" /></a><a href="#" class="goto-tag"><img /></a><a href="#" class="clear-tag"><img /></a>
function widget($args, $instance) {
global $post;
extract($args, EXTR_SKIP);
echo $before_widget;
$title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']);
if (!empty($title)) { echo $before_title . $title . $after_title; };
$id = 'comic-bookmark-' . md5(rand());
switch ($instance['mode']) {
case 'three-button': ?>
<div class="bookmark-widget" id="<?php echo $id ?>">
<a href="#" class="tag-page"></a>
<a href="#" class="goto-tag"></a>
<a href="#" class="clear-tag"></a>
</div>
<?php break;
case 'one-button': ?>
<div class="bookmark-widget" id="<?php echo $id ?>">
<a href="#" class="bookmark-clicker"></a>
</div>
<?php break;
} ?>
<script type="text/javascript">
(function() {
ComicBookmark.setup('<?php echo $id ?>', '<?php echo $instance['mode'] ?>', '<?php echo get_permalink($post) ?>', {
<?php
$elements = array();
foreach (array_keys($this->text_fields[$instance['mode']]) as $field) {
$elements[] = "'{$field}': '{$instance[$field]}'";
}
echo implode(',', $elements);
?>
});
}());
</script><?php
echo $after_widget;
}
}