my_collect.js 11 KB

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