(function($){ $.fn.htm = {}; /* --------------------- HTM UTILITY ------------------------ */ /** * creates a table row */ $.fn.htm.row = function(opts){ opts = $.extend({ 'cells': [], 'id': '', 'class': '', 'row_class': '' }, opts); var txt = opts.row_class ? $.fn.htm.tag(opts.row_class) : ''; txt += $.fn.htm.tag('tr', opts); var cells = jQuery.map(opts.cells, this._cell_map); txt += cells.join("\n"); txt += ''; txt += opts.row_class ? $.fn.htm.tag(opts.row_class, { end: 1 }) : ''; return txt; } $.fn.htm._cell_map = function(c, i){ // presumes an external txt in scope recieving data var tt = c.type ? c.type : 'td'; return $.fn.htm.tag(tt, c) + c.body + $.fn.htm.tag(tt, { end: true }) } $.fn.htm._prop = function(n_v){ return n_v.name + '="' + n_v.value + '"'; } /** * generic gateway for an HTML tag. * note -- difference between end and close. * end represents the end tag -- close represents a self-closing tag * with no body. This method always returns a single tag -- everything * between '<' and '>'. composing tags with body and end tags * is the responsiility of the caller. * */ $.fn.htm.tag = function(name, opts){ if (!opts) opts = {}; if (opts.end){ return ''; } var out = '<' + name + ' '; if (opts['class']) out += ' class="' + opts['class'] + '" '; if (opts.id) out += ' id="' + opts.id + '" '; if (opts.props){ out += jQuery.map(opts.props, $.fn.htm._prop).join(' '); } if (opts.close) out += '/'; out += '>'; return out; }; /** * this is a utility for generating div elements. Handles tag, body and close. * */ $.fn.htm.div = function(opts){ opts = $.extend({ 'class': '', 'id': '', 'body': ' ' }, opts); return $.fn.htm.tag('div', opts) + opts.body + ''; } $.fn.htm.link = function (opts){ opts = $.extend({ 'class': '', 'id': '', 'body': ' ' }, opts); if (opts.props){ opts.props.push({name: 'href', value: opts.href}); } else { opts.props = [{name: 'href', value: opts.href}]; }; return $.fn.htm.tag('a', opts) + opts.body + ''; } })(jQuery);