working on making bookmark widget work w/ prototype and cookiejar

This commit is contained in:
John Bintz 2009-08-23 16:44:54 -04:00
parent fa64f77738
commit 88c75878e4
7 changed files with 316 additions and 40 deletions

View File

@ -1,6 +1,8 @@
<?php <?php
class ComicPressAddonBookmarkWidget extends ComicPressAddon { class ComicPressAddonBookmarkWidget extends ComicPressAddon {
var $name = "Bookmark Widget";
function init($comicpress) { function init($comicpress) {
wp_register_sidebar_widget('comic-bookmark', __('Comic Bookmark', 'comicpress'), array(&$this, 'render_widget'), array('description' => __('Let your readers save their place via a cookie.', 'comicpress'))); wp_register_sidebar_widget('comic-bookmark', __('Comic Bookmark', 'comicpress'), array(&$this, 'render_widget'), array('description' => __('Let your readers save their place via a cookie.', 'comicpress')));
@ -8,31 +10,99 @@ class ComicPressAddonBookmarkWidget extends ComicPressAddon {
add_action('wp_head', array(&$this, 'wp_head')); add_action('wp_head', array(&$this, 'wp_head'));
$this->comicpress->additional_javascripts[] = '/js/bookmark.js'; $this->comicpress->additional_javascripts[] = '/js/bookmark.js';
wp_enqueue_script('prototype');
wp_enqueue_script('cookiejar', get_template_directory_uri() . '/js/cookiejar.js', array('prototype'));
} }
function wp_head() { function wp_head() {
$last_comic = $this->comicpress->get_last_comic(); ?> $last_comic = $this->comicpress->get_last_comic(); ?>
<script type="text/javascript"> <script type="text/javascript">
var imgTag = '<?php bloginfo('template_directory'); ?>/images/1.gif'; //add tag image. var image_root = '<?php bloginfo('template_directory'); ?>/images/';
var imgClearOff = '<?php bloginfo('template_directory'); ?>/images/3a.gif'; //no comic tagged, clear not possible
var imgGotoOff = '<?php bloginfo('template_directory'); ?>/images/2a.gif'; //no comic tagged, goto not possible var button_images = {
var imgClearOn = '<?php bloginfo('template_directory'); ?>/images/3.gif'; //clear a tag, shows when comic previously tagged 'clear-tag': {
var imgGotoOn = '<?php bloginfo('template_directory'); ?>/images/2.gif'; //shows when a comic is tagged 'off': '3a.gif', 'on': '3.gif'
var imgInfo = '<?php bloginfo('template_directory'); ?>/images/4.gif'; //img that displays the help },
var comicDir = '/'; //alter this if you run multiple comics in different directories on your site. 'goto-tag': {
'off': '2a.gif', 'on': '2.gif'
var comicPermalink = '<?php echo get_permalink($last_comic->ID) ?>'; }
};
var permalink = '<?php echo get_permalink($last_comic->ID) ?>';
var isHome = <?php echo is_home() ? "true" : "false" ?>; var BookmarkInfo = Class.create({
var isSingle = <?php echo (is_single() && $this->comicpress->in_comic_category()) ? "true" : "false" ?>; 'default': {
'permalink': false
},
'initialize': function() {
this.jar = new CookieJar({
'expires': 60 * 60 * 24 * 31,
'path': '<?php bloginfo('template_directory') ?>'
});
},
'read': function() {
var bookmark_info = this.jar.get('bookmark-info');
return bookmark_info;
},
'write': function(bookmark_info) {
this.jar.put('bookmark-info', bookmark_info);
if (this.onWrite) { this.onWrite(bookmark_info); }
}
});
Event.observe(window, 'load', function() {
var bookmark_info = new BookmarkInfo();
var info = bookmark_info.read();
var hrefs = {};
$$('#comic-bookmark-holder a').each(function(a) {
var name = $w(a.className).shift();
hrefs[name] = a;
});
var set_goto_tag = function(i) {
top.console.log(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"];
top.console.log(i.permalink ? "on" : "off");
});
};
bookmark_info.onWrite = function(i) { set_goto_tag(i); }
set_goto_tag(info);
Event.observe(hrefs['tag-page'], 'click', function(e) {
Event.stop(e);
info.permalink = permalink;
bookmark_info.write(info);
});
Event.observe(hrefs['clear-tag'], '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); }
});
});
</script> </script>
<?php <?php
} }
function render_widget() { function render_widget() {
?> ?>
<div class="comic-bookmark"> <div id="comic-bookmark-holder">
<script type="text/javascript">writeBookmarkWidget()</script> <a href="#" class="tag-page"><img src="<?php bloginfo('template_directory'); ?>/images/1.gif" /></a>
<a href="#" class="goto-tag"><img /></a>
<a href="#" class="clear-tag"><img /></a>
<a href="#" class="info-tag"><img src="<?php bloginfo('template_directory'); ?>/images/4.gif" </a>
</div> </div>
<?php <?php
} }

