my_collect.js 10 KB

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