my_collect.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. //新的层级比较大,为上一个的子目录
  193. treeParents.push(lastInsNode);
  194. lastInsNode.children = new Array();
  195. lastInsNode.children.push(newNode);
  196. currParentNode = lastInsNode;
  197. }
  198. else if(newNode.level==iCurrLevel){
  199. //目录层级相同,为平级
  200. currParentNode = treeParents[treeParents.length-1];
  201. treeParents[treeParents.length-1].children.push(newNode);
  202. }
  203. else{
  204. // 小于 挂在上一个层级
  205. while(treeParents.length>1){
  206. treeParents.pop();
  207. if(treeParents[treeParents.length-1].level<newNode.level){
  208. break;
  209. }
  210. }
  211. currParentNode = treeParents[treeParents.length-1];
  212. treeParents[treeParents.length-1].children.push(newNode);
  213. iLastParentNodeLevel = treeParents[treeParents.length-1].level;
  214. }
  215. lastInsNode = newNode;
  216. iCurrLevel = newNode.level;
  217. if(active==element.article){
  218. tocActivePath = new Array();
  219. for (let index = 1; index < treeParents.length; index++) {
  220. treeParents[index]["expanded"]=true;
  221. tocActivePath.push(treeParents[index]);
  222. }
  223. }
  224. }
  225. return treeData[0].children;
  226. }
  227. function editNode(node){
  228. var prevTitle = node.title,
  229. tree = node.tree;
  230. // Disable dynatree mouse- and key handling
  231. tree.widget._unbind();
  232. // Replace node with <input>
  233. $(".fancytree-title", node.span).html("<input id='editNode' value='" + prevTitle + "'>");
  234. // Focus <input> and bind keyboard handler
  235. $("input#editNode")
  236. .focus()
  237. .keydown(function(event){
  238. switch( event.which ) {
  239. case 27: // [esc]
  240. // discard changes on [esc]
  241. $("input#editNode").val(prevTitle);
  242. $(this).blur();
  243. break;
  244. case 13: // [enter]
  245. // simulate blur to accept new value
  246. $(this).blur();
  247. break;
  248. }
  249. }).blur(function(event){
  250. // Accept new value, when user leaves <input>
  251. var title = $("input#editNode").val();
  252. node.setTitle(title);
  253. // Re-enable mouse and keyboard handlling
  254. tree.widget._bind();
  255. //node.focus();
  256. });
  257. }
  258. function removeTocNode(){
  259. if(confirm("Delete article and the sub article?")){
  260. currSelectNode.remove();
  261. }
  262. }
  263. var arrTocTree;
  264. var iTocTreeCurrLevel = 1;
  265. function getTocTreeData(){
  266. arrTocTree = new Array();
  267. let tree = $("#ul_article_list").fancytree("getTree");
  268. let d = tree.toDict(false);
  269. for (const iterator of d) {
  270. getTreeNodeData(iterator);
  271. }
  272. }
  273. function getTreeNodeData(node){
  274. let children = 0;
  275. if( typeof node.children != "undefined"){
  276. children = node.children.length;
  277. }
  278. arrTocTree.push({article:node.key,title:node.title,level:iTocTreeCurrLevel,children:children});
  279. if(children>0){
  280. iTocTreeCurrLevel++;
  281. for (const iterator of node.children) {
  282. getTreeNodeData(iterator);
  283. }
  284. iTocTreeCurrLevel--;
  285. }
  286. }
  287. function my_collect_render_article(index, article) {
  288. let html = "";
  289. html += "<li id='article_item_" + index + "' article_index='" + index + "' class='file_list_row'>";
  290. html += "<span style='flex:1;'>";
  291. html += "<select>";
  292. let selected = "";
  293. for (let i = 1; i < 9; i++) {
  294. if (parseInt(article.level) == i) {
  295. selected = "selected";
  296. } else {
  297. selected = "";
  298. }
  299. html += "<option " + selected + " value='" + i + "' >H " + i + "</option>";
  300. }
  301. html += "</select>";
  302. html += "</span>";
  303. html += "<span style='flex:3;'>";
  304. html += "<a href='../article/my_article_edit.php?id=" + article.article + "'>";
  305. html += article.title;
  306. html += "</a>";
  307. html += "</span>";
  308. html += "<span style='flex:1;' onclick=\"article_preview('" + article.article + "')\">";
  309. html += "Preview";
  310. html += "</span>";
  311. html += "</li>";
  312. return html;
  313. }
  314. function article_preview(id) {
  315. $.get(
  316. "../article/get.php",
  317. {
  318. id: id,
  319. setting: "",
  320. },
  321. function (data, status) {
  322. if (status == "success") {
  323. try {
  324. let html = "";
  325. let result = JSON.parse(data);
  326. $("#preview_inner").html(note_init(result.content));
  327. note_refresh_new();
  328. } catch (e) {
  329. console.error(e.message);
  330. }
  331. }
  332. }
  333. );
  334. }
  335. function my_collect_save() {
  336. getTocTreeData();
  337. $("#form_article_list").val( JSON.stringify(arrTocTree))
  338. $.ajax({
  339. type: "POST", //方法类型
  340. dataType: "json", //预期服务器返回的数据类型
  341. url: "../article/my_collect_post.php", //url
  342. data: $("#collect_edit").serialize(),
  343. success: function (result) {
  344. console.log(result); //打印服务端返回的数据(调试用)
  345. if (result.status == 0) {
  346. alert("保存成功");
  347. } else {
  348. alert("error:" + result.message);
  349. }
  350. },
  351. error: function (data, status) {
  352. alert("异常!" + data.responseText);
  353. switch (status) {
  354. case "timeout":
  355. break;
  356. case "error":
  357. break;
  358. case "notmodified":
  359. break;
  360. case "parsererror":
  361. break;
  362. default:
  363. break;
  364. }
  365. },
  366. });
  367. }
  368. function collection_share(id) {
  369. share_win.show("../share/share.php?id=" + id + "&type=4");
  370. }