editor.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /**
  2. * Created by lifei6671 on 2017/4/29 0029.
  3. */
  4. /**
  5. * 保存排序
  6. * @param node
  7. * @param parent
  8. */
  9. function jstree_save(node, parent) {
  10. var parentNode = window.treeCatalog.get_node(parent.parent);
  11. var nodeData = window.getSiblingSort(parentNode);
  12. if (parent.parent !== parent.old_parent) {
  13. parentNode = window.treeCatalog.get_node(parent.old_parent);
  14. var newNodeData = window.getSiblingSort(parentNode);
  15. if (newNodeData.length > 0) {
  16. nodeData = nodeData.concat(newNodeData);
  17. }
  18. }
  19. var index = layer.load(1, {
  20. shade: [0.1, '#fff'] //0.1透明度的白色背景
  21. });
  22. console.log(JSON.stringify(nodeData));
  23. $.ajax({
  24. url : window.sortURL,
  25. type :"post",
  26. data : JSON.stringify(nodeData),
  27. success : function (res) {
  28. layer.close(index);
  29. if (res.errcode === 0){
  30. layer.msg("保存排序成功");
  31. }else{
  32. layer.msg(res.message);
  33. }
  34. }
  35. })
  36. }
  37. /**
  38. * 创建文档
  39. */
  40. function openCreateCatalogDialog($node) {
  41. var $then = $("#addDocumentModal");
  42. var doc_id = $node ? $node.id : 0;
  43. $then.find("input[name='parent_id']").val(doc_id);
  44. $then.modal("show");
  45. }
  46. /**
  47. * 处理排序
  48. * @param node
  49. * @returns {Array}
  50. */
  51. function getSiblingSort (node) {
  52. var data = [];
  53. for(var key in node.children){
  54. var index = data.length;
  55. data[index] = {
  56. "id" : parseInt(node.children[key]),
  57. "sort" : parseInt(key),
  58. "parent" : Number(node.id) ? Number(node.id) : 0
  59. };
  60. }
  61. return data;
  62. }
  63. /**
  64. * 删除一个文档
  65. * @param $node
  66. */
  67. function openDeleteDocumentDialog($node) {
  68. var index = layer.confirm('你确定要删除该文档吗?', {
  69. btn: ['确定','取消'] //按钮
  70. }, function(){
  71. $.post(window.deleteURL,{"identify" : window.book.identify,"doc_id" : $node.id}).done(function (res) {
  72. layer.close(index);
  73. if(res.errcode === 0){
  74. window.treeCatalog.delete_node($node);
  75. }else{
  76. layer.msg("删除失败",{icon : 2})
  77. }
  78. }).fail(function () {
  79. layer.close(index);
  80. layer.msg("删除失败",{icon : 2})
  81. });
  82. });
  83. }
  84. /**
  85. * 打开文档编辑界面
  86. * @param $node
  87. */
  88. function openEditCatalogDialog($node) {
  89. var $then = $("#addDocumentModal");
  90. var doc_id = parseInt($node ? $node.id : 0);
  91. var text = $node ? $node.text : '';
  92. var parentId = $node && $node.parent !== '#' ? $node.parent : 0;
  93. $then.find("input[name='doc_id']").val(doc_id);
  94. $then.find("input[name='parent_id']").val(parentId);
  95. $then.find("input[name='doc_name']").val(text);
  96. if($node.a_attr && $node.a_attr.is_open){
  97. $then.find("input[name='is_open'][value='1']").prop("checked","checked");
  98. }else{
  99. $then.find("input[name='is_open'][value='0']").prop("checked","checked");
  100. }
  101. for (var index in window.documentCategory){
  102. var item = window.documentCategory[index];
  103. if(item.id === doc_id){
  104. $then.find("input[name='doc_identify']").val(item.identify);
  105. break;
  106. }
  107. }
  108. $then.modal({ show : true });
  109. }
  110. /**
  111. * 将一个节点推送到现有数组中
  112. * @param $node
  113. */
  114. function pushDocumentCategory($node) {
  115. for (var index in window.documentCategory){
  116. var item = window.documentCategory[index];
  117. if(item.id === $node.id){
  118. window.documentCategory[index] = $node;
  119. return;
  120. }
  121. }
  122. window.documentCategory.push($node);
  123. }
  124. /**
  125. * 将数据重置到Vue列表中
  126. * @param $lists
  127. */
  128. function pushVueLists($lists) {
  129. window.vueApp.lists = [];
  130. for(var j in $lists){
  131. var item = $lists[j];
  132. window.vueApp.lists.push(item);
  133. }
  134. }
  135. /**
  136. * 发布项目
  137. */
  138. function releaseBook() {
  139. $.ajax({
  140. url: window.releaseURL,
  141. data: { "identify": window.book.identify },
  142. type: "post",
  143. dataType: "json",
  144. success: function (res) {
  145. if (res.errcode === 0) {
  146. layer.msg("发布任务已推送到任务队列,稍后将在后台执行。");
  147. } else {
  148. layer.msg(res.message);
  149. }
  150. }
  151. });
  152. }
  153. //实现小提示
  154. $("[data-toggle='tooltip']").hover(function () {
  155. var title = $(this).attr('data-title');
  156. var direction = $(this).attr("data-direction");
  157. var tips = 3;
  158. if(direction === "top"){
  159. tips = 1;
  160. }else if(direction === "right"){
  161. tips = 2;
  162. }else if(direction === "bottom"){
  163. tips = 3;
  164. }else if(direction === "left"){
  165. tips = 4;
  166. }
  167. index = layer.tips(title, this, {
  168. tips: tips
  169. });
  170. }, function () {
  171. layer.close(index);
  172. });
  173. //弹出创建文档的遮罩层
  174. $("#btnAddDocument").on("click",function () {
  175. $("#addDocumentModal").modal("show");
  176. });
  177. //用于还原创建文档的遮罩层
  178. $("#addDocumentModal").on("hidden.bs.modal",function () {
  179. // $(this).find("form").html(window.sessionStorage.getItem("addDocumentModal"));
  180. }).on("shown.bs.modal",function () {
  181. $(this).find("input[name='doc_name']").focus();
  182. }).on("show.bs.modal",function () {
  183. // window.sessionStorage.setItem("addDocumentModal",$(this).find("form").html())
  184. });
  185. function showError($msg,$id) {
  186. if(!$id){
  187. $id = "#form-error-message"
  188. }
  189. $($id).addClass("error-message").removeClass("success-message").text($msg);
  190. return false;
  191. }
  192. function showSuccess($msg,$id) {
  193. if(!$id){
  194. $id = "#form-error-message"
  195. }
  196. $($id).addClass("success-message").removeClass("error-message").text($msg);
  197. return true;
  198. }
  199. window.documentHistory = function() {
  200. layer.open({
  201. type: 2,
  202. title: '历史版本',
  203. shadeClose: true,
  204. shade: 0.8,
  205. area: ['700px','80%'],
  206. content: window.historyURL + "?identify=" + window.book.identify + "&doc_id=" + window.selectNode.id,
  207. end : function () {
  208. if(window.SelectedId){
  209. var selected = {node:{
  210. id : window.SelectedId
  211. }};
  212. window.loadDocument(selected);
  213. window.SelectedId = null;
  214. }
  215. }
  216. });
  217. };
  218. //格式化文件大小
  219. function formatBytes($size) {
  220. var $units = [" B", " KB", " MB", " GB", " TB"];
  221. for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
  222. return $size.toFixed(2) + $units[$i];
  223. }
  224. function uploadImage($id,$callback) {
  225. /** 粘贴上传图片 **/
  226. document.getElementById($id).addEventListener('paste', function(e) {
  227. if(e.clipboardData && e.clipboardData.items) {
  228. var clipboard = e.clipboardData;
  229. for (var i = 0, len = clipboard.items.length; i < len; i++) {
  230. if (clipboard.items[i].kind === 'file' || clipboard.items[i].type.indexOf('image') > -1) {
  231. var imageFile = clipboard.items[i].getAsFile();
  232. var fileName = String((new Date()).valueOf());
  233. switch (imageFile.type) {
  234. case "image/png" :
  235. fileName += ".png";
  236. break;
  237. case "image/jpg" :
  238. fileName += ".jpg";
  239. break;
  240. case "image/jpeg" :
  241. fileName += ".jpeg";
  242. break;
  243. case "image/gif" :
  244. fileName += ".gif";
  245. break;
  246. default :
  247. layer.msg("不支持的图片格式");
  248. return;
  249. }
  250. var form = new FormData();
  251. form.append('editormd-image-file', imageFile, fileName);
  252. var layerIndex = 0;
  253. $.ajax({
  254. url: window.imageUploadURL,
  255. type: "POST",
  256. dataType: "json",
  257. data: form,
  258. processData: false,
  259. contentType: false,
  260. beforeSend: function () {
  261. layerIndex = $callback('before');
  262. },
  263. error: function () {
  264. layer.close(layerIndex);
  265. $callback('error');
  266. layer.msg("图片上传失败");
  267. },
  268. success: function (data) {
  269. layer.close(layerIndex);
  270. $callback('success', data);
  271. if (data.errcode !== 0) {
  272. layer.msg(data.message);
  273. }
  274. }
  275. });
  276. e.preventDefault();
  277. }
  278. }
  279. }
  280. });
  281. }
  282. /**
  283. * 初始化代码高亮
  284. */
  285. function initHighlighting() {
  286. $('pre code,pre.ql-syntax').each(function (i, block) {
  287. hljs.highlightBlock(block);
  288. });
  289. }
  290. $(function () {
  291. window.vueApp = new Vue({
  292. el : "#attachList",
  293. data : {
  294. lists : []
  295. },
  296. delimiters : ['${','}'],
  297. methods : {
  298. removeAttach : function ($attach_id) {
  299. var $this = this;
  300. var item = $this.lists.filter(function ($item) {
  301. return $item.attachment_id == $attach_id;
  302. });
  303. if(item && item[0].hasOwnProperty("state")){
  304. $this.lists = $this.lists.filter(function ($item) {
  305. return $item.attachment_id != $attach_id;
  306. });
  307. return;
  308. }
  309. $.ajax({
  310. url : window.removeAttachURL,
  311. type : "post",
  312. data : { "attach_id" : $attach_id},
  313. success : function (res) {
  314. if(res.errcode === 0){
  315. $this.lists = $this.lists.filter(function ($item) {
  316. return $item.attachment_id != $attach_id;
  317. });
  318. }else{
  319. layer.msg(res.message);
  320. }
  321. }
  322. });
  323. }
  324. },
  325. watch : {
  326. lists : function ($lists) {
  327. $("#attachInfo").text(" " + $lists.length + " 个附件")
  328. }
  329. }
  330. });
  331. });