View File

@ -1,6 +1,10 @@
<?php <?php
class ComicPressAddonCore extends ComicPressAddon { class ComicPressAddonCore extends ComicPressAddon {
var $is_addon_manager = true;
var $cannot_be_disabled = true;
var $name = "ComicPress Core";
/** /**
* Initialize the addon. * Initialize the addon.
* @param ComicPress $comicpress The master ComicPress object. * @param ComicPress $comicpress The master ComicPress object.
@ -504,7 +508,9 @@ class ComicPressAddonCore extends ComicPressAddon {
* Update ComicPress options. * Update ComicPress options.
*/ */
function handle_update_comicpress_options() { function handle_update_comicpress_options() {
$this->comicpress->comicpress_options['helpers'] = array(); foreach (array('helpers', 'options') as $type) {
$this->comicpress->comicpress_options[$type] = array();
}
foreach ($this->comicpress->comicpress_options as $option => $value) { foreach ($this->comicpress->comicpress_options as $option => $value) {
if (isset($_POST['cp'][$option])) { if (isset($_POST['cp'][$option])) {
switch ($option) { switch ($option) {
@ -550,8 +556,9 @@ class ComicPressAddonCore extends ComicPressAddon {
$this->comicpress->comicpress_options[$option] = $_POST['cp'][$option]; $this->comicpress->comicpress_options[$option] = $_POST['cp'][$option];
break; break;
case 'helpers': case 'helpers':
foreach ($_POST['cp'][$option] as $helper => $set) { case 'addons':
$this->comicpress->comicpress_options['helpers'][$helper] = true; foreach ($_POST['cp'][$option] as $type => $set) {
$this->comicpress->comicpress_options[$option][$type] = true;
} }
break; break;
} }

View File

@ -69,7 +69,7 @@
<?php } <?php }
} }
?> ?>
<p><em>(categories can be modified on the Posts -> Categories page)</em></p> <p><em><?php _e('(categories can be modified on the Posts -> Categories page)', 'comicpress') ?></em></p>
</td> </td>
</tr> </tr>
<tr> <tr>
@ -98,20 +98,51 @@
<h3><?php _e('Admin Options', 'comicpress') ?></h3> <h3><?php _e('Admin Options', 'comicpress') ?></h3>
<table class="widefat fixed"> <table class="widefat fixed">
<tr> <tr>
<th scope="row"> <th scope="row"><?php _e('Enable editing helpers', 'comicpress') ?></th>
Enable editing helpers
</th>
<td> <td>
<?php <?php
foreach (array( foreach (array(
"show_partials_info" => __('Show partials info', 'comicpress'), "show_partials_info" => __('Show partials info', 'comicpress'),
"show_inline_comic_ordering" => __('Show inline comic ordering', 'comicpress') "show_inline_comic_ordering" => __('Show inline comic ordering', 'comicpress')
) as $key => $label) { ?> ) as $key => $label) { ?>
<label><input type="checkbox" name="cp[helpers][<?php echo $key ?>]" value="yes" <?php echo (($this->comicpress->comicpress_options['helpers'][$key] === true) ? 'checked="checked"' : '') ?> /> <?php echo $label ?></label><br /> <label>
<input type="checkbox"
name="cp[helpers][<?php echo $key ?>]"
value="yes"
<?php echo (($this->comicpress->comicpress_options['helpers'][$key] === true) ? 'checked="checked"' : '') ?> />
<?php echo $label ?>
</label>
<br />
<?php } <?php }
?> ?>
</td> </td>
</tr> </tr>
<?php if (is_array($this->all_addons)) { ?>
<tr>
<th scope="row"><?php _e('Enable addons', 'comicpress') ?></th>
<td>
<?php
foreach ($this->all_addons as $addon) {
if (!empty($addon->name)) {
$enabled = ($addon->is_addon_manager !== true);
$checked = $enabled && $this->comicpress->comicpress_options['addons'][$addon->name];
?>
<label>
<input type="checkbox"
name="cp[addons][<?php echo $addon->name ?>]"
value="yes"
<?php echo !$enabled ? 'disabled="disabled"' : '' ?>
<?php echo $checked ? 'checked="checked"' : '' ?> />
<?php echo $addon->name ?>
</label><br />
<?php }
}
?>
</td>
</tr>
<?php } ?>
</table> </table>
<input type="submit" value="<?php _e("Submit Changes", 'comicpress') ?>" /> </form> <input class="button" type="submit" value="<?php _e('Submit Changes', 'comicpress') ?>" />
</form>
</div> </div>

View File

@ -7,6 +7,7 @@
class ComicPressAddonSearchTranscripts extends ComicPressAddon { class ComicPressAddonSearchTranscripts extends ComicPressAddon {
var $custom_template_default = 'search-transcript.php'; var $custom_template_default = 'search-transcript.php';
var $name = "Search Transcripts";
function init($comicpress) { function init($comicpress) {
add_filter('posts_join', array(&$this, 'search_custom_join')); add_filter('posts_join', array(&$this, 'search_custom_join'));

View File

@ -16,7 +16,8 @@ class ComicPress {
'category_usage' => 'storyline', 'category_usage' => 'storyline',
'layout' => 'classic.inc', 'layout' => 'classic.inc',
'helpers' => array(), 'helpers' => array(),
'override_partials' => array() 'override_partials' => array(),
'addons' => array()
); );
var $additional_stylesheets = array(); var $additional_stylesheets = array();

View File

@ -16,7 +16,7 @@ function __comicpress_init() {
$comicpress = new ComicPress(); $comicpress = new ComicPress();
$comicpress->init(); $comicpress->init();
$addons = array(); $addons = array();
if (is_dir($addons_dir = (dirname(__FILE__) . '/addons'))) { if (is_dir($addons_dir = (dirname(__FILE__) . '/addons'))) {
$entries = glob($addons_dir . '/*'); $entries = glob($addons_dir . '/*');
@ -29,24 +29,29 @@ function __comicpress_init() {
$classname = "ComicPressAddon${classname}"; $classname = "ComicPressAddon${classname}";
if (class_exists($classname)) { if (class_exists($classname)) {
$addon =& new $classname(); $addon =& new $classname();
$addon->init(&$comicpress); if (
if (current_user_can('edit_posts')) { $comicpress->comicpress_options['addons'][$addon->name] ||
if (is_array($_REQUEST['cp'])) { $addon->is_addon_manager
if (isset($_REQUEST['cp']['_nonce'])) { ) {
if (wp_verify_nonce($_REQUEST['cp']['_nonce'], 'comicpress')) { $addon->init(&$comicpress);
if (method_exists($addon, 'handle_update')) { if (current_user_can('edit_posts')) {
$addon->handle_update(); if (is_array($_REQUEST['cp'])) {
$comicpress->load(); if (isset($_REQUEST['cp']['_nonce'])) {
if (wp_verify_nonce($_REQUEST['cp']['_nonce'], 'comicpress')) {
if (method_exists($addon, 'handle_update')) {
$addon->handle_update();
$comicpress->load();
}
} }
} }
} }
} if (is_admin()) {
if (is_admin()) { add_action('admin_notices', array(&$addon, 'display_messages'));
add_action('admin_notices', array(&$addon, 'display_messages')); } else {
} else { add_action('wp_head', array(&$addon, 'display_messages'));
add_action('wp_head', array(&$addon, 'display_messages')); }
} }
} }
$addons[] = $addon; $addons[] = $addon;
} }
@ -54,6 +59,10 @@ function __comicpress_init() {
} }
} }
} }
}
foreach ($addons as $addon) {
if ($addon->is_addon_manager) { $addon->all_addons =& $addons; break; }
} }
$layouts = $comicpress->get_layout_choices(); $layouts = $comicpress->get_layout_choices();

157
js/cookiejar.js Normal file
View File

@ -0,0 +1,157 @@
/**
* Javascript code to store data as JSON strings in cookies.
* It uses prototype.js 1.5.1 (http://www.prototypejs.org)
*
* Author : Lalit Patel
* Website: http://www.lalit.org/lab/jsoncookies
* License: Apache Software License 2
* http://www.apache.org/licenses/LICENSE-2.0
* Version: 0.5
* Updated: Jan 26, 2009
*
* Chnage Log:
* v 0.5
* - Changed License from CC to Apache 2
* v 0.4
* - Removed a extra comma in options (was breaking in IE and Opera). (Thanks Jason)
* - Removed the parameter name from the initialize function
* - Changed the way expires date was being calculated. (Thanks David)
* v 0.3
* - Removed dependancy on json.js (http://www.json.org/json.js)
* - empty() function only deletes the cookies set by CookieJar
*/
var CookieJar = Class.create();
CookieJar.prototype = {
/**
* Append before all cookie names to differntiate them.
*/
appendString: "__CJ_",
/**
* Initializes the cookie jar with the options.
*/
initialize: function(options) {
this.options = {
expires: 3600, // seconds (1 hr)
path: '', // cookie path
domain: '', // cookie domain
secure: '' // secure ?
};
Object.extend(this.options, options || {});
if (this.options.expires != '') {
var date = new Date();
date = new Date(date.getTime() + (this.options.expires * 1000));
this.options.expires = '; expires=' + date.toGMTString();
}
if (this.options.path != '') {
this.options.path = '; path=' + escape(this.options.path);
}
if (this.options.domain != '') {
this.options.domain = '; domain=' + escape(this.options.domain);
}
if (this.options.secure == 'secure') {
this.options.secure = '; secure';
} else {
this.options.secure = '';
}
},
/**
* Adds a name values pair.
*/
put: function(name, value) {
name = this.appendString + name;
cookie = this.options;
var type = typeof value;
switch(type) {
case 'undefined':
case 'function' :
case 'unknown' : return false;
case 'boolean' :
case 'string' :
case 'number' : value = String(value.toString());
}
var cookie_str = name + "=" + escape(Object.toJSON(value));
try {
document.cookie = cookie_str + cookie.expires + cookie.path + cookie.domain + cookie.secure;
} catch (e) {
return false;
}
return true;
},
/**
* Removes a particular cookie (name value pair) form the Cookie Jar.
*/
remove: function(name) {
name = this.appendString + name;
cookie = this.options;
try {
var date = new Date();
date.setTime(date.getTime() - (3600 * 1000));
var expires = '; expires=' + date.toGMTString();
document.cookie = name + "=" + expires + cookie.path + cookie.domain + cookie.secure;
} catch (e) {
return false;
}
return true;
},
/**
* Return a particular cookie by name;
*/
get: function(name) {
name = this.appendString + name;
var cookies = document.cookie.match(name + '=(.*?)(;|$)');
if (cookies) {
return (unescape(cookies[1])).evalJSON();
} else {
return null;
}
},
/**
* Empties the Cookie Jar. Deletes all the cookies.
*/
empty: function() {
keys = this.getKeys();
size = keys.size();
for(i=0; i<size; i++) {
this.remove(keys[i]);
}
},
/**
* Returns all cookies as a single object
*/
getPack: function() {
pack = {};
keys = this.getKeys();
size = keys.size();
for(i=0; i<size; i++) {
pack[keys[i]] = this.get(keys[i]);
}
return pack;
},
/**
* Returns all keys.
*/
getKeys: function() {
keys = $A();
keyRe= /[^=; ]+(?=\=)/g;
str = document.cookie;
CJRe = new RegExp("^" + this.appendString);
while((match = keyRe.exec(str)) != undefined) {
if (CJRe.test(match[0].strip())) {
keys.push(match[0].strip().gsub("^" + this.appendString,""));
}
}
return keys;
}
};