my_collect.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. var share_win;
  2. function my_collect_init() {
  3. my_collect_list();
  4. share_win = iframe_win_init({ container: "share_win", name: "share", width: "500px" });
  5. collect_add_dlg_init("collect_add_div");
  6. }
  7. function my_collect_list() {
  8. $.get(
  9. "../article/collect_list.php",
  10. {
  11. userid: getCookie("userid"),
  12. setting: "",
  13. },
  14. function (data, status) {
  15. if (status == "success") {
  16. try {
  17. let html = "";
  18. let result = JSON.parse(data);
  19. let key = 1;
  20. for (const iterator of result) {
  21. html += '<div class="file_list_row" style="padding:5px;">';
  22. html += '<div style="max-width:2em;flex:1;"><input type="checkbox" /></div>';
  23. html += "<div style='flex:1;'>" + key++ + "</div>";
  24. html += "<div style='flex:2;'>" ;
  25. html += "<a href='../article/my_collect_edit.php?id=" + iterator.id + "'>" ;
  26. html += iterator.title ;
  27. html += "</a>";
  28. html += "</div>";
  29. html += "<div style='flex:2;'>" + render_status(iterator.status) + "</div>";
  30. //html += "<div style='flex:1;'>" + gLocal.gui.copy_link + "</div>";
  31. html += "<div style='flex:1;'>";
  32. html += "<a href='../article/?collect=" + iterator.id + "' target='_blank'>" + gLocal.gui.preview + "</a>";
  33. html += "</div>";
  34. html += "<div style='flex:1;'>";
  35. html += "<a onclick=\"collection_share('" + iterator.id + "')\">"+gLocal.gui.share+"</a>";
  36. html += "</div>";
  37. html += "</div>";
  38. }
  39. $("#article_list").html(html);
  40. } catch (e) {
  41. console.error(e);
  42. }
  43. } else {
  44. console.error("ajex error");
  45. }
  46. }
  47. );
  48. }
  49. var _arrArticleList;
  50. var _arrArticleOrder = new Array();
  51. function my_collect_edit(id) {
  52. $.get(
  53. "../article/collect_get.php",
  54. {
  55. id: id,
  56. setting: "",
  57. },
  58. function (data, status) {
  59. if (status == "success") {
  60. let html = "";
  61. let result;
  62. try {
  63. result = JSON.parse(data);
  64. } catch (e) {
  65. console.error(e);
  66. }
  67. $("#article_collect").attr("a_id", result.id);
  68. html += '<div class="" style="padding:5px;">';
  69. html += '<div style="max-width:2em;flex:1;"></div>';
  70. html += "<input type='hidden' name='id' value='" + result.id + "'/>";
  71. html += "<div style='display:flex;'>";
  72. html += "<div style='flex:2;'>" + gLocal.gui.title + "</div>";
  73. html += "<div style='flex:8;'>";
  74. html += "<input type='input' name='title' value='" + result.title + "'/>";
  75. html += "</div></div>";
  76. html += "<div style='display:flex;'>";
  77. html += "<div style='flex:2;'>" + gLocal.gui.sub_title + "</div>";
  78. html += "<div style='flex:8;'>";
  79. html += "<input type='input' name='subtitle' value='" + result.subtitle + "'/>";
  80. html += "</div></div>";
  81. html += "<div style='display:flex;'>";
  82. html += "<div style='flex:2;'>" + gLocal.gui.introduction + "</div>";
  83. html += "<div style='flex:8;'>";
  84. html += "<input type='input' name='summary' value='" + result.summary + "'/>";
  85. html += "</div></div>";
  86. html += "<div style='display:flex;'>";
  87. html += "<div style='flex:2;'>" + gLocal.gui.status + "</div>";
  88. html += "<div style='flex:8;'>";
  89. html += render_status(result.status, false);
  90. html += "</div></div>";
  91. html += "<div style='display:flex;'>";
  92. html += "<div style='flex:2;'>" + gLocal.gui.language + "</div>";
  93. html += "<div style='flex:8;'>";
  94. html += "<input type='input' name='lang' value='" + result.lang + "'/>";
  95. html += "</div></div>";
  96. html +=
  97. "<input id='form_article_list' type='hidden' name='article_list' value='" +
  98. result.article_list +
  99. "'/>";
  100. html += "</div>";
  101. html += "<div style='display:flex;'>";
  102. html += "<div style='flex:4;'>";
  103. _arrArticleList = JSON.parse(result.article_list);
  104. let treeData = new Array()
  105. html += "<div id='ul_article_list'>";
  106. let treeParents = [];
  107. let rootNode = {key:0,title:"root",level:0};
  108. treeData.push(rootNode);
  109. let lastInsNode = rootNode;
  110. let iLastParentNodeLevel = 0;
  111. let iLastParentNode = 0;
  112. let iCurrLevel = 0;
  113. for (let index = 0; index < _arrArticleList.length; index++) {
  114. const element = _arrArticleList[index];
  115. let newNode = {key:element.article,title:element.title,level:element.level};
  116. if(newNode.level>iCurrLevel){
  117. treeParents.push(lastInsNode);
  118. lastInsNode.children = new Array();
  119. lastInsNode.children.push(newNode);
  120. }
  121. else if(newNode.level==iCurrLevel){
  122. treeParents[treeParents.length-1].children.push(newNode);
  123. }
  124. else{
  125. // 小于
  126. treeParents.pop();
  127. treeParents[treeParents.length-1].children.push(newNode);
  128. iLastParentNodeLevel = treeParents[treeParents.length-1].level;
  129. }
  130. lastInsNode = newNode;
  131. iCurrLevel = newNode.level;
  132. }
  133. html += "</div>";
  134. html += "</div>";
  135. html += "<div id='preview_div'>";
  136. html += "<div id='preview_inner' ></div>";
  137. html += "</div>";
  138. html += "</div>";
  139. $("#article_list").html(html);
  140. $("#collection_title").html(result.title);
  141. $("#ul_article_list").fancytree({
  142. autoScroll: true,
  143. extensions: ["dnd"],
  144. source: treeData[0].children,
  145. click: function(e, data) {
  146. if( e.ctrlKey ){
  147. window.open("../article/?id="+data.node.key,"_blank");
  148. return false;
  149. }
  150. },
  151. dblclick: function(e, data) {
  152. editNode(data.node);
  153. return false;
  154. },
  155. keydown: function(e, data) {
  156. switch( e.which ) {
  157. case 113: // [F2]
  158. editNode(data.node);
  159. return false;
  160. case 13: // [enter]
  161. if( isMac ){
  162. editNode(data.node);
  163. return false;
  164. }
  165. }
  166. },
  167. dnd: {
  168. preventVoidMoves: true, // Prevent dropping nodes 'before self', etc.
  169. preventRecursiveMoves: true, // Prevent dropping nodes on own descendants
  170. autoExpandMS: 400,
  171. onDragStart: function(node) {
  172. return true;
  173. },
  174. onDragEnter: function(node, sourceNode) {
  175. return true;
  176. },
  177. onDrop: function(node, sourceNode, hitMode, ui, draggable) {
  178. sourceNode.moveTo(node, hitMode);
  179. node.setExpanded(true);
  180. }
  181. },
  182. activate: function(e, data) {
  183. // alert("activate " + data.node);
  184. }
  185. });
  186. } else {
  187. console.error("ajex error");
  188. }
  189. }
  190. );
  191. }
  192. function editNode(node){
  193. var prevTitle = node.title,
  194. tree = node.tree;
  195. // Disable dynatree mouse- and key handling
  196. tree.widget._unbind();
  197. // Replace node with <input>
  198. $(".fancytree-title", node.span).html("<input id='editNode' value='" + prevTitle + "'>");
  199. // Focus <input> and bind keyboard handler
  200. $("input#editNode")
  201. .focus()
  202. .keydown(function(event){
  203. switch( event.which ) {
  204. case 27: // [esc]
  205. // discard changes on [esc]
  206. $("input#editNode").val(prevTitle);
  207. $(this).blur();
  208. break;
  209. case 13: // [enter]
  210. // simulate blur to accept new value
  211. $(this).blur();
  212. break;
  213. }
  214. }).blur(function(event){
  215. // Accept new value, when user leaves <input>
  216. var title = $("input#editNode").val();
  217. node.setTitle(title);
  218. // Re-enable mouse and keyboard handlling
  219. tree.widget._bind();
  220. //node.focus();
  221. });
  222. }
  223. var arrTocTree = new Array();
  224. var iTocTreeCurrLevel = 1;
  225. function getTocTreeData(){
  226. let tree = $("#ul_article_list").fancytree("getTree");
  227. let d = tree.toDict(false);
  228. for (const iterator of d) {
  229. getTreeNodeData(iterator);
  230. }
  231. }
  232. function getTreeNodeData(node){
  233. let children = 0;
  234. if( typeof node.children != "undefined"){
  235. children = node.children.length;
  236. }
  237. arrTocTree.push({article:node.key,title:node.title,level:iTocTreeCurrLevel});
  238. if(children>0){
  239. iTocTreeCurrLevel++;
  240. for (const iterator of node.children) {
  241. getTreeNodeData(iterator);
  242. }
  243. iTocTreeCurrLevel--;
  244. }
  245. }
  246. function my_collect_render_article(index, article) {
  247. let html = "";
  248. html += "<li id='article_item_" + index + "' article_index='" + index + "' class='file_list_row'>";
  249. html += "<span style='flex:1;'>";
  250. html += "<select>";
  251. let selected = "";
  252. for (let i = 1; i < 9; i++) {
  253. if (parseInt(article.level) == i) {
  254. selected = "selected";
  255. } else {
  256. selected = "";
  257. }
  258. html += "<option " + selected + " value='" + i + "' >H " + i + "</option>";
  259. }
  260. html += "</select>";
  261. html += "</span>";
  262. html += "<span style='flex:3;'>";
  263. html += "<a href='../article/my_article_edit.php?id=" + article.article + "'>";
  264. html += article.title;
  265. html += "</a>";
  266. html += "</span>";
  267. html += "<span style='flex:1;' onclick=\"article_preview('" + article.article + "')\">";
  268. html += "Preview";
  269. html += "</span>";
  270. html += "</li>";
  271. return html;
  272. }
  273. function article_preview(id) {
  274. $.get(
  275. "../article/get.php",
  276. {
  277. id: id,
  278. setting: "",
  279. },
  280. function (data, status) {
  281. if (status == "success") {
  282. try {
  283. let html = "";
  284. let result = JSON.parse(data);
  285. $("#preview_inner").html(note_init(result.content));
  286. note_refresh_new();
  287. } catch (e) {
  288. console.error(e.message);
  289. }
  290. }
  291. }
  292. );
  293. }
  294. function my_collect_save() {
  295. getTocTreeData();
  296. $("#form_article_list").val( JSON.stringify(arrTocTree))
  297. $.ajax({
  298. type: "POST", //方法类型
  299. dataType: "json", //预期服务器返回的数据类型
  300. url: "../article/my_collect_post.php", //url
  301. data: $("#collect_edit").serialize(),
  302. success: function (result) {
  303. console.log(result); //打印服务端返回的数据(调试用)
  304. if (result.status == 0) {
  305. alert("保存成功");
  306. } else {
  307. alert("error:" + result.message);
  308. }
  309. },
  310. error: function (data, status) {
  311. alert("异常!" + data.responseText);
  312. switch (status) {
  313. case "timeout":
  314. break;
  315. case "error":
  316. break;
  317. case "notmodified":
  318. break;
  319. case "parsererror":
  320. break;
  321. default:
  322. break;
  323. }
  324. },
  325. });
  326. }
  327. function collection_share(id) {
  328. share_win.show("../share/share.php?id=" + id + "&type=4");
  329. }