diff options
author | fuxiaohei <fuxiaohei@vip.qq.com> | 2014-09-25 21:52:58 +0800 |
---|---|---|
committer | fuxiaohei <fuxiaohei@vip.qq.com> | 2014-09-25 21:52:58 +0800 |
commit | 7c30ae7002f203aac1841fefdf535b0d017aabbf (patch) | |
tree | dbbf215903c63e7c4838522ba68b20ed11e61a27 /public/ng/js/utils | |
parent | 089d934547c88a8c3c7ce5587fcc2481cc98f3a3 (diff) |
is utils improvement
Diffstat (limited to 'public/ng/js/utils')
-rw-r--r-- | public/ng/js/utils/preview.js | 53 | ||||
-rw-r--r-- | public/ng/js/utils/tabs.js | 45 |
2 files changed, 98 insertions, 0 deletions
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); + } +}); diff --git a/public/ng/js/utils/tabs.js b/public/ng/js/utils/tabs.js new file mode 100644 index 00000000..2e2731ef --- /dev/null +++ b/public/ng/js/utils/tabs.js @@ -0,0 +1,45 @@ +/* + js tabs and tabbed content plugin + */ +function Tabs(selector) { + + function hide($nav) { + console.log("hide", $nav); + $nav.removeClass("js-tab-nav-show"); + $($nav.data("tab-target")).removeClass("js-tab-show").hide(); + } + + function show($nav) { + console.log("show", $nav); + $nav.addClass("js-tab-nav-show"); + $($nav.data("tab-target")).addClass("js-tab-show").show(); + } + + var $e = $(selector); + if ($e.length) { + // pre-assign init index + var $current = $e.find('.js-tab-nav-show'); + if ($current.length) { + $($current.data("tab-target")).addClass("js-tab-show"); + } + // bind nav click + $e.on("click", ".js-tab-nav", function (e) { + e.preventDefault(); + var $this = $(this); + // is showing, not change. + if ($this.hasClass("js-tab-nav-show")) { + return; + } + $current = $e.find(".js-tab-nav-show").eq(0); + hide($current); + show($this); + }); + console.log("init tabs @", selector) + } +} + +$.fn.extend({ + tabs: function () { + Tabs(this); + } +});
\ No newline at end of file |