From 0ffac972f61fd3613df63fd19d2760f01b05031c Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sun, 24 Jan 2010 22:07:24 -0500 Subject: [PATCH] final bits to make widget work --- classes/BookmarkWidget.inc | 12 +-- comicpress-widgets.php | 65 +++++++++++++++ js/bookmark.js | 99 +++++++++++++++++++++++ js/cookiejar.js | 157 +++++++++++++++++++++++++++++++++++++ 4 files changed, 323 insertions(+), 10 deletions(-) create mode 100644 comicpress-widgets.php create mode 100644 js/bookmark.js create mode 100644 js/cookiejar.js diff --git a/classes/BookmarkWidget.inc b/classes/BookmarkWidget.inc index 78da664..76a4cf4 100644 --- a/classes/BookmarkWidget.inc +++ b/classes/BookmarkWidget.inc @@ -60,23 +60,15 @@ class ComicPressBookmarkWidget extends WP_Widget { function init() {} function is_active() { - add_action('wp_head', array(&$this, 'wp_head')); add_action('template_redirect', array(&$this, 'template_redirect')); } function template_redirect() { wp_enqueue_script('prototype'); - wp_enqueue_script('cookiejar', get_template_directory_uri() . '/js/cookiejar.js', array('prototype')); - wp_enqueue_script('bookmark', get_template_directory_uri() . '/js/bookmark.js', array('prototype', 'cookiejar')); + wp_enqueue_script('cookiejar', plugin_dir_url(dirname(__FILE__)) . 'js/cookiejar.js', array('prototype')); + wp_enqueue_script('bookmark', plugin_dir_url(dirname(__FILE__)) . 'js/bookmark.js', array('prototype', 'cookiejar')); } - function wp_head() { ?> - - init(); + } + + $available_widgets[strtolower($class_name)] = $widget; + } + } + closedir($dh); + } + + foreach (wp_get_sidebars_widgets() as $type => $widgets) { + if ($type != 'wp_inactive_widgets') { + foreach ($widgets as $widget_id) { + foreach ($available_widgets as $key => $widget) { + if (method_exists($widget, 'is_active')) { + if (strpos(strtolower($widget_id), $key) === 0) { + $widget->is_active(); + } + } + } + } + } + } + } +} + +add_action('widgets_init', array('ComicPressWidgets', 'init')); diff --git a/js/bookmark.js b/js/bookmark.js new file mode 100644 index 0000000..135def2 --- /dev/null +++ b/js/bookmark.js @@ -0,0 +1,99 @@ +var BookmarkInfo = Class.create({ + 'def': { + 'permalink': false + }, + 'initialize': function() { + this.jar = new CookieJar({ + 'expires': 60 * 60 * 24 * 31, + 'path': '/' + }); + }, + 'read': function() { + var bookmark_info = this.jar.get('bookmark-info'); + + if ((typeof(bookmark_info) != 'object') || (bookmark_info == null)) { + bookmark_info = this.def; + } + + return bookmark_info; + }, + 'write': function(bookmark_info) { + this.jar.put('bookmark-info', bookmark_info); + if (this.onWrite) { this.onWrite(bookmark_info); } + } +}); + +var ComicBookmark = {}; +ComicBookmark.setup = function(id, mode, url, elements) { + var bookmark_info = new BookmarkInfo(); + var info = bookmark_info.read(); + + if ($(id)) { + var hrefs = {}; + $$('#' + 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].innerHTML = elements[which + '-' + (i.permalink ? "on" : "off")]; + }); + + $H(hrefs).each(function(info) { + info.value[i.permalink ? 'addClassName' : 'removeClassName']('active'); + }); + }; + + hrefs['tag-page'].innerHTML = elements['tag-page']; + + bookmark_info.onWrite = function(i) { set_goto_tag(i); } + set_goto_tag(info); + + hrefs['tag-page'].observe('click', function(e) { + Event.stop(e); + info.permalink = url; + bookmark_info.write(info); + }); + + 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); + }); + + 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")]; + hrefs['bookmark-clicker'][i.permalink ? 'addClassName' : 'removeClassName']('active'); + }; + 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; + } + } +}; diff --git a/js/cookiejar.js b/js/cookiejar.js new file mode 100644 index 0000000..4daab60 --- /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