/** * jQuery Hatenabookmark Entry Plugin * http://code.google.com/p/jquery-hatenabookmark-entry/ * * Copyright (c) 2009 Yusuke Horie * * Released under the MIT License: * http://www.opensource.org/licenses/mit-license.php * * Since : 0.1.0 - 08/13/2009 * Version: 0.2.0 - 09/14/2009 */ (function(jQuery){ var _baseUrl = 'http://b.hatena.ne.jp'; var _endPoint = ['/entry/json/', '/entry/jsonlite/']; /** public methods **/ jQuery.fn.hatenaEntry = function (url, a) { var c, opts = {}; if (jQuery.isFunction(a)) { c = a; } else if (typeof a == 'object') { opts = jQuery.extend({}, jQuery.fn.hatenaEntry.defaults, a); c = jQuery.isFunction(opts.callback) ? opts.callback: _callback; } else if (typeof a == 'undefined') { opts = jQuery.fn.hatenaEntry.defaults; c = _callback; } else { return this; } var endPoint = (opts.useJsonlite) ? _endPoint[1]: _endPoint[0]; return this.each(function(i, e) { jQuery.getJSON(_baseUrl + endPoint + '?url=' + encodeURIComponent(url) + '&callback=?', function (o) { if ($.isFunction(opts.onLoad)) opts.onLoad.call(e, o); c.apply(this, [o, e, opts]); } ); }); }; jQuery.fn.hatenaEntry.defaults = { callback: null, icon: true, username: true, tags: true, comment: true, timestamp: true, useJsonlite: false, headerBgColor: '#2C6EBD', headerColor: '#ffffff', userColor: '#0000DD', tagColor: '#6666cc', timeColor: '#666666', limit: 7, headerLabel: 'ブックマークしているユーザー', onLoad: null }; jQuery.extend({ hatenaEntry: function (url, a) { var c = jQuery.isFunction(a) ? a: _callback; jQuery.getJSON(_baseUrl + '?url=' + encodeURIComponent(url) + '&callback=?', function (o) { var args = [o]; if (typeof a == 'string') args.push($(a).get()); args.push(jQuery.fn.hatenaEntry.defaults); c.apply(this, args); }); } }); /** private method **/ var _callback = function (o, e, opts) { var $this = $(e); if (!o || $this.length == 0) return false; $this.data('_inc', 1); var h = '
' + ' ' + opts.headerLabel + ' (' + o.count + ')' + '
'+ '
' + _build(o, $this, opts) + '
'; $this.html(h); $('a.hbEntryShow').live('click.hbEntryShow', function (event) { event.preventDefault(); var $t = $(this); $t.parent().fadeOut(400, function () { var i = $this.data('_inc'); i++; $this.data('_inc', i); if ($t.hasClass('hbEntryAll')) { $t.die('click.hbEntryShow'); var start = (i*opts.limit) - opts.limit; var end = o.bookmarks.length; $(this).after(_build(o, $this, opts, start, end)).remove(); } else { $(this).after(_build(o, $this, opts)).remove(); } }); }); }; var _build = function (o, $t, opts, s, e) { var h = '', inc = $t.data('_inc'), start = s || (inc*opts.limit) - opts.limit, end = e || ((o.bookmarks.length > start + opts.limit) ? start + opts.limit: o.bookmarks.length); for (var i=start; i'; if (opts.username) h += ' ' + b.user + ''; if (opts.tags) h +=' ' + b.tags.join(' ') + ''; if (opts.comment) h += ' ' + b.comment + ''; if (opts.timestamp) h += ' ' + b.timestamp.split(' ')[0] + ''; h += ''; } if (o.bookmarks.length > end) { h += '
' + '次の' + opts.limit + ' 件を開く  / ' + 'すべて開く' + '
'; } return h; }; })(jQuery);