From 7c30ae7002f203aac1841fefdf535b0d017aabbf Mon Sep 17 00:00:00 2001 From: fuxiaohei Date: Thu, 25 Sep 2014 21:52:58 +0800 Subject: is utils improvement --- public/ng/js/utils/preview.js | 53 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 public/ng/js/utils/preview.js (limited to 'public/ng/js/utils/preview.js') diff --git a/public/ng/js/utils/preview.js b/public/ng/js/utils/preview.js new file mode 100644 index 00000000..87ab39e5 --- /dev/null +++ b/public/ng/js/utils/preview.js @@ -0,0 +1,53 @@ +/** + * preview plugin + * @param selector + * @param target_selector + */ +function Preview(selector, target_selector) { + + // get input element + function get_input($e) { + return $e.find(".js-preview-input").eq(0); + } + + // get result html container element + function get_container($t) { + if ($t.hasClass("js-preview-container")) { + return $t + } + return $t.find(".js-preview-container").eq(0); + } + + var $e = $(selector); + var $t = $(target_selector); + + var $ipt = get_input($t); + if (!$ipt.length) { + console.log("[preview]: no preview input"); + return + } + var $cnt = get_container($t); + if (!$cnt.length) { + console.log("[preview]: no preview container"); + return + } + + + // call api via ajax + $e.on("click", function () { + $.post("/api/v1/markdown", { + text: $ipt.val() + }, function (html) { + $cnt.html(html); + }) + }); + + console.log("[preview]: init preview @", selector, "&", target_selector); +} + + +$.fn.extend({ + markdown_preview: function (target) { + Preview(this, target); + } +}); -- cgit v1.2.3