my_collect.js 11 KB

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