1
0

editor.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. 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. */
  133. function releaseBook() {
  134. $.ajax({
  135. url: window.releaseURL,
  136. data: { "identify": window.book.identify },
  137. type: "post",
  138. dataType: "json",
  139. success: function (res) {
  140. if (res.errcode === 0) {
  141. layer.msg("发布任务已推送到任务队列,稍后将在后台执行。");
  142. } else {
  143. layer.msg(res.message);
  144. }
  145. }
  146. });
  147. }
  148. //实现小提示
  149. $("[data-toggle='tooltip']").hover(function () {
  150. var title = $(this).attr('data-title');
  151. var direction = $(this).attr("data-direction");
  152. var tips = 3;
  153. if(direction === "top"){
  154. tips = 1;
  155. }else if(direction === "right"){
  156. tips = 2;
  157. }else if(direction === "bottom"){
  158. tips = 3;
  159. }else if(direction === "left"){
  160. tips = 4;
  161. }
  162. index = layer.tips(title, this, {
  163. tips: tips
  164. });
  165. }, function () {
  166. layer.close(index);
  167. });
  168. //弹出创建文档的遮罩层
  169. $("#btnAddDocument").on("click",function () {
  170. $("#addDocumentModal").modal("show");
  171. });
  172. //用于还原创建文档的遮罩层
  173. $("#addDocumentModal").on("hidden.bs.modal",function () {
  174. $(this).find("form").html(window.addDocumentModalFormHtml);
  175. }).on("shown.bs.modal",function () {
  176. $(this).find("input[name='doc_name']").focus();
  177. });
  178. function showError($msg,$id) {
  179. if(!$id){
  180. $id = "#form-error-message"
  181. }
  182. $($id).addClass("error-message").removeClass("success-message").text($msg);
  183. return false;
  184. }
  185. function showSuccess($msg,$id) {
  186. if(!$id){
  187. $id = "#form-error-message"
  188. }
  189. $($id).addClass("success-message").removeClass("error-message").text($msg);
  190. return true;
  191. }
  192. window.documentHistory = function() {
  193. layer.open({
  194. type: 2,
  195. title: '历史版本',
  196. shadeClose: true,
  197. shade: 0.8,
  198. area: ['700px','80%'],
  199. content: window.historyURL + "?identify=" + window.book.identify + "&doc_id=" + window.selectNode.id,
  200. end : function () {
  201. if(window.SelectedId){
  202. var selected = {node:{
  203. id : window.SelectedId
  204. }};
  205. window.loadDocument(selected);
  206. window.SelectedId = null;
  207. }
  208. }
  209. });
  210. };
  211. //格式化文件大小
  212. function formatBytes($size) {
  213. var $units = [" B", " KB", " MB", " GB", " TB"];
  214. for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
  215. return $size.toFixed(2) + $units[$i];
  216. }
  217. function uploadImage($id,$callback) {
  218. /** 粘贴上传图片 **/
  219. document.getElementById($id).addEventListener('paste', function(e) {
  220. var clipboard = e.clipboardData;
  221. for (var i = 0, len = clipboard.items.length; i < len; i++) {
  222. if (clipboard.items[i].kind === 'file' || clipboard.items[i].type.indexOf('image') > -1) {
  223. var imageFile = clipboard.items[i].getAsFile();
  224. var fileName = Date.parse(new Date());
  225. switch (imageFile.type){
  226. case "image/png" : fileName += ".png";break;
  227. case "image/jpg" : fileName += ".jpg";break
  228. case "image/jpeg" : fileName += ".jpeg";break;
  229. case "image/gif" : fileName += ".gif";break;
  230. default : layer.msg("不支持的图片格式");return;
  231. }
  232. var form = new FormData();
  233. form.append('editormd-image-file', imageFile, fileName);
  234. var layerIndex = 0;
  235. $.ajax({
  236. url: window.imageUploadURL,
  237. type: "POST",
  238. dataType: "json",
  239. data: form,
  240. processData: false,
  241. contentType: false,
  242. beforeSend: function() {
  243. layerIndex = $callback('before');
  244. },
  245. error: function() {
  246. layer.close(layerIndex);
  247. $callback('error');
  248. layer.msg("图片上传失败");
  249. },
  250. success: function(data) {
  251. layer.close(layerIndex);
  252. $callback('success', data);
  253. if(data.errcode !== 0){
  254. layer.msg(data.message);
  255. }
  256. }
  257. });
  258. e.preventDefault();
  259. }
  260. }
  261. });
  262. }
  263. $(function () {
  264. window.vueApp = new Vue({
  265. el : "#attachList",
  266. data : {
  267. lists : []
  268. },
  269. delimiters : ['${','}'],
  270. methods : {
  271. removeAttach : function ($attach_id) {
  272. var $this = this;
  273. var item = $this.lists.filter(function ($item) {
  274. return $item.attachment_id == $attach_id;
  275. });
  276. if(item && item[0].hasOwnProperty("state")){
  277. $this.lists = $this.lists.filter(function ($item) {
  278. return $item.attachment_id != $attach_id;
  279. });
  280. return;
  281. }
  282. $.ajax({
  283. url : window.removeAttachURL,
  284. type : "post",
  285. data : { "attach_id" : $attach_id},
  286. success : function (res) {
  287. console.log(res);
  288. if(res.errcode === 0){
  289. $this.lists = $this.lists.filter(function ($item) {
  290. return $item.attachment_id != $attach_id;
  291. });
  292. }else{
  293. layer.msg(res.message);
  294. }
  295. }
  296. });
  297. }
  298. },
  299. watch : {
  300. lists : function ($lists) {
  301. $("#attachInfo").text(" " + $lists.length + " 个附件")
  302. }
  303. }
  304. });
  305. });