editor.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. resetEditor($node);
  76. }else{
  77. layer.msg("删除失败",{icon : 2})
  78. }
  79. }).fail(function () {
  80. layer.close(index);
  81. layer.msg("删除失败",{icon : 2})
  82. });
  83. });
  84. }
  85. /**
  86. * 打开文档编辑界面
  87. * @param $node
  88. */
  89. function openEditCatalogDialog($node) {
  90. var $then = $("#addDocumentModal");
  91. var doc_id = parseInt($node ? $node.id : 0);
  92. var text = $node ? $node.text : '';
  93. var parentId = $node && $node.parent !== '#' ? $node.parent : 0;
  94. $then.find("input[name='doc_id']").val(doc_id);
  95. $then.find("input[name='parent_id']").val(parentId);
  96. $then.find("input[name='doc_name']").val(text);
  97. for (var index in window.documentCategory){
  98. var item = window.documentCategory[index];
  99. if(item.id === doc_id){
  100. $then.find("input[name='doc_identify']").val(item.identify);
  101. break;
  102. }
  103. }
  104. $then.modal({ show : true });
  105. }
  106. /**
  107. * 将一个节点推送到现有数组中
  108. * @param $node
  109. */
  110. function pushDocumentCategory($node) {
  111. for (var index in window.documentCategory){
  112. var item = window.documentCategory[index];
  113. if(item.id === $node.id){
  114. window.documentCategory[index] = $node;
  115. console.log( window.documentCategory[index]);
  116. return;
  117. }
  118. }
  119. window.documentCategory.push($node);
  120. }
  121. /**
  122. * 将数据重置到Vue列表中
  123. * @param $lists
  124. */
  125. function pushVueLists($lists) {
  126. window.vueApp.lists = [];
  127. for(var j in $lists){
  128. var item = $lists[j];
  129. window.vueApp.lists.push(item);
  130. }
  131. }
  132. //实现小提示
  133. $("[data-toggle='tooltip']").hover(function () {
  134. var title = $(this).attr('data-title');
  135. var direction = $(this).attr("data-direction");
  136. var tips = 3;
  137. if(direction === "top"){
  138. tips = 1;
  139. }else if(direction === "right"){
  140. tips = 2;
  141. }else if(direction === "bottom"){
  142. tips = 3;
  143. }else if(direction === "left"){
  144. tips = 4;
  145. }
  146. index = layer.tips(title, this, {
  147. tips: tips
  148. });
  149. }, function () {
  150. layer.close(index);
  151. });
  152. //弹出创建文档的遮罩层
  153. $("#btnAddDocument").on("click",function () {
  154. $("#addDocumentModal").modal("show");
  155. });
  156. //用于还原创建文档的遮罩层
  157. $("#addDocumentModal").on("hidden.bs.modal",function () {
  158. $(this).find("form").html(window.addDocumentModalFormHtml);
  159. }).on("shown.bs.modal",function () {
  160. $(this).find("input[name='doc_name']").focus();
  161. });
  162. function showError($msg,$id) {
  163. if(!$id){
  164. $id = "#form-error-message"
  165. }
  166. $($id).addClass("error-message").removeClass("success-message").text($msg);
  167. return false;
  168. }
  169. function showSuccess($msg,$id) {
  170. if(!$id){
  171. $id = "#form-error-message"
  172. }
  173. $($id).addClass("success-message").removeClass("error-message").text($msg);
  174. return true;
  175. }
  176. window.documentHistory = function() {
  177. layer.open({
  178. type: 2,
  179. title: '历史版本',
  180. shadeClose: true,
  181. shade: 0.8,
  182. area: ['700px','80%'],
  183. content: window.historyURL + "?identify=" + window.book.identify + "&doc_id=" + window.selectNode.id,
  184. end : function () {
  185. if(window.SelectedId){
  186. var selected = {node:{
  187. id : window.SelectedId
  188. }};
  189. window.loadDocument(selected);
  190. window.SelectedId = null;
  191. }
  192. }
  193. });
  194. };
  195. //格式化文件大小
  196. function formatBytes($size) {
  197. var $units = [" B", " KB", " MB", " GB", " TB"];
  198. for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
  199. return $size.toFixed(2) + $units[$i];
  200. }
  201. function uploadImage($id,$callback) {
  202. /** 粘贴上传图片 **/
  203. document.getElementById($id).addEventListener('paste', function(e) {
  204. var clipboard = e.clipboardData;
  205. for (var i = 0, len = clipboard.items.length; i < len; i++) {
  206. if (clipboard.items[i].kind === 'file' || clipboard.items[i].type.indexOf('image') > -1) {
  207. var imageFile = clipboard.items[i].getAsFile();
  208. console.log(imageFile)
  209. var fileName = Date.parse(new Date());
  210. switch (imageFile.type){
  211. case "image/png" : fileName += ".png";break;
  212. case "image/jpg" : fileName += ".jpg";break
  213. case "image/jpeg" : fileName += ".jpeg";break;
  214. case "image/gif" : fileName += ".gif";break;
  215. default : layer.msg("不支持的图片格式");return;
  216. }
  217. var form = new FormData();
  218. form.append('editormd-image-file', imageFile, fileName);
  219. var layerIndex = 0;
  220. $.ajax({
  221. url: window.imageUploadURL,
  222. type: "POST",
  223. dataType: "json",
  224. data: form,
  225. processData: false,
  226. contentType: false,
  227. beforeSend: function() {
  228. layerIndex = $callback('before');
  229. },
  230. error: function() {
  231. layer.close(layerIndex);
  232. $callback('error');
  233. layer.msg("图片上传失败");
  234. },
  235. success: function(data) {
  236. layer.close(layerIndex);
  237. $callback('success', data);
  238. if(data.errcode !== 0){
  239. layer.msg(data.message);
  240. }
  241. }
  242. });
  243. e.preventDefault();
  244. }
  245. }
  246. });
  247. }
  248. $(function () {
  249. window.vueApp = new Vue({
  250. el : "#attachList",
  251. data : {
  252. lists : []
  253. },
  254. delimiters : ['${','}'],
  255. methods : {
  256. removeAttach : function ($attach_id) {
  257. var $this = this;
  258. var item = $this.lists.filter(function ($item) {
  259. return $item.attachment_id == $attach_id;
  260. });
  261. if(item && item[0].hasOwnProperty("state")){
  262. $this.lists = $this.lists.filter(function ($item) {
  263. return $item.attachment_id != $attach_id;
  264. });
  265. return;
  266. }
  267. $.ajax({
  268. url : window.removeAttachURL,
  269. type : "post",
  270. data : { "attach_id" : $attach_id},
  271. success : function (res) {
  272. console.log(res);
  273. if(res.errcode === 0){
  274. $this.lists = $this.lists.filter(function ($item) {
  275. return $item.attachment_id != $attach_id;
  276. });
  277. }else{
  278. layer.msg(res.message);
  279. }
  280. }
  281. });
  282. }
  283. },
  284. watch : {
  285. lists : function ($lists) {
  286. $("#attachInfo").text(" " + $lists.length + " 个附件")
  287. }
  288. }
  289. });
  290. });