editor.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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.split('<span')[0] : '';
  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. for (var index in window.documentCategory){
  97. var item = window.documentCategory[index];
  98. if(item.id === doc_id){
  99. $then.find("input[name='doc_identify']").val(item.identify);
  100. break;
  101. }
  102. }
  103. $then.modal({ show : true });
  104. }
  105. /**
  106. * 将一个节点推送到现有数组中
  107. * @param $node
  108. */
  109. function pushDocumentCategory($node) {
  110. for (var index in window.documentCategory){
  111. var item = window.documentCategory[index];
  112. if(item.id === $node.id){
  113. window.documentCategory[index] = $node;
  114. return;
  115. }
  116. }
  117. window.documentCategory.push($node);
  118. }
  119. /**
  120. * 将数据重置到Vue列表中
  121. * @param $lists
  122. */
  123. function pushVueLists($lists) {
  124. window.vueApp.lists = [];
  125. for(var j in $lists){
  126. var item = $lists[j];
  127. window.vueApp.lists.push(item);
  128. }
  129. }
  130. /**
  131. * 锁定文档
  132. * @param $node
  133. */
  134. function lockDocumentAction($node) {
  135. var index = layer.load(1, {
  136. shade: [0.1, '#fff'] // 0.1 透明度的白色背景
  137. });
  138. $.post(window.lockURL,{"identify" : window.book.identify,"doc_id" : $node.id}).done(function (res) {
  139. layer.close(index);
  140. if(res.errcode === 0){
  141. var node = {"id":$node.id};
  142. var name = res.data.doc_name + "<span class='lock-text'> [锁定]</span>";
  143. window.treeCatalog.rename_node(node, name);
  144. window.treeCatalog.set_type(node,"lock");
  145. }else{
  146. layer.msg(res.message,{icon : 2})
  147. }
  148. }).fail(function () {
  149. layer.close(index);
  150. layer.msg("锁定失败",{icon : 2})
  151. });
  152. }
  153. /**
  154. * 解锁文档
  155. * @param $node
  156. */
  157. function unLockDocumentAction($node) {
  158. var index = layer.load(1, {
  159. shade: [0.1, '#fff'] // 0.1 透明度的白色背景
  160. });
  161. $.post(window.unLockURL,{"identify" : window.book.identify,"doc_id" : $node.id}).done(function (res) {
  162. layer.close(index);
  163. if(res.errcode === 0){
  164. var node = {"id":$node.id};
  165. var name = res.data.doc_name;
  166. window.treeCatalog.rename_node(node,name);
  167. window.treeCatalog.set_type(node,"unlock");
  168. }else{
  169. layer.msg("解锁失败",{icon : 2})
  170. }
  171. }).fail(function () {
  172. layer.close(index);
  173. layer.msg("解锁失败",{icon : 2})
  174. });
  175. }
  176. /**
  177. * 发布项目
  178. */
  179. function releaseBook() {
  180. $.ajax({
  181. url: window.releaseURL,
  182. data: { "identify": window.book.identify },
  183. type: "post",
  184. dataType: "json",
  185. success: function (res) {
  186. if (res.errcode === 0) {
  187. layer.msg("发布任务已推送到任务队列,稍后将在后台执行。");
  188. } else {
  189. layer.msg(res.message);
  190. }
  191. }
  192. });
  193. }
  194. //实现小提示
  195. $("[data-toggle='tooltip']").hover(function () {
  196. var title = $(this).attr('data-title');
  197. var direction = $(this).attr("data-direction");
  198. var tips = 3;
  199. if(direction === "top"){
  200. tips = 1;
  201. }else if(direction === "right"){
  202. tips = 2;
  203. }else if(direction === "bottom"){
  204. tips = 3;
  205. }else if(direction === "left"){
  206. tips = 4;
  207. }
  208. index = layer.tips(title, this, {
  209. tips: tips
  210. });
  211. }, function () {
  212. layer.close(index);
  213. });
  214. //弹出创建文档的遮罩层
  215. $("#btnAddDocument").on("click",function () {
  216. $("#addDocumentModal").modal("show");
  217. });
  218. //用于还原创建文档的遮罩层
  219. $("#addDocumentModal").on("hidden.bs.modal",function () {
  220. $(this).find("form").html(window.addDocumentModalFormHtml);
  221. }).on("shown.bs.modal",function () {
  222. $(this).find("input[name='doc_name']").focus();
  223. });
  224. function showError($msg,$id) {
  225. if(!$id){
  226. $id = "#form-error-message"
  227. }
  228. $($id).addClass("error-message").removeClass("success-message").text($msg);
  229. return false;
  230. }
  231. function showSuccess($msg,$id) {
  232. if(!$id){
  233. $id = "#form-error-message"
  234. }
  235. $($id).addClass("success-message").removeClass("error-message").text($msg);
  236. return true;
  237. }
  238. window.documentHistory = function() {
  239. layer.open({
  240. type: 2,
  241. title: '历史版本',
  242. shadeClose: true,
  243. shade: 0.8,
  244. area: ['700px','80%'],
  245. content: window.historyURL + "?identify=" + window.book.identify + "&doc_id=" + window.selectNode.id,
  246. end : function () {
  247. if(window.SelectedId){
  248. var selected = {node:{
  249. id : window.SelectedId
  250. }};
  251. window.loadDocument(selected);
  252. window.SelectedId = null;
  253. }
  254. }
  255. });
  256. };
  257. //格式化文件大小
  258. function formatBytes($size) {
  259. var $units = [" B", " KB", " MB", " GB", " TB"];
  260. for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
  261. return $size.toFixed(2) + $units[$i];
  262. }
  263. function uploadImage($id,$callback) {
  264. /** 粘贴上传图片 **/
  265. document.getElementById($id).addEventListener('paste', function(e) {
  266. var clipboard = e.clipboardData;
  267. for (var i = 0, len = clipboard.items.length; i < len; i++) {
  268. if (clipboard.items[i].kind === 'file' || clipboard.items[i].type.indexOf('image') > -1) {
  269. var imageFile = clipboard.items[i].getAsFile();
  270. var fileName = Date.parse(new Date());
  271. switch (imageFile.type){
  272. case "image/png" : fileName += ".png";break;
  273. case "image/jpg" : fileName += ".jpg";break
  274. case "image/jpeg" : fileName += ".jpeg";break;
  275. case "image/gif" : fileName += ".gif";break;
  276. default : layer.msg("不支持的图片格式");return;
  277. }
  278. var form = new FormData();
  279. form.append('editormd-image-file', imageFile, fileName);
  280. var layerIndex = 0;
  281. $.ajax({
  282. url: window.imageUploadURL,
  283. type: "POST",
  284. dataType: "json",
  285. data: form,
  286. processData: false,
  287. contentType: false,
  288. beforeSend: function() {
  289. layerIndex = $callback('before');
  290. },
  291. error: function() {
  292. layer.close(layerIndex);
  293. $callback('error');
  294. layer.msg("图片上传失败");
  295. },
  296. success: function(data) {
  297. layer.close(layerIndex);
  298. $callback('success', data);
  299. if(data.errcode !== 0){
  300. layer.msg(data.message);
  301. }
  302. }
  303. });
  304. e.preventDefault();
  305. }
  306. }
  307. });
  308. }
  309. /**
  310. * 初始化代码高亮
  311. */
  312. function initHighlighting() {
  313. $('pre code,pre.ql-syntax').each(function (i, block) {
  314. hljs.highlightBlock(block);
  315. });
  316. hljs.initLineNumbersOnLoad();
  317. }
  318. $(function () {
  319. window.vueApp = new Vue({
  320. el : "#attachList",
  321. data : {
  322. lists : []
  323. },
  324. delimiters : ['${','}'],
  325. methods : {
  326. removeAttach : function ($attach_id) {
  327. var $this = this;
  328. var item = $this.lists.filter(function ($item) {
  329. return $item.attachment_id == $attach_id;
  330. });
  331. if(item && item[0].hasOwnProperty("state")){
  332. $this.lists = $this.lists.filter(function ($item) {
  333. return $item.attachment_id != $attach_id;
  334. });
  335. return;
  336. }
  337. $.ajax({
  338. url : window.removeAttachURL,
  339. type : "post",
  340. data : { "attach_id" : $attach_id},
  341. success : function (res) {
  342. console.log(res);
  343. if(res.errcode === 0){
  344. $this.lists = $this.lists.filter(function ($item) {
  345. return $item.attachment_id != $attach_id;
  346. });
  347. }else{
  348. layer.msg(res.message);
  349. }
  350. }
  351. });
  352. }
  353. },
  354. watch : {
  355. lists : function ($lists) {
  356. $("#attachInfo").text(" " + $lists.length + " 个附件")
  357. }
  358. }
  359. });
  360. });