(function($) { // Hide scope, no $ conflict var PROP_NAME = 'bookmark'; /* Bookmark sharing manager. */ function Bookmark() { this._defaults = { url: '', // The URL to bookmark, leave blank for the current page title: '', // The title to bookmark, leave blank for the current one sites: [], // List of site IDs to use, empty for all icons: 'http://www.ojezu.pl/img/skin/bookmarks.png', // Horizontal amalgamation of all site icons iconSize: 16, // The size of the individual icons target: '_blank', // The name of the target window for the bookmarking links compact: true, // True if a compact presentation should be used, false for full hint: 'Send to {s}', // Popup hint for links, {s} is replaced by display name popup: false, // True to have it popup on demand, false to show always popupText: 'Bookmark this site...', // Text for the popup trigger addFavorite: false, // True to add a 'add to favourites' link, false for none favoriteText: 'Favorite', // Display name for the favourites link favoriteIcon: 0, // Icon for the favourites link addEmail: false, // True to add a 'e-mail a friend' link, false for none emailText: 'E-mail', // Display name for the e-mail link emailIcon: 1, // Icon for the e-mail link emailSubject: 'Interesting page', // The subject for the e-mail emailBody: 'I thought you might find this page interesting:\n{t} ({u})', // The body of the e-mail // Use '{t}' for the position of the page title, '{u}' for the page URL, and '\n' for new lines manualBookmark: 'Please close this dialog and\npress Ctrl-D to bookmark this page.' // Instructions for manually bookmarking the page }; this._sites = { // The definitions of the available bookmarking sites 'wykop': {display: 'wykop ', icon: 'http://www.ojezu.pl/img/skin/wykop.gif', url: 'http://www.wykop.pl/dodaj?url={u}&title={t}'}, 'flaker': {display: 'flaker ', icon: 'http://www.ojezu.pl/img/skin/flaker.png', url: 'http://flaker.pl/add2flaker.php?title={t}&url={u}'}, 'blip': {display: 'blip ', icon: 'http://www.ojezu.pl/img/skin/blip.png', url: 'http://blip.pl/dashboard?body={t}+{u}'}, 'facebook': {display: 'facebook ', icon: 'http://www.ojezu.pl/img/skin/facebook.gif', url: 'http://www.facebook.com/share.php?u={u}&t={t}'} }; } $.extend(Bookmark.prototype, { /* Class name added to elements to indicate already configured with bookmarking. */ markerClassName: 'hasBookmark', /* Override the default settings for all bookmarking instances. @param settings object - the new settings to use as defaults @return void */ setDefaults: function(settings) { extendRemove(this._defaults, settings || {}); return this; }, /* Add a new bookmarking site to the list. @param id string - the ID of the new site @param display string - the display name for this site @param icon url - the location of an icon for this site (16x16), or number - the index of the icon within the combined image @param url url - the submission URL for this site, with {u} marking where the current page's URL should be inserted, and {t} indicating the title insertion point @return void */ addSite: function(id, display, icon, url) { this._sites[id] = {display: display, icon: icon, url: url}; return this; }, /* Return the list of defined sites. @return object[] - indexed by site id (string), each object contains display (string) - the display name, icon (string) - the location of the icon,, or (number) the icon's index in the combined image url (string) - the submission URL for the site */ getSites: function() { return this._sites; }, /* Attach the bookmarking widget to a div. */ _attachBookmark: function(target, settings) { target = $(target); if (target.hasClass(this.markerClassName)) { return; } target.addClass(this.markerClassName); this._updateBookmark(target, settings); }, /* Reconfigure the settings for a bookmarking div. */ _changeBookmark: function(target, settings) { target = $(target); if (!target.hasClass(this.markerClassName)) { return; } this._updateBookmark(target, settings); }, /* Construct the requested bookmarking links. */ _updateBookmark: function(target, settings) { var oldSettings = $.data(target[0], PROP_NAME) || $.extend({}, this._defaults); settings = extendRemove(oldSettings, settings || {}); $.data(target[0], PROP_NAME, settings); var sites = settings.sites; if (sites.length == 0) { $.each(this._sites, function(id) { sites.push(id); }); } var hint = settings.hint || '{s}'; var html = (settings.popup ? '' + settings.popupText + '
' : '') + '' + (settings.popup ? '
' : ''); target.html(html); if (settings.popup) { $(target).click(function() { var target = $(this); var offset = target.offset(); var extras = $.bookmark._getExtras(target); $('.bookmark_popup', target).css('left', offset.left). css('top', offset.top + target.height() + extras[1]). toggle(); }); } }, /* Retrieve the size of borders and padding for an element. @param elem (jQuery object) the element of interest @return (number[2]) the horizontal and vertical sizes */ _getExtras: function(elem) { var convert = function(value) { return {thin: 1, medium: 3, thick: 5}[value] || value; }; return [parseInt(convert(elem.css('border-left-width')), 10) + parseInt(convert(elem.css('border-right-width')), 10) + parseInt(elem.css('padding-left'), 10) + parseInt(elem.css('padding-right'), 10), parseInt(convert(elem.css('border-top-width')), 10) + parseInt(convert(elem.css('border-bottom-width')), 10) + parseInt(elem.css('padding-top'), 10) + parseInt(elem.css('padding-bottom'), 10)]; }, /* Remove the bookmarking widget from a div. */ _destroyBookmark: function(target) { target = $(target); if (!target.hasClass(this.markerClassName)) { return; } target.removeClass(this.markerClassName).empty(); $.removeData(target[0], PROP_NAME); }, /* Add the current page as a favourite in the browser. @param url (string) the URL to bookmark @param title (string) the title to bookmark */ _addFavourite: function(url, title) { if ($.browser.msie) { window.external.addFavorite(url, title); } else { alert(this._defaults.manualBookmark); } } }); /* jQuery extend now ignores nulls! */ function extendRemove(target, props) { $.extend(target, props); for (var name in props) { if (props[name] == null) { target[name] = null; } } return target; } /* Attach the bookmarking functionality to a jQuery selection. @param command string - the command to run (optional, default 'attach') @param options object - the new settings to use for these bookmarking instances @return jQuery object - for chaining further calls */ $.fn.bookmark = function(options) { var otherArgs = Array.prototype.slice.call(arguments, 1); return this.each(function() { if (typeof options == 'string') { $.bookmark['_' + options + 'Bookmark']. apply($.bookmark, [this].concat(otherArgs)); } else { $.bookmark._attachBookmark(this, options || {}); } }); }; /* Initialise the bookmarking functionality. */ $.bookmark = new Bookmark(); // singleton instance })(jQuery);