From f4018bcfb3e1a09ab4b6a7d1e00e75a6295f89bd Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sat, 21 Nov 2009 10:46:13 -0500 Subject: [PATCH] get new bookmark widget working --- functions.php | 1 + js/bookmark.js | 22 +++--- js/cookiejar.js | 157 +++++++++++++++++++++++++++++++++++++ widgets/BookmarkWidget.inc | 15 ++-- 4 files changed, 178 insertions(+), 17 deletions(-) create mode 100644 js/cookiejar.js diff --git a/functions.php b/functions.php index f5019b6..b2f182f 100644 --- a/functions.php +++ b/functions.php @@ -11,6 +11,7 @@ function __comicpress_widgets_init() { if (strpos($file, '.inc') !== false) { $class_name = preg_replace('#\..*$#', '', $file); require_once(dirname(__FILE__) . '/widgets/' . $file); + register_widget($class_name); $widget = new $class_name(); if (method_exists($widget, 'init')) { $widget->init(); } } diff --git a/js/bookmark.js b/js/bookmark.js index e890e3e..eea3119 100644 --- a/js/bookmark.js +++ b/js/bookmark.js @@ -18,37 +18,37 @@ var BookmarkInfo = Class.create({ }); }, 'read': function() { - var bookmark_info = this.jar.get('bookmark-info'); + var bookmark_info = this.jar.get('bookmark-info'); if ((typeof(bookmark_info) != 'object') || (bookmark_info == null)) { - bookmark_info = this.default; + bookmark_info = this.default; } - + return bookmark_info; }, 'write': function(bookmark_info) { - this.jar.put('bookmark-info', bookmark_info); + this.jar.put('bookmark-info', bookmark_info); if (this.onWrite) { this.onWrite(bookmark_info); } } }); -Event.observe(window, 'load', function() { +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) { 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"]; - }); + }); }; - + bookmark_info.onWrite = function(i) { set_goto_tag(i); } set_goto_tag(info); @@ -57,13 +57,13 @@ Event.observe(window, 'load', function() { 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); } }); diff --git a/js/cookiejar.js b/js/cookiejar.js new file mode 100644 index 0000000..a7c4a8d --- /dev/null +++ b/js/cookiejar.js @@ -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 __('Let your readers save their place via a cookie.', 'comicpress'))); +class BookmarkWidget extends WP_Widget { + function BookmarkWidget() { + $widget_ops = array('classname' => __CLASS__, 'description' => __('Allow the user to bookmark a page and then jump to it upon return.','comicpress') ); + $this->WP_Widget('comicpress-bookmark', __('ComicPress Bookmark','comicpress'), $widget_ops); + } + function init() { add_action('wp_head', array(&$this, 'wp_head')); wp_enqueue_script('prototype'); @@ -13,15 +16,15 @@ class BookmarkWidget { function wp_head() { ?>
- +