common.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. Date.prototype.format = function(format){
  2. var o = {
  3. "M+" : this.getMonth()+1, //month
  4. "d+" : this.getDate(), //day
  5. "h+" : this.getHours(), //hour
  6. "m+" : this.getMinutes(), //minute
  7. "s+" : this.getSeconds(), //second
  8. "q+" : Math.floor((this.getMonth()+3)/3), //quarter
  9. "S" : this.getMilliseconds() //millisecond
  10. };
  11. if(/(y+)/.test(format)){
  12. format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
  13. }
  14. for(var k in o) {
  15. if(new RegExp("("+ k +")").test(format)){
  16. format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
  17. }
  18. }
  19. return format;
  20. };
  21. var TT = TAOTAO = {
  22. // 编辑器参数
  23. kingEditorParams : {
  24. filePostName : "uploadFile",
  25. uploadJson : '/rest/pic/upload',
  26. dir : "image"
  27. },
  28. // 格式化时间
  29. formatDateTime : function(val,row){
  30. var now = new Date(val);
  31. return now.format("yyyy-MM-dd hh:mm:ss");
  32. },
  33. // 格式化连接
  34. formatUrl : function(val,row){
  35. if(val){
  36. return "<a href='"+val+"' target='_blank'>查看</a>";
  37. }
  38. return "";
  39. },
  40. // 格式化价格
  41. formatPrice : function(val,row){
  42. return (val/100).toFixed(2);
  43. },
  44. // 避免js注入
  45. formatText : function(val,row){
  46. if(val){
  47. val = val.replace(/&/g,"&amp;");
  48. val = val.replace(/</g,"&lt;");
  49. val = val.replace(/>/g,"&gt;");
  50. val = val.replace(/"/g,"&quot;");
  51. }
  52. return val;
  53. },
  54. // 格式化商品的状态
  55. formatItemStatus : function formatStatus(val,row){
  56. if (val == 1){
  57. return '正常';
  58. } else if(val == 2){
  59. return '<span style="color:red;">下架</span>';
  60. } else {
  61. return '未知';
  62. }
  63. },
  64. init : function(data){
  65. this.initPicUpload(data);
  66. this.initItemCat(data);
  67. },
  68. // 初始化图片上传组件
  69. initPicUpload : function(data){
  70. $(".picFileUpload").each(function(i,e){
  71. var _ele = $(e);
  72. _ele.siblings("div.pics").remove();
  73. _ele.after('\
  74. <div class="pics">\
  75. <ul></ul>\
  76. </div>');
  77. if(data && data.pics){
  78. var imgs = data.pics.split(",");
  79. for(var i in imgs){
  80. if($.trim(imgs[i]).length > 0){
  81. _ele.siblings(".pics").find("ul").append("<li><a href='"+imgs[i]+"' target='_blank'><img src='"+imgs[i]+"' width='80' height='50' /></a></li>");
  82. }
  83. }
  84. }
  85. $(e).unbind('click').click(function(){
  86. var form = $(this).parentsUntil("form").parent("form");
  87. KindEditor.editor(TT.kingEditorParams).loadPlugin('multiimage',function(){
  88. var editor = this;
  89. editor.plugin.multiImageDialog({
  90. clickFn : function(urlList) {
  91. _ele.siblings("div.pics").remove();
  92. _ele.after('\
  93. <div class="pics">\
  94. <ul></ul>\
  95. </div>');
  96. var imgArray = [];
  97. KindEditor.each(urlList, function(i, data) {
  98. imgArray.push(data.url);
  99. form.find(".pics ul").append("<li><a href='"+data.url+"' target='_blank'><img src='"+data.url+"' width='80' height='50' /></a></li>");
  100. });
  101. form.find("[name=image]").val(imgArray.join(","));
  102. editor.hideDialog();
  103. }
  104. });
  105. });
  106. });
  107. });
  108. },
  109. // 初始化选择类目组件
  110. initItemCat : function(data){
  111. $(".selectItemCat").each(function(i,e){
  112. var _ele = $(e);
  113. if(data && data.cid){
  114. $.getJSON('/rest/item/cat/'+data.cid,function(_data){
  115. _ele.after("<span style='margin-left:10px;'>"+_data.name+"</span>");
  116. });
  117. }else{
  118. _ele.after("<span style='margin-left:10px;'></span>");
  119. }
  120. _ele.unbind('click').click(function(){
  121. $("<div>").css({padding:"5px"}).html("<ul>")
  122. .window({
  123. width:'500',
  124. height:"450",
  125. modal:true,
  126. closed:true,
  127. iconCls:'icon-save',
  128. title:'选择类目',
  129. onOpen : function(){
  130. var _win = this;
  131. $("ul",_win).tree({
  132. url:'/rest/item/cat',
  133. method:'GET',
  134. animate:true,
  135. onClick : function(node){
  136. if($(this).tree("isLeaf",node.target)){
  137. // 填写到cid中
  138. _ele.parent().find("[name=cid]").val(node.id);
  139. _ele.next().text(node.text).attr("cid",node.id);
  140. $(_win).window('close');
  141. }
  142. }
  143. });
  144. },
  145. onClose : function(){
  146. $(this).window("destroy");
  147. }
  148. }).window('open');
  149. });
  150. });
  151. },
  152. createEditor : function(select){
  153. return KindEditor.create(select, TT.kingEditorParams);
  154. },
  155. /**
  156. * 创建一个窗口,关闭窗口后销毁该窗口对象。<br/>
  157. *
  158. * 默认:<br/>
  159. * width : 80% <br/>
  160. * height : 80% <br/>
  161. * title : (空字符串) <br/>
  162. *
  163. * 参数:<br/>
  164. * width : <br/>
  165. * height : <br/>
  166. * title : <br/>
  167. * url : 必填参数 <br/>
  168. * onLoad : function 加载完窗口内容后执行<br/>
  169. *
  170. *
  171. */
  172. createWindow : function(params){
  173. $("<div>").css({padding:"5px"}).window({
  174. width : params.width?params.width:"90%",
  175. height : params.height?params.height:"90%",
  176. modal:true,
  177. title : params.title?params.title:" ",
  178. href : params.url,
  179. onClose : function(){
  180. $(this).window("destroy");
  181. },
  182. onLoad : function(){
  183. if(params.onLoad){
  184. params.onLoad.call(this);
  185. }
  186. }
  187. }).window("open");
  188. },
  189. closeCurrentWindow : function(){
  190. $(".panel-tool-close").click();
  191. },
  192. getSelectionsIds : function (select){
  193. var list = $(select);
  194. var sels = list.datagrid("getSelections");
  195. var ids = [];
  196. for(var i in sels){
  197. ids.push(sels[i].id);
  198. }
  199. ids = ids.join(",");
  200. return ids;
  201. },
  202. /**
  203. * 初始化单图片上传组件 <br/>
  204. * 选择器为:.onePicUpload <br/>
  205. * 上传完成后会设置input内容以及在input后面追加<img>
  206. */
  207. initOnePicUpload : function(){
  208. $(".onePicUpload").click(function(){
  209. var _self = $(this);
  210. KindEditor.editor(TT.kingEditorParams).loadPlugin('image', function() {
  211. this.plugin.imageDialog({
  212. showRemote : false,
  213. clickFn : function(url, title, width, height, border, align) {
  214. var input = _self.siblings("input");
  215. input.parent().find("img").remove();
  216. input.val(url);
  217. input.after("<a href='"+url+"' target='_blank'><img src='"+url+"' width='80' height='50'/></a>");
  218. this.hideDialog();
  219. }
  220. });
  221. });
  222. });
  223. }
  224. };