learnku.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /**
  2. * First we will load all of this project's JavaScript dependencies which
  3. * includes Vue and other libraries. It is a great starting point when
  4. * building robust, powerful web applications using Vue and Laravel.
  5. */
  6. require('./bootstrap_learnku');
  7. require('./vendor/search');
  8. /** 返回对应位置 */
  9. window.scrollToAnchor = function(id) {
  10. var e = $("*[id='" + id + "']");
  11. $("html,body").animate({
  12. scrollTop: e.offset().top
  13. }, "slow")
  14. };
  15. // 七牛 images 镜像
  16. window.assert_images = function (path) {
  17. return Config.routes.images_domain + path;
  18. };
  19. // 全局公用函数
  20. window.public = {
  21. axios_then: function (res) {
  22. Swal.fire({
  23. type: 'success',
  24. title: res.msg,
  25. });
  26. },
  27. axios_catch: function(error) {
  28. let response = error.response,
  29. html = '';
  30. if (response.data && response.data.errors) {
  31. for (let error in response.data.errors) {
  32. html += '<p style="text-align: left;">' + response.data.errors[error] + '</p>';
  33. }
  34. Swal.fire({
  35. type: 'error',
  36. html: html,
  37. showConfirmButton: false,
  38. showCancelButton: true
  39. });
  40. } else if(response.data && response.data.message){
  41. Swal.fire({
  42. type: 'error',
  43. text: response.data.message,
  44. showConfirmButton: false,
  45. showCancelButton: true
  46. });
  47. }
  48. },
  49. };
  50. class Learnku {
  51. init() {
  52. this.initFunc();
  53. this.initSubmitBtn();
  54. this.closeMessage();
  55. this.initSematicUI();
  56. this.initLazyload();
  57. this.initDeleteForm();
  58. this.initLightBox();
  59. // this.axiosDeleteForm();
  60. }
  61. initFunc() {
  62. // markdown 编辑的页面。非本站地址,以新窗口打开
  63. $('.markdown-body').find('a').each(function () {
  64. let href = $(this).attr('href');
  65. if (href.indexOf('learnku.net') === -1) {
  66. $(this).attr('target', '_black')
  67. }
  68. });
  69. }
  70. /** 表单提交 */
  71. initSubmitBtn(){
  72. $('button.ui.button[type="submit"]:not(.no-loading)').click(function() {
  73. $(this).addClass("disabled");
  74. if($(".clear-submit").length > 0) {
  75. var self = $(this);
  76. setTimeout(function() {
  77. self.after('<a class="clear-submit ml-3 ts-small" href="javascript:;">清除加载状态</a>')
  78. }, 3000)
  79. }
  80. });
  81. $("body").on("click", "a.clear-submit", function() {
  82. $(this).siblings('button.ui.button[type="submit"]').removeClass("loading"),
  83. $(this).siblings('button.ui.button[type="submit"]').removeClass("disabled"),
  84. $(this).remove()
  85. })
  86. }
  87. /** 关闭 message 提示消息 */
  88. closeMessage() {
  89. $('.message .close').on('click', function () {
  90. $(this).closest('.message').transition('fade');
  91. });
  92. }
  93. /** SematicUI */
  94. initSematicUI(){
  95. $(".ui.dropdown").dropdown();
  96. $(".ui.community-nav.dropdown").dropdown({
  97. onChange: function(t, e, n) {
  98. window.location.href = n.attr("href")
  99. }
  100. });
  101. $(".ui.checkbox").checkbox();
  102. $(".ui.accordion").accordion();
  103. $(".ui.popover").popup({
  104. on: "hover",
  105. position: "top center"
  106. });
  107. // 文章详情页 贴附效果
  108. if ($('.js-computed-height-right ').outerHeight(true) >= $(".js-computed-height-left").outerHeight(true)) {
  109. $(".ui.toc.sticky").sticky({
  110. silent: true,
  111. debug: false,
  112. context: ".article-content"
  113. });
  114. }
  115. // 文章详情页 右边栏 贴附效果
  116. $(".ui.topic-operation.sticky").sticky({
  117. silent: true,
  118. context: ".main-column"
  119. });
  120. $(".ui.translate-box.sticky").sticky({
  121. silent: !0
  122. });
  123. $(".message .close").on("click", function() {
  124. $(".message-container").transition("fade")
  125. });
  126. $(".ui.left.sidebar").sidebar({
  127. transition: "overlay",
  128. mobileTransition: "uncover",
  129. observeChanges: !0,
  130. closable: !1,
  131. dimPage: !1,
  132. onVisible: function() {
  133. var e = 200 / screen.width * 100
  134. , n = 100 - e + "%";
  135. $(".pusher").width(n),
  136. $(".pusher").animate({
  137. marginLeft: e + "%"
  138. }, 400)
  139. },
  140. onShow: function() {
  141. $(".fixed.launch.button").css("left", 200),
  142. $(".fixed.launch.button").css("padding", "10px 6px"),
  143. $(".ui.toc.sticky").sticky("refresh")
  144. },
  145. onHide: function() {
  146. $(".pusher").width("100%"),
  147. $(".pusher").css("margin-left", "inherit"),
  148. $(".ui.toc.sticky").each(function() {
  149. $(this).data("moduleSticky").refresh()
  150. }),
  151. $(".fixed.launch.button").css("padding", "0.78571429em 1.5em 0.78571429em"),
  152. $(".fixed.launch.button").css("left", 0)
  153. },
  154. onHidden: function() {
  155. $(".ui.toc.sticky").sticky("refresh")
  156. }
  157. }).sidebar("attach events", ".launch.button, .chapter-title, .launch.item");
  158. $(".ui.top.sidebar").sidebar({
  159. transition: "overlay",
  160. mobileTransition: "uncover"
  161. }).sidebar("attach events", ".item.header.right.menu");
  162. $(".ui.progress").progress();
  163. // 选项页
  164. $('.menu .item').tab()
  165. }
  166. /** 懒加载*/
  167. initLazyload(){
  168. // <img class="lazy" data-original="img/corvette_pitstop.jpg">
  169. $("img.lazy").lazyload({
  170. // placeholder
  171. });
  172. }
  173. /** 删除 form 表单 */
  174. initDeleteForm(){
  175. $("[data-method]").append(function() {
  176. return `
  177. <form action="${$(this).attr("data-url")}" method='POST' style='display:none'>
  178. <input type='hidden' name='_method' value="${$(this).attr("data-method").toUpperCase()}">
  179. <input type='hidden' name='_token' value="${Config.token}">
  180. </form>
  181. `;
  182. }).css("cursor", "pointer").click(function () {
  183. var self = this;
  184. var e = $(this);
  185. var method = e.attr("data-method").toUpperCase();
  186. if("DELETE" == method){
  187. Swal.fire({
  188. title: "",
  189. html: $(self).attr("data-hint") ? $(self).attr("data-hint") : "你确定要删除此内容吗?",
  190. type: "warning",
  191. showCancelButton: !0,
  192. cancelButtonText: "取消",
  193. confirmButtonText: "删除"
  194. }).then(function(res) {
  195. if(res.value){
  196. e.find("form").submit()
  197. }
  198. })
  199. }else if("POST" != method && "GET" != method || (e.attr("data-prompt"))){
  200. Swal.fire({
  201. title: "",
  202. html: $(self).attr("data-prompt"),
  203. type: "warning",
  204. showCancelButton: !0,
  205. cancelButtonText: "取消",
  206. confirmButtonText: "确定"
  207. }).then(function(res) {
  208. if(res.value){
  209. e.find("form").submit()
  210. }
  211. })
  212. }
  213. });
  214. }
  215. /** 删除 form 表单 2*/
  216. axiosDeleteForm(callback = null) {
  217. $("[axios-method]").css("cursor", "pointer").click(function () {
  218. let self = this;
  219. let method = $(this).attr("axios-method").toUpperCase();
  220. if("DELETE" == method){
  221. Swal.fire({
  222. title: "",
  223. html: $(self).attr("data-hint") ? $(self).attr("data-hint") : "你确定要删除此内容吗?",
  224. type: "warning",
  225. showCancelButton: !0,
  226. cancelButtonText: "取消",
  227. confirmButtonText: "删除"
  228. }).then(function(res) {
  229. axios({
  230. url: $(self).attr('data-url'),
  231. method: method
  232. }).then((res)=> {
  233. if (callback) {
  234. callback(self, res);
  235. }
  236. Swal.fire({
  237. type: 'success',
  238. title: '删除成功 . . .',
  239. });
  240. })
  241. })
  242. }else if("POST" != method && "GET" != method || (e.attr("data-prompt"))){
  243. /*Swal.fire({
  244. title: "",
  245. html: $(self).attr("data-prompt"),
  246. type: "warning",
  247. showCancelButton: !0,
  248. cancelButtonText: "取消",
  249. confirmButtonText: "确定"
  250. }).then(function(res) {
  251. if(res.value){
  252. e.find("form").submit()
  253. }
  254. })*/
  255. }
  256. })
  257. }
  258. /** 图库灯箱 */
  259. initLightBox(){
  260. $(".fluidbox-content img:not(.emoji)").each(function() {
  261. $(this).wrap("<a href='" + $(this).attr("src") + "' class='fluidbox'></a>")
  262. }).promise().done(function() {
  263. $("a.fluidbox").fluidbox()
  264. })
  265. }
  266. }
  267. /** 代码高亮工具 */
  268. class MarkdownBody {
  269. constructor(){
  270. this.bodyClass = '.markdown-body';
  271. this.codeClass = '.markdown-body pre';
  272. this.addLineNumber();
  273. this.addThreePoints();
  274. this.addTitleNav();
  275. }
  276. /** 增加行号 */
  277. addLineNumber(){
  278. $(this.codeClass).addClass("line-numbers").css("white-space", "pre-wrap");
  279. }
  280. /** 增加 三个点 漂亮样式 */
  281. addThreePoints(){
  282. var html = '<div class="window-controls"><i class="red"></i><i class="yellow"></i><i class="green"></i></div>';
  283. $(this.codeClass).prepend(html);
  284. }
  285. /** 增加标题导航 tocify */
  286. addTitleNav(){
  287. var self = this;
  288. var selectors = ['h1', 'h2', 'h3', 'h4'];
  289. var selectorsTmp = [];
  290. selectors.forEach((item)=> {
  291. if ($(self.bodyClass).find(item).length > 0) {
  292. selectorsTmp.push(item);
  293. }
  294. });
  295. if (selectorsTmp.length > 0) {
  296. selectorsTmp = selectorsTmp.join(', ');
  297. } else {
  298. selectorsTmp = '';
  299. }
  300. /** #markdown-tocify */
  301. $("#markdown-tocify").tocify({
  302. theme: '',
  303. context: self.bodyClass, // 任意可用的jQuery选择器
  304. selectors: selectorsTmp, // 文章节点,可以关联生成目录
  305. showAndHide: false, // 是否展示二级目录结构
  306. showEffect:'show', // 目录展示效果:"none", "fadeIn", "show", or "slideDown"
  307. hashGenerator: function (a,b) {
  308. // 返回随机数
  309. return Math.random().toString(36).substr(2,10);
  310. }
  311. });
  312. }
  313. }
  314. /** 代码调用 ******************************************************/
  315. // 代码高亮工具
  316. new MarkdownBody();
  317. // 基础代码
  318. window.LearnkuNew = new Learnku();
  319. LearnkuNew.init();