/*global jQuery, console, Markdown*/ /* * Meltdown (Markup Extra Live Toolbox) * Version: 0.1 (13-FEB-2013) * Requires: jQuery v1.7.2 or later */ (function (jQuery) { 'use strict'; var ver, name, dbg; ver = '0.1'; name = 'meltdown'; dbg = true; function debug(msg) { if ((typeof console !== 'undefined') && (dbg === true)) { console.log(msg); } } function update(previewArea, input) { var mde = Markdown; previewArea.height(input.outerHeight()); previewArea.html(mde(input.val())); } function addEventHandler(thees, example, control) { control.click(function (e) { var text, selection, before, placeholder, after, lineStart, lineEnd, charBefore, charAfter; before = example.before || ""; placeholder = example.placeholder || ""; after = example.after || ""; if (typeof thees.surroundSelectedText !== 'undefined') { text = thees.val(); selection = thees.getSelection(); if (example.lineSelect) { lineStart = text.lastIndexOf('\n', selection.start) + 1; lineEnd = text.indexOf('\n', selection.end); if(lineEnd === -1) { lineEnd = text.length; } thees.setSelection(lineStart, lineEnd); selection = thees.getSelection(); } if(selection.length > 0) { placeholder = selection.text; } if (example.isBlock) { for (var i = 0; i < 2; i++) { charBefore = text.charAt(selection.start - 1 - i); charAfter = text.charAt(selection.end + i); if (charBefore !== "\n" && charBefore !== "") { before = "\n" + before; } if (charAfter !== "\n" && charAfter !== "") { after = after + "\n"; } } } if (selection.text !== placeholder) { thees.replaceSelectedText(placeholder, "select"); } thees.surroundSelectedText(before, after, "select"); } else { debug('Failed to load surroundSelectedText'); thees.val(before + placeholder + after + "\n\n" + thees.val()); } e.preventDefault(); thees.focus(); thees.keyup(); }); } function buildControls(opts, thees, controls) { var controlList, example, control, tuple, t, groupClass, group, outer, tmpThis; controlList = []; for (example in opts.examples) { if (opts.examples.hasOwnProperty(example)) { example = opts.examples[example]; control = jQuery('
  • ' + example.label + '
  • '); control.addClass(name + '_control'); if (typeof example.styleClass !== 'undefined') { control.addClass(example.styleClass); } control.children(":first").attr('title', example.altText); addEventHandler(thees, example, control); tuple = {}; tuple.example = example; tuple.control = control; controlList.push(tuple); } } function addClickHandler(outer) { outer.on('click', function () { var element = jQuery(this); element.siblings('li').removeClass(name + '_controlgroup-open').children('ul').hide(); element.toggleClass(name + '_controlgroup-open').children('ul').toggle(); }); } for (t in controlList) { if (controlList.hasOwnProperty(t)) { t = controlList[t]; if (t.example.group && t.example.groupLabel) { groupClass = name + "_controlgroup-" + t.example.group; group = controls.find("ul." + groupClass); outer = jQuery('
  • '); if (group.length === 0) { group = jQuery('