邓淳远、崔鹏宇、翁思扬组云计算期末项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
6.0 KiB

4 years ago
  1. var url_prefix = "http://127.0.0.1:5000/";
  2. function sendRequest(url_suffix, data, func) {
  3. var url = url_prefix + url_suffix;
  4. $.ajax({
  5. type: "post",
  6. url: url,
  7. data: data,
  8. dataType: "json",
  9. processData: false,
  10. contentType: false,
  11. success: function (data) {
  12. func(data);
  13. },
  14. error: function (XMLHttpRequest, textStatus, errorThrown) {
  15. alert(XMLHttpRequest.status);
  16. alert(XMLHttpRequest.readyState);
  17. alert(textStatus);
  18. console.log(this);
  19. }
  20. });
  21. }
  22. function GetRequest() {
  23. var url = location.search; //获取url中"?"符后的字串
  24. var theRequest = new Object();
  25. if (url.indexOf("?") != -1) {
  26. var str = url.substr(1);
  27. strs = str.split("&");
  28. for (var i = 0; i < strs.length; i++) {
  29. theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
  30. }
  31. }
  32. return theRequest;
  33. }
  34. function getBlock() {
  35. var url = "block/select";
  36. var data = new FormData();
  37. data.append("collection_id",id);
  38. sendRequest(url, data,
  39. function (data) {
  40. var list = $("#textList");
  41. var blocks = data.blocks;
  42. for (i in blocks) {
  43. var content;
  44. var block = blocks[i];
  45. if (block.type == 'img') {
  46. content = document.createElement('img');
  47. content.src = block.content;
  48. }
  49. else if (block.type == 'text') {
  50. content = document.createElement('div');
  51. content.innerText = block.content;
  52. }
  53. else {
  54. content = document.createElement('div');
  55. content.innerText = block.content;
  56. }
  57. content.id = block.id;
  58. var block = gengerate_block(content,"block");
  59. list.append(block);
  60. }
  61. });
  62. }
  63. function submit(obj, content, type, order) {
  64. var url = "http://127.0.0.1:5000/block/add";
  65. var data = new FormData();
  66. data.append("content", content);
  67. data.append('type', type);
  68. data.append('order', order);
  69. var that = obj;
  70. $.ajax({
  71. type: "post",
  72. url: url,
  73. data: data,
  74. dataType: "json",
  75. processData: false,
  76. contentType: false,
  77. success: function (data) {
  78. that.id = data.id;
  79. },
  80. error: function (XMLHttpRequest, textStatus, errorThrown) {
  81. alert(XMLHttpRequest.status);
  82. alert(XMLHttpRequest.readyState);
  83. alert(textStatus);
  84. console.log(this);
  85. }
  86. });
  87. }
  88. function swap(obj, dir,type) {
  89. var url;
  90. if (type == "block") {
  91. url = "block/swap";
  92. }
  93. else {
  94. url = "collection/swap";
  95. }
  96. var pos = obj.parentNode.id;
  97. if (dir == 'up' && pos > 0) {
  98. pos--;
  99. }
  100. if (dir == 'down' && dir < $("#textList").childElementCount - 1) {
  101. pos++;
  102. }
  103. var data = new FormData();
  104. data.append("new_order", pos);
  105. data.append("id", obj.id);
  106. if (type == "block") {
  107. sendRequest(url, data, getBlock);
  108. }
  109. else{
  110. sendRequest(url, data, getCollection);
  111. }
  112. }
  113. function delete_block(obj,type) {
  114. var url;
  115. var data = new FormData();
  116. data.append("collection_id", id);
  117. if (type == "block") {
  118. data.append("block_id", obj.id);
  119. url = "block/delete";
  120. }
  121. else {
  122. url = "collection/delete";
  123. }
  124. sendRequest(url, data, function () { ;});
  125. }
  126. function gengerate_block(content,type) {
  127. var obj = document.createElement("div");
  128. obj.className = "standard-input";
  129. obj.id = document.getElementById('textList').childElementCount;
  130. var up = document.createElement("div");
  131. up.addEventListener("click", function () { swap(content, 'up',type); });
  132. up.innerText = "swap up";
  133. obj.appendChild(up);
  134. obj.appendChild(content);
  135. var down = document.createElement("div");
  136. down.addEventListener("click", function () { swap(content, 'down',type) });
  137. down.innerText = "swap down";
  138. obj.appendChild(down);
  139. var delete_item = document.createElement("div");
  140. delete_item.addEventListener("click", function () { delete_block(content,type); });
  141. delete_item.innerText = "delete";
  142. obj.appendChild(delete_item);
  143. return obj;
  144. }
  145. function add(type) {
  146. var text = $("#myInput").val();
  147. $("#myInput").val("");
  148. if (text == "") {
  149. return;
  150. }
  151. let div = document.createElement('div');
  152. div.innerText = text;
  153. var block = gengerate_block(div,type)
  154. $('#textList').append(block);
  155. var data = new FormData();
  156. data.append('type', 'text');
  157. data.append('name', text);
  158. data.append('order', block.id);
  159. data.append('content', text);
  160. var url;
  161. if (type == 'block') {
  162. data.append('collection_id');
  163. url = "block/add";
  164. }
  165. else {
  166. url = "collection/add";
  167. }
  168. sendRequest(url,data,function (data) {
  169. div.id = data.id;
  170. })
  171. }
  172. function getCollection() {
  173. var url = "collection/select";
  174. var data = new FormData();
  175. if ("undefined" != typeof phonenum) {
  176. data.append('phonenum',phonenum);
  177. }
  178. var name = $("#search").val();
  179. if(name != ""){
  180. data.append('name',name);
  181. }
  182. $("#textList").children().remove();
  183. sendRequest(url, data, function (data) {
  184. console.log(data);
  185. var list = $("#textList");
  186. var collections = data.collections;
  187. for (i in collections) {
  188. var content = document.createElement('div');
  189. var collection = collections[i];
  190. content.innerText = collection.name;
  191. content.onclick = function () {
  192. console.log("?");
  193. window.location.href = "add.html?id=" + collection.id + "&name=" + collection.name;
  194. };
  195. content.className = "standard-box";
  196. content.id = collection.id;
  197. $('#textList').append(gengerate_block(content,"collection"));
  198. }
  199. });
  200. }