Skip to content
This repository has been archived by the owner on Apr 8, 2024. It is now read-only.

Commit

Permalink
General cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
KrofDrakula committed Feb 1, 2011
1 parent 29a0bb0 commit 16e22e4
Showing 1 changed file with 56 additions and 79 deletions.
135 changes: 56 additions & 79 deletions jquery.microdata.js
@@ -1,105 +1,80 @@
(function($) {
/*
var log = function() {
if (console && console.debug)
console.debug(arguments);
else if (console && console.log) {
console.log(Array.prototype.slice.call(arguments).join(', '));
}
};
$(function() {
$('[itemscope]').each(
function() {
var $t = $(this), propElements = $t.find('[itemprop]'), props = [];
propElements.each(function() {
var propname = $(this).attr('itemprop').toLowerCase()
.split(' '), $p = $(this);
for ( var i = 0; i < propname.length; i++) {
var v = $p.text();
if (propname[i] == 'url') {
// elements with href
if ($p.is('a,area,link'))
v = $p.attr('href');
// elements with src
else if ($p
.is('audio,embed,iframe,img,source,video'))
v = $p.attr('src');
// elements with data
else if ($p.is('object'))
v = $p.attr('data');
}
if ($p.is('time'))
v = $p.attr('datetime') || $p.text();
props.push({
property : propname[i],
value : v
});
}
});
log('Microdata item', $(this).attr('itemtype'), $.map(props, function(el, idx) {
return el.property + '=' + el.value;
}).join(', '));
});
});
*/

(function($) {
var items = [], widget,

// CSS styles injected into head
// TODO: need to clean this up or externalize it
cssBlock = "#microdata-container { " +
"position: absolute; bottom: 10px; left: 10px; background: white; padding: 5px; color: #444; " +
"-webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius: 5px; " +
"-webkit-box-shadow: 0 0 5px black; -moz-box-shadow: 0 0 5px black; box-shadow: 0 0 5px; " +
"font: normal 11px Droid Sans Mono; letter-spacing: -1px; max-height: 300px; " +
"font: normal 11px Droid Sans Mono, Inconsolata, Consolas, monospace; letter-spacing: -1px; max-height: 300px; " +
"}\n" +
"#microdata-container li { list-style: none; padding: 0; margin: 0; }\n" +
"#microdata-container ul { padding: 0; margin-left: 10px; color: #999 }\n" +
".microdata-highlighted { outline: 5px dashed red !important };";
".microdata-highlighted { outline: 5px dashed red !important; background: yellow !important };";


var refreshItems = function() {
/**
* Updates the list of microdata elements on the page
*/
var refreshList = function() {
items = $('[itemscope]');
};

var getItems = function() {
if(items.length == 0) return [];
};

/**
* Removes all microdata objects from widget
*/
var clearObjects = function() {
widget.children().remove();
};


/**
* Adds a microdata object to the widget and adds a hover
* handler to highlight the relevant element
*/
var addObject = function(element, mdata) {
var t = $('<li>' + $(element).attr('itemtype') + '</li>').appendTo(widget), u = $('<ul/>').appendTo(t);
var t = $('<li>' + $(element).attr('itemtype') + '</li>').appendTo(widget),
u = $('<ul/>').appendTo(t);

for(var i = 0; i < mdata.length; i++) {
u.append('<li>' + mdata[i].property + ' = ' + mdata[i].value + '</li>');
}
t.hover(function() { $(element).addClass('microdata-highlighted'); }, function() { $(element).removeClass('microdata-highlighted'); });
t.hover(
function() { $(element).addClass('microdata-highlighted'); },
function() { $(element).removeClass('microdata-highlighted'); }
);
};


/**
* Updates the list of microdata objects in the widget
*/
var updateList = function() {

clearObjects();
refreshList();

items.each(function() {
var $t = $(this), propElements = $t.find('[itemprop]'), props = [];
propElements.each(function() {
var propname = $(this).attr('itemprop').toLowerCase()
.split(' '), $p = $(this);
for ( var i = 0; i < propname.length; i++) {
var propname = $(this).attr('itemprop').toLowerCase().split(' '), $p = $(this);

for (var i = 0; i < propname.length; i++) {
var v = $p.text();

// special cases for 'url' itemprop; takes attribute values
// for certain tags
if (propname[i] == 'url') {
// elements with href
if ($p.is('a,area,link'))
v = $p.attr('href');
// elements with src
else if ($p
.is('audio,embed,iframe,img,source,video'))
else if ($p.is('audio,embed,iframe,img,source,video'))
v = $p.attr('src');
// elements with data
else if ($p.is('object'))
v = $p.attr('data');
}
if ($p.is('time'))
v = $p.attr('datetime') || $p.text();

if ($p.is('time')) v = $p.attr('datetime') || $p.text();

props.push({
property : propname[i],
value : v
Expand All @@ -108,26 +83,28 @@
});

addObject(this, props);
/*
log('Microdata item', $(this).attr('itemtype'), $.map(props, function(el, idx) {
return el.property + '=' + el.value;
}).join(', '));
*/
});
};

$.extend({
microdata: {
getItems: getItems,
updateList: updateList
}
});

$(function() {
refreshItems();
// this function fires on DOMready
var init = function() {
$('<style/>').html(cssBlock).attr('type', 'text/css').appendTo('head');
widget = $('<ul id="microdata-container"/>').appendTo('body');

updateList();
};


// expose functions for use by outside scripts via plugin
$.extend({
microdata: {
getItems: function() { return items; },
updateList: updateList
}
});


// init the drop-in script
$(function() { init(); });
})(jQuery);

0 comments on commit 16e22e4

Please sign in to comment.