editor.js 9.1 KB

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