list.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // 基本信息(包括userAgent)
  2. var baseInfo = null,
  3. // 基础环境信息(包括当前webview、encry相关)
  4. baseEnv = null,
  5. self,
  6. docInfo = null;
  7. var sort,touchtime, item_offset = {
  8. left: '',
  9. top: ''
  10. };
  11. var old_back = mui.back;
  12. // 获取登录相关信息
  13. var getBaseInfo = function() {
  14. docInfo = JSON.parse(plus.storage.getItem("docInfo"));
  15. // 登录的相关信息
  16. var userAgent = JSON.parse(plus.storage.getItem("userAgent"));
  17. return {
  18. userAgent: userAgent
  19. }
  20. },
  21. // 获取基础环境信息
  22. getBaseEnvPromise = function () {
  23. var env = {
  24. webview: plus.webview.currentWebview()
  25. };
  26. mui.back = function(){
  27. var index = plus.webview.getWebviewById("index");
  28. mui.fire(index, 'refresh');
  29. plus.webview.currentWebview().close();
  30. }
  31. // 因为有异步请求,需要等待数据返回之后进行处理,所以返回Promise
  32. return Promise.resolve().then(function(res) {
  33. return env;
  34. });
  35. },
  36. initPage = function(){
  37. plus.nativeUI.showWaiting();
  38. $("#edit_btn").text("编辑");
  39. $("#edit_btn").attr("data-status", "read");
  40. $("#reply_list").find(".c-list-key").hide();
  41. $(".c-list-info").attr("contenteditable","false");
  42. var params={}
  43. if(self.type == 'xufang'){
  44. params.type=1
  45. }
  46. sendGet("doctor/reply/list",params,null,function(res){
  47. $("#reply_list").empty().append(template('reply_list_tmp',{list: res.data}));
  48. plus.nativeUI.closeWaiting();
  49. });
  50. },
  51. bindEvents = function(){
  52. sort = new Sortable(document.getElementById("reply_list"), {
  53. chosenClass: 'sort-chosen',
  54. ghostClass: 'sort-ghost',
  55. scroll: true,
  56. // delay:300,
  57. animation: 400,
  58. handle: '.drag-handle',
  59. isDropAnimation: false, //自定义属性
  60. onStart: function( /**Event*/ evt) { // 拖拽
  61. //console.log(new Date(evt.timeStamp).toLocaleString());
  62. //console.log(evt.oldIndex);
  63. touchtime = evt.timeStamp;
  64. item_offset.left = evt.item.offsetLeft;
  65. item_offset.top = evt.item.offsetTop;
  66. var itemEl = evt.item;
  67. },
  68. onEnd: function( /**Event*/ evt) { // 拖拽
  69. var itemEl = evt.item;
  70. //console.log(new Date(evt.timeStamp).toLocaleString());
  71. var timespan = evt.timeStamp - touchtime;
  72. //console.log(evt.timeStamp - touchtime);
  73. if (timespan < 200) {} else if (itemEl.offsetLeft == item_offset.left && itemEl.offsetTop == item_offset.top) {} else {
  74. // reset_order();
  75. //TODO 还有一个bug未处理,重新排序后,预览图片并返回时offset位置不对,应提供方法重置previewimg
  76. }
  77. touchtime = null;
  78. //将保存按钮高亮
  79. $("#edit_btn").removeClass("disabled");
  80. },
  81. });
  82. //编辑按钮的点击事件
  83. $("#edit_btn").on('click', function(){
  84. var status = $(this).attr("data-status");
  85. if(status == "read"){
  86. $(this).text("保存排序");
  87. $(this).addClass("disabled");
  88. $(this).attr("data-status", "edit");
  89. $("#reply_list").find(".c-list-key").show();
  90. $(".c-list-info.ok-fill").attr("contenteditable","true");
  91. }
  92. if(status == "edit"){
  93. if(!$(this).hasClass("disabled")){
  94. $(this).text("编辑");
  95. $(this).attr("data-status", "read");
  96. $("#reply_list").find(".c-list-key").hide();
  97. $(".c-list-info").attr("contenteditable","false");
  98. //保存排序
  99. var li_list = $("#reply_list").find("li"),
  100. len = li_list.length,
  101. id_list = [];
  102. for(i=0; i<len; i++){
  103. var item = li_list[i],
  104. id = $(item).attr("data-id");
  105. id_list.push(id);
  106. }
  107. plus.nativeUI.showWaiting();
  108. var url = "doctor/reply/sortList",
  109. params = {id: id_list.join(",")};
  110. if(self.type == 'xufang'){
  111. params.type =1;//续方咨询排序标志
  112. }
  113. sendPost(url, params, null, function(res){
  114. var xufang = plus.webview.getWebviewById('xufangzixun')
  115. var xufang2 = plus.webview.getWebviewById('xufangzixun.html')
  116. setTimeout(function(){
  117. if(xufang){
  118. mui.fire(xufang, 'quickReplyRefresh');
  119. }
  120. if(xufang2){
  121. mui.fire(xufang2, 'quickReplyRefresh');
  122. }
  123. },500)
  124. mui.toast(res.msg);
  125. plus.nativeUI.closeWaiting();
  126. });
  127. }
  128. }
  129. });
  130. //删除图标的点击事件
  131. $("#reply_list").on('click', '.del-icon', function(){
  132. var parent = this.parentElement.parentElement;
  133. //点开显示删除按钮
  134. mui.swipeoutOpen(this.parentElement.parentElement);
  135. }).on('click','.mui-disabled', function(){
  136. var self = this,
  137. $this = $(this),
  138. $parent = $this.parent(),
  139. code = $parent.attr("data-id");
  140. plus.nativeUI.showWaiting();
  141. sendPost("doctor/reply/delete",{id:code},null, function(res){
  142. if(res.status == 200){
  143. var xufang = plus.webview.getWebviewById('xufangzixun')
  144. setTimeout(function(){
  145. if(xufang){
  146. mui.fire(xufang, 'quickReplyRefresh');
  147. }
  148. },500)
  149. $parent.remove();
  150. mui.toast("删除成功");
  151. }else{
  152. mui.toast(res.msg);
  153. }
  154. plus.nativeUI.closeWaiting();
  155. });
  156. }).on('click','.c-list-info',function(){
  157. mui.swipeoutClose(this.parentElement.parentElement);
  158. var editable = $(this).attr("contenteditable");
  159. if(editable == "false" && $("#edit_btn").attr("data-status")=="read"){
  160. //将结果放置到IM的输入框内
  161. var content = $(this).text();
  162. var type = $(this).attr('data-type')
  163. var opener = baseEnv.webview.opener();
  164. if(opener){
  165. mui.fire(opener, 'fillText', {content: content,type:type});
  166. old_back();
  167. }
  168. }
  169. }).on("input",'.c-list-info.ok-fill', function(e){
  170. var content = $(this).text(),
  171. len = content.length;
  172. if(content == " "){
  173. content = $.trim(content);
  174. len = content.length;
  175. }
  176. if(len > 100){
  177. $(this).text(content.substr(0, 100));
  178. var range = document.createRange();
  179. range.selectNodeContents(this);
  180. range.collapse(false);
  181. var sel = window.getSelection();
  182. sel.removeAllRanges();
  183. sel.addRange(range);
  184. }
  185. }).on('blur','.c-list-info.ok-fill', function(){
  186. var editable = $(this).attr("contenteditable"),
  187. $parent = $(this).closest("li"),
  188. id = $(this).closest("li").attr("data-id");
  189. if(editable == "true"){
  190. //保存编辑的内容
  191. if($.trim($(this).text()).length == 0){ //清空内容默认删除该内容
  192. plus.nativeUI.showWaiting();
  193. sendPost("doctor/reply/delete",{id:id},null, function(res){
  194. if(res.status == 200){
  195. var xufang = plus.webview.getWebviewById('xufangzixun')
  196. setTimeout(function(){
  197. if(xufang){
  198. mui.fire(xufang, 'quickReplyRefresh');
  199. }
  200. },500)
  201. $parent.remove();
  202. mui.toast("删除成功");
  203. }else{
  204. // mui.toast(res.msg);
  205. }
  206. plus.nativeUI.closeWaiting();
  207. });
  208. }else{
  209. var url = "doctor/reply/modify",
  210. params = {id: id, content: $(this).text()};
  211. if(self.type == 'xufang'){
  212. params.type = 1
  213. }
  214. plus.nativeUI.showWaiting();
  215. sendPost(url, params, null, function(res){
  216. if(res.status == 200){
  217. mui.toast("修改成功");
  218. var xufang = plus.webview.getWebviewById('xufangzixun')
  219. setTimeout(function(){
  220. if(xufang){
  221. mui.fire(xufang, 'quickReplyRefresh');
  222. }
  223. },500)
  224. }
  225. plus.nativeUI.closeWaiting();
  226. });
  227. }
  228. }
  229. })
  230. $("#add_reply").on('click', function(){
  231. if(self.type == 'xufang'){
  232. openWebview("add_reply.html",{type:'xufang'});
  233. }else{
  234. openWebview("add_reply.html");
  235. }
  236. $("#edit_btn").text("编辑");
  237. $("#edit_btn").attr("data-status", "read");
  238. $("#edit_btn").removeClass("disabled");
  239. $("#reply_list").find(".c-list-key").hide();
  240. $(".c-list-info").attr("contenteditable","false");
  241. })
  242. };
  243. // 页面业务处理流程开始
  244. new Promise(function(resolve, reject) {
  245. // TODO 临时放开
  246. //resolve(true);
  247. mui.plusReady(function() {
  248. // plus已经准备好,可以往下执行
  249. resolve(true);
  250. });
  251. }).then(function() {
  252. self = plus.webview.currentWebview();
  253. // 获取基础环境信息
  254. return getBaseEnvPromise().then(function(env) {
  255. baseEnv = env;
  256. }).then(function() {
  257. old_back = mui.back;
  258. mui.back = function(){
  259. if($("#edit_btn").attr("data-status") == "edit"){
  260. //编辑状态时点击返回,退出编辑状态
  261. $("#edit_btn").text("编辑");
  262. $("#edit_btn").removeClass("disabled");
  263. $("#edit_btn").attr("data-status", "read");
  264. $("#reply_list").find(".c-list-key").hide();
  265. $(".c-list-info").attr("contenteditable","false");
  266. }else{
  267. old_back();
  268. }
  269. }
  270. // 获取登录医生信息
  271. baseInfo = getBaseInfo();
  272. //初始化页面内容
  273. initPage();
  274. // 绑定页面事件
  275. bindEvents();
  276. })
  277. }).catch(function(e) {
  278. plus.nativeUI.closeWaiting();
  279. console && console.error(e);
  280. });
  281. window.addEventListener("refresh", function(){
  282. initPage();
  283. });