adminDiy.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  1. ;(function($){
  2. var app = new Object();
  3. /*获取 当前活动元素或指定元素的样式*/
  4. app.getActiveStyle = function(styleName,object){
  5. var canvas = this.mainCanvas;
  6. object = object || canvas.getActiveObject();
  7. if(!object) return '';
  8. return (object.getSelectionStyles && object.isEditing) ? (object.getSelectionStyles()[styleName] || '') : (object[styleName] || '');
  9. }
  10. /*设置 当前活动元素或指定元素的样式*/
  11. app.setActiveStyle = function(styleName,value,object){
  12. var canvas = this.mainCanvas;
  13. object = object || canvas.getActiveObject();
  14. if (!object) return;
  15. if (object.setSelectionStyles && object.isEditing) {
  16. var style = {};
  17. style[styleName] = value;
  18. object.setSelectionStyles(style);
  19. object.setCoords();
  20. }
  21. else {
  22. object.set(styleName, value);
  23. }
  24. object.setCoords();
  25. canvas.renderAll();
  26. }
  27. /*获取 当前活动或者指定元素的属性值*/
  28. app.getActiveProp = function(name,object){
  29. var canvas = this.mainCanvas;
  30. var object = object || canvas.getActiveObject();
  31. if (!object) return '';
  32. return object[name] || '';
  33. }
  34. /*设置 当前活动或者指定元素的属性值*/
  35. app.setActiveProp = function(name, value,object){
  36. var canvas = this.mainCanvas;
  37. var object = object || canvas.getActiveObject();
  38. if (!object) return;
  39. object.set(name, value).setCoords();
  40. canvas.renderAll();
  41. }
  42. /*调整印花控制器显示的个数*/
  43. app.ControlVisibility = function(canvasItem){
  44. var _controlsVisibility = {
  45. tl: !0,
  46. tr: !0,
  47. br: !0,
  48. bl: !0,
  49. ml: 0,
  50. mt: 0,
  51. mr: 0,
  52. mb: 0,
  53. mtr: !0
  54. }
  55. for(var item in _controlsVisibility){
  56. canvasItem['setControlVisible'](item,_controlsVisibility[item]);
  57. }
  58. }
  59. //添加图片
  60. app.addImg = function(url){
  61. var that = this;
  62. fabric.Image.fromURL(url,function(designImg){
  63. designImg.set({
  64. cornerSize:15,
  65. cornerStyle:'circle',
  66. rotatingPointOffset:30,
  67. borderDashArray:[3,6],
  68. borderColor:'#000',
  69. cornerColor: '#000'
  70. });
  71. //图片宽高比
  72. var imgRATIO = designImg.width / designImg.height;
  73. //图片最大尺寸 app.oldCutWidth
  74. that.maxWidthImg = that.oldCutWidth*0.8;
  75. that.maxHeightImg = that.oldCutHeight*0.8;
  76. var imgWidth,imgHeight
  77. //app.cutWidth
  78. if(that.maxHeightImg*imgRATIO < that.maxWidthImg ){
  79. imgWidth = that.maxHeightImg*imgRATIO;
  80. imgHeight = that.maxHeightImg;
  81. }else{
  82. imgWidth = that.maxWidthImg;
  83. imgHeight = that.maxWidthImg/imgRATIO;
  84. }
  85. designImg.setWidth(imgWidth);
  86. designImg.setHeight(imgHeight);
  87. that.centerObj(designImg);
  88. that.mainCanvas.add(designImg).setActiveObject(designImg);
  89. that.mainCanvas.renderAll();
  90. that.mainCanvas.calcOffset();
  91. })
  92. }
  93. //返回当前中心店坐标
  94. app.transformViewPoint = function(o){
  95. var that = this;
  96. var point = that.mainCanvas.getCenter();
  97. if(o){
  98. var i ={
  99. x:o.x,
  100. y:o.y
  101. }
  102. }else{
  103. var i ={
  104. x:point.left,
  105. y:point.top
  106. }
  107. }
  108. // after 计算出 变换之后 中心点坐标
  109. var after = fabric.util.transformPoint(i, fabric.util.invertTransform(that.mainCanvas.viewportTransform));
  110. return after;
  111. }
  112. //当前元素居中
  113. app.centerObj = function(obj){
  114. var that = this;
  115. console.log(obj)
  116. var afterPoint = app.transformViewPoint();
  117. obj.set('left',afterPoint.x - obj.width*obj.scaleX/2);
  118. obj.set('top',afterPoint.y - obj.height*obj.scaleY/2);
  119. obj.setCoords();
  120. }
  121. //添加文字
  122. app.addText = function(texts){
  123. var that = this;
  124. var text = new fabric.IText(texts, {
  125. fontSize: 30,
  126. textAlign:'left',
  127. cornerStyle:'circle',
  128. borderColor:'#000',
  129. cornerColor: '#000',
  130. fontFamily:'微软雅黑',
  131. cornerSize:15,
  132. rotatingPointOffset:30,
  133. borderDashArray:[3,6]
  134. });
  135. text.on('editing:entered',function(){
  136. app.textFlage = true;
  137. });
  138. text.on('editing:exited',function(){
  139. app.textFlage = false;
  140. });
  141. app.ControlVisibility(text);
  142. that.centerObj(text);
  143. that.mainCanvas.add(text).setActiveObject(text);
  144. that.mainCanvas.renderAll();
  145. }
  146. app.delObject = function(){
  147. var that = this;
  148. $("#diy_del_object").click(function(){
  149. if(that.mainCanvas.getActiveGroup()){
  150. alert('请选择单个对象删除');
  151. return false;
  152. }
  153. var obj = that.mainCanvas.getActiveObject();
  154. if(!obj||obj=='undefined'){return false;}
  155. that.mainCanvas.remove(obj);
  156. that.mainCanvas.renderAll();
  157. });
  158. }
  159. app.cloneObject = function(){
  160. var that = this;
  161. $("#diy_clone_object").click(function(){
  162. if(that.mainCanvas.getActiveGroup()){
  163. alert('请选择单个对象复制');
  164. return false;
  165. }
  166. var obj = that.mainCanvas.getActiveObject();
  167. if(!obj||obj=='undefined'){return false;}
  168. var point = that.mainCanvas.getCenter();
  169. obj.clone(function(obj){
  170. that.centerObj(obj);
  171. that.mainCanvas.add(obj).setActiveObject(obj);
  172. },["cornerSize","_controlsVisibility","cornerStyle","rotatingPointOffset","borderDashArray","borderColor","cornerColor","fill"]);
  173. });
  174. }
  175. //canvas 检测按下
  176. app.dowmCanvas = function(canvas){
  177. canvas.on('mouse:down',function(options){
  178. if(options.target && options.target.type=='i-text'){
  179. $("#editor-panel").show();
  180. }else{
  181. $("#editor-panel").hide();
  182. $(".diy_top_nav_item").removeClass('active');
  183. }
  184. });
  185. canvas.on('mouse:up',function(options){
  186. if(options.target){
  187. }
  188. });
  189. canvas.on('object:selected',function(options){
  190. var group = canvas.getActiveGroup();
  191. if(group){
  192. group.set({
  193. cornerSize:15,
  194. cornerStyle:'circle',
  195. rotatingPointOffset:30,
  196. borderDashArray:[3,6],
  197. borderColor:'#000',
  198. cornerColor: '#000'
  199. })
  200. }
  201. })
  202. }
  203. //把当前画布保存成图片
  204. app.returnCanvasImg = function(width){
  205. var that= this;
  206. app.outputWidth = width || 500 ;
  207. app.outputHeight = app.outputWidth/app.plateRATIO;
  208. //新的缩放比例
  209. var afterCutRATIO = util.numDiv(app.outputWidth,app.oldCutWidth);
  210. //中心点坐标
  211. var point = that.mainCanvas.getCenter();
  212. //之前缩放比例
  213. var zoomNum = that.mainCanvas.getZoom();
  214. //执行新的缩放
  215. that.mainCanvas.zoomToPoint(new fabric.Point(point.left,point.top),afterCutRATIO);
  216. //生成剪切图片
  217. var dataURL = that.mainCanvas.toDataURL({
  218. format: 'png',
  219. left: point.left -app.outputWidth/2 ,
  220. top: point.top - app.outputHeight/2,
  221. width: app.outputWidth,
  222. height: app.outputHeight
  223. });
  224. //缩放回原来大小
  225. that.mainCanvas.zoomToPoint(new fabric.Point(point.left,point.top),zoomNum);
  226. return dataURL;
  227. }
  228. app.returnCanvasJson = function(){
  229. var that = this;
  230. return that.mainCanvas.toJSON([
  231. 'borderColor',
  232. 'cornerColor',
  233. 'cornerStyle',
  234. 'cornerSize',
  235. 'rotatingPointOffset',
  236. 'borderDashArray',
  237. 'plateRATIO',
  238. 'viewportTransform',
  239. 'oldCutWidth',
  240. 'oldCutHeight',
  241. 'width',
  242. 'height',
  243. '_controlsVisibility'
  244. ]);
  245. }
  246. //绘制剪切区域
  247. app.cutArea = function(width,height,win){
  248. if(app.cutCanvas){
  249. $("#cutCanvas").remove();
  250. }
  251. app.cutCanvas = document.createElement('canvas');
  252. app.cutCanvas.width = width;
  253. app.cutCanvas.height= height;
  254. app.cutCanvas.id = 'cutCanvas';
  255. $(app.cutCanvas).css({
  256. position:'absolute',
  257. top:0,
  258. left:0,
  259. width:width,
  260. height:height
  261. });
  262. var cutCtx = app.cutCanvas.getContext('2d');
  263. cutCtx.fillStyle = 'rgba(173,173,173,0.8)';
  264. cutCtx.fillRect(0,0,width,height);
  265. //剪切区域的最大高度 和宽度
  266. app.maxWidth = app.baseWidth*0.8;
  267. app.maxHeight = app.baseHeight*0.8;
  268. if(!win){
  269. if(app.maxHeight*app.plateRATIO < app.maxWidth){
  270. app.cutWidth = app.maxHeight*app.plateRATIO;
  271. app.cutHeight = app.maxHeight;
  272. }else {
  273. app.cutWidth = app.maxWidth;
  274. app.cutHeight = app.maxWidth/app.plateRATIO;
  275. }
  276. if(!app.oldCutWidth){
  277. //存储之前剪切大小
  278. app.oldCutWidth = app.cutWidth;
  279. app.oldCutHeight = app.cutHeight;
  280. app.mainCanvas.set('oldCutWidth',app.oldCutWidth);
  281. app.mainCanvas.set('oldCutHeight',app.oldCutHeight);
  282. }
  283. }
  284. cutCtx.clearRect((width-app.cutWidth)/2,(height-app.cutHeight)/2, app.cutWidth, app.cutHeight);
  285. $("#diy").after(app.cutCanvas);
  286. }
  287. //变换屏幕变换时,让canvas 元素居中
  288. app.resizeCanvasSize = function(){
  289. var canvas = this.mainCanvas;
  290. app.centerLeft = canvas.getWidth()/2;
  291. app.centerTop = canvas.getHeight()/2;
  292. canvas.setWidth(app.baseWidth);
  293. canvas.setHeight(app.baseHeight);
  294. var point = canvas.getCenter();
  295. //抛弃选择中的组
  296. canvas.discardActiveGroup();
  297. app.resizeLeft = point.left - app.centerLeft,
  298. app.resizeTop = point.top - app.centerTop;
  299. var zoomNum = canvas.getZoom();
  300. //console.log(zoomNum)
  301. var objects = canvas.getObjects();
  302. //console.log(objects[0])
  303. for (var i in objects) {
  304. objects[i].left = objects[i].left + app.resizeLeft/zoomNum;
  305. objects[i].top = objects[i].top + app.resizeTop/zoomNum;
  306. objects[i].setCoords();
  307. }
  308. canvas.renderAll();
  309. canvas.calcOffset();
  310. }
  311. app.cutRATIO = null;
  312. //自适应屏幕 缩放
  313. app.resizeRender = function(){
  314. var that = this;
  315. //缩放比例
  316. var afterCutRATIO = util.numDiv(app.cutWidth,app.oldCutWidth);
  317. if(app.cutRATIO == afterCutRATIO){
  318. return false;
  319. }else{
  320. app.cutRATIO = afterCutRATIO;
  321. }
  322. var point = that.mainCanvas.getCenter();
  323. that.mainCanvas.zoomToPoint(new fabric.Point(point.left,point.top),app.cutRATIO);
  324. that.mainCanvas.renderAll();
  325. that.mainCanvas.calcOffset();
  326. }
  327. app.init = function(w,h,did){
  328. //diy 基础宽度
  329. app.baseWidth = $(".diy_canvas_wrap").width();
  330. app.baseHeight = $(".diy_canvas_wrap").height();
  331. if(!$("#diy")[0]){return false;};
  332. app.mainCanvas = new fabric.Canvas('diy',{
  333. selectionColor: 'rgba(0,0,0,0.1)',
  334. controlsAboveOverlay:true,
  335. preserveObjectStacking:true,
  336. backgroundColor:'rgba(255,255,255,0)',
  337. selctionBorderColor:'#000',
  338. selectionDashArray:[3,6],
  339. selectionLineWidth: 1
  340. });
  341. if(did){
  342. app.diyId = did;
  343. sdzAjax.getDiy(app.diyId,function(data){
  344. console.log(data);
  345. var json = JSON.parse(data.diy.content);
  346. app.plateRATIO = json.plateRATIO;
  347. app.oldCutWidth = json.oldCutWidth;
  348. app.oldCutHeight = json.oldCutHeight;
  349. app.cutArea(app.baseWidth,app.baseHeight);
  350. app.mainCanvas.loadFromJSON(json,function(){
  351. app.resizeCanvasSize();
  352. app.resizeRender();
  353. app.mainCanvas.renderAll.bind(app.mainCanvas);
  354. });
  355. app.diyTitle = data.diy.title;
  356. app.diyRemark = data.diy.remark;
  357. app.use_number = data.diy.use_number;
  358. app.festival_id = data.diy.festival_id;
  359. app.industry_id = data.diy.industry_id;
  360. app.purpose_id = data.diy.purpose_id;
  361. app.style_id = data.diy.style_id;
  362. app.subject_id = data.diy.subject_id;
  363. app.bottle = {
  364. id:$("#bottle_id").val(),
  365. img_url:$("#bottle_img_url").val()
  366. }
  367. })
  368. }else{
  369. //计算出 板式宽度高度比例
  370. app.plateRATIO = w/h;
  371. app.mainCanvas.set('plateRATIO',app.plateRATIO);
  372. app.mainCanvas.setWidth(app.baseWidth);
  373. app.mainCanvas.setHeight(app.baseHeight);
  374. app.cutArea(app.baseWidth,app.baseHeight);
  375. }
  376. app.dowmCanvas(app.mainCanvas);
  377. return app.mainCanvas;
  378. }
  379. //获取分类 sdzAjax
  380. function getMaterialType(){
  381. sdzAjax.getMaterialType(function(res){
  382. var list = res.list;
  383. var html = '';
  384. res.list.forEach(function(item){
  385. html+='<option value="'+ item.id +'">'+ item.title +'</option>';
  386. })
  387. $("#diy_color_select").html(html);
  388. //
  389. getMaterial(list[0].id);
  390. });
  391. }
  392. //素材分类 ID
  393. app.mtid = null;
  394. //change select
  395. function changeSelectMaterialType(){
  396. $("#diy_color_select").change(function(){
  397. var id = $(this).val();
  398. id && id != app.mtid && getMaterial(id);
  399. app.mtid = id;
  400. })
  401. }
  402. //获取分类下面内容
  403. function getMaterial(id){
  404. sdzAjax.getMaterial(id,function(res){
  405. var html ='';
  406. for(var i=0;i<res.list.length;i++){
  407. html+='<div class="box_item" data-imgUrl="'+ res.list[i].img_url +'">';
  408. html+='<div class="c">';
  409. html+='<p><img src="'+res.list[i].img_url+'" /></p>';
  410. html+='<p>'+res.list[i].title+'</p>';
  411. html+='</div>';
  412. html+='</div>';
  413. }
  414. $("#diy_box_content").html(html);
  415. $(".do-nicescroll").getNiceScroll().resize();
  416. $(".box_item").each(function(){
  417. $(this).unbind('click').click(function(){
  418. //添加设计图
  419. var imgUrl = $(this).data('imgurl');
  420. app.addImg(imgUrl);
  421. })
  422. })
  423. })
  424. }
  425. //屏幕变化的时候
  426. function windowResize(){
  427. $(window).resize(function(e){
  428. if(app.mainCanvas){
  429. app.baseWidth = $(".diy_canvas_wrap").width();
  430. app.baseHeight = $(".diy_canvas_wrap").height();
  431. //变换剪切区域
  432. app.cutArea(app.baseWidth,app.baseHeight,true);
  433. app.resizeCanvasSize();
  434. }
  435. })
  436. }
  437. //适应屏幕
  438. function niceWindow(){
  439. $("#diy_resize").click(function(){
  440. app.cutArea(app.baseWidth,app.baseHeight);
  441. app.resizeRender();
  442. })
  443. }
  444. //层级修改
  445. function changeHierarchy(){
  446. $("#view_ward .click_show_item").each(function(){
  447. $(this).click(function(){
  448. var key = $(this).data('key');
  449. switch(key)
  450. {
  451. case 0://上一层
  452. var obj = app.mainCanvas.getActiveObject();
  453. if(!obj||obj=='undefined'){return false;};
  454. app.mainCanvas.bringForward(obj);
  455. app.mainCanvas.renderAll();
  456. break;
  457. case 1: //下一层
  458. var obj = app.mainCanvas.getActiveObject();
  459. if(!obj||obj=='undefined'){return false;};
  460. app.mainCanvas.sendBackwards(obj);
  461. app.mainCanvas.renderAll();
  462. break;
  463. case 2: //置顶
  464. var obj = app.mainCanvas.getActiveObject();
  465. if(!obj||obj=='undefined'){return false;};
  466. app.mainCanvas.bringToFront(obj);
  467. app.mainCanvas.renderAll();
  468. break;
  469. default: //置底
  470. var obj = app.mainCanvas.getActiveObject();
  471. if(!obj||obj=='undefined'){return false;};
  472. app.mainCanvas.sendToBack(obj);
  473. app.mainCanvas.renderAll();
  474. }
  475. })
  476. })
  477. }
  478. //颜色遍历
  479. function getColor(){
  480. sdzAjax.getColor(function(data){
  481. var html = ''
  482. var divContent = '<div class="djsy-color-panel-item">{span}</div>'
  483. var spanContent = '<span data-color="{color}" style="background-color:{color}"></span>'
  484. for( var item in data.colorPanel){
  485. var span = ''
  486. for(var i=0;i<data.colorPanel[item].length;i++){
  487. span+=spanContent.replace(/{color}/g,data.colorPanel[item][i]);
  488. }
  489. html+=divContent.replace("{span}",span);
  490. }
  491. $("#djsy_text_color_panel_content").html(html);
  492. $("#djsy_text_color_panel_content .djsy-color-panel-item > span").each(function(){
  493. $(this).click(function(){
  494. var color = $(this).data('color');
  495. app.setActiveStyle('fill',color);
  496. $(".color-span >i").css('background-color',color);
  497. })
  498. })
  499. });
  500. }
  501. //预览效果
  502. function previewCanvas(){
  503. $("#diy_preview").click(function(){
  504. var imgUrl = app.returnCanvasImg(200);
  505. var previewCanvas = document.createElement('canvas');
  506. var ctx = previewCanvas.getContext("2d");
  507. var img = new Image();
  508. img.src = app.bottle.img_url;
  509. console.log(app.bottle);
  510. img.onload = function(){
  511. previewCanvas.width = img.width;
  512. previewCanvas.height = img.height;
  513. ctx.drawImage(img,0,0,img.width,img.height);
  514. var imgBotte = new Image();
  515. imgBotte.src = imgUrl;
  516. imgBotte.onload = function(){
  517. imgBotte.height = img.height*0.54;
  518. imgBotte.width = imgBotte.height*app.plateRATIO;
  519. ctx.drawImage(imgBotte,(img.width-imgBotte.width)/2,img.height*0.3,imgBotte.width,imgBotte.height);
  520. var imgBotteUrl = previewCanvas.toDataURL();
  521. $("#previewCanvas").show();
  522. var imgEle = '<img class="previewImg" src="'+ imgBotteUrl +'"/>';
  523. $("#previewImgWrap").html(imgEle);
  524. }
  525. }
  526. })
  527. }
  528. //关闭弹窗
  529. function close_modal(){
  530. $(".close_modal").click(function(){
  531. $(".GlobalModalsWrapper").hide();
  532. })
  533. }
  534. //保存diy
  535. function saveCanvas(){
  536. $("#diy_save_diy_btn").click(function(){
  537. var obj = app.mainCanvas._objects;
  538. if(obj.length<=0){
  539. alert("请添加内容");
  540. return false;
  541. }
  542. if(app.diyId){
  543. var data = {
  544. id :app.diyId,
  545. img_url:app.returnCanvasImg(200),
  546. content:JSON.stringify(app.returnCanvasJson())
  547. }
  548. }else{
  549. var data = {
  550. img_url:app.returnCanvasImg(200),
  551. content:JSON.stringify(app.returnCanvasJson()),
  552. bottle_id:app.bottle.id
  553. }
  554. }
  555. console.log(data);
  556. util.tipHtml('loading','正在提交');
  557. sdzAjax.saveDiy(data,function(data){
  558. util.hideTip();
  559. if(app.diyTitle){
  560. $("#diy_title").val(app.diyTitle);
  561. }
  562. if(app.diyRemark){
  563. $("#diy_remark").val(app.diyRemark);
  564. }
  565. if(app.use_number){
  566. $("#use_number").val(app.use_number);
  567. }
  568. $("#update_diy").show();
  569. var diyId = data.did;
  570. sdzAjax.editDiy(function(data){
  571. console.log(data);
  572. var div = '';
  573. $.each(data,function(k,val){
  574. //console.log(k)
  575. if(k=='festival_list'){
  576. div+=createHtml('节日',val,'festival_id',app.festival_id);
  577. }else if(k=='industry_list'){
  578. div+=createHtml('行业',val,'industry_id',app.industry_id);
  579. }else if(k=='purpose_list'){
  580. div+=createHtml('用途',val,'purpose_id',app.purpose_id);
  581. }else if(k=='style_list'){
  582. div+=createHtml('风格',val,'style_id',app.style_id);
  583. }else if(k=='subject_list'){
  584. div+=createHtml('专题',val,'subject_id',app.subject_id);
  585. }
  586. });
  587. $("#category_list").html(div);
  588. //保存
  589. $("#btn_update_diy").click(function(){
  590. var dataObj = {};
  591. dataObj.title = $("#diy_title").val();
  592. dataObj.remark = $("#diy_remark").val();
  593. dataObj.use_number= $("#use_number").val();
  594. dataObj.id = diyId;
  595. dataObj.subject_id = parseInt($("#subject_id").val());
  596. dataObj.festival_id = parseInt($("#festival_id").val());
  597. dataObj.purpose_id = parseInt($("#purpose_id").val());
  598. dataObj.style_id = parseInt($("#style_id").val());
  599. dataObj.industry_id = parseInt($("#industry_id").val());
  600. if(!dataObj.title){
  601. alert('请输入标题');
  602. return false;
  603. }
  604. if(!dataObj.remark){
  605. alert('请输入简介');
  606. return false;
  607. }
  608. if(!dataObj.use_number){
  609. alert('请输入使用次数');
  610. return false;
  611. }
  612. if(!dataObj.subject_id){
  613. alert('请选择专题');
  614. return false;
  615. }
  616. if(!dataObj.festival_id){
  617. alert('请选择节日');
  618. return false;
  619. }
  620. if(!dataObj.purpose_id){
  621. alert('请选择用途');
  622. return false;
  623. }
  624. if(!dataObj.style_id){
  625. alert('请选择风格');
  626. return false;
  627. }
  628. if(!dataObj.industry_id){
  629. alert('请输入行业');
  630. return false;
  631. }
  632. sdzAjax.upDateDiy(dataObj,function(data){
  633. //console.log(data);
  634. window.location.href = '/admin/diy';
  635. });
  636. });
  637. });
  638. });
  639. });
  640. }
  641. //返回html
  642. function createHtml(text,val,key,initVal){
  643. var html = '<div class="category_item clearfix">';
  644. html+='<span class="category_item_title">'+ text +'</span>';
  645. html+='<div class="category_item_content clearfix">';
  646. var childSpan = '<select class="form_input" id="'+ key +'">';
  647. for(var i=0;i<val.length;i++){
  648. if(initVal!='undefined'&&initVal == val[i].id){
  649. childSpan+='<option value="'+ val[i].id +'" selected="selected">' + val[i].title + '</option>';
  650. }else{
  651. childSpan+='<option value="'+ val[i].id +'">' + val[i].title + '</option>';
  652. }
  653. }
  654. childSpan+='</select>'
  655. html+=childSpan;
  656. html+='</div>'
  657. html+='</div>';
  658. return html;
  659. }
  660. //判断diy id 存在
  661. function hasDiyId(){
  662. var did = $("#diyId").val();
  663. if(did){
  664. $("#plate_input").remove();
  665. app.init(null,null,did);
  666. }else{
  667. //获取瓶子列表
  668. getBottle();
  669. $("#plate_input").show();
  670. }
  671. }
  672. //获取瓶子列表
  673. function getBottle(){
  674. sdzAjax.getBottle(function(res){
  675. console.log(res.bottle_list);
  676. var bottle_list = app.bottle_list = res.bottle_list;
  677. var html= '<select id="select_bottle" class="form_input">';
  678. html+='<option value="">-选择瓶子-</option>'
  679. for(var i = 0;i<bottle_list.length;i++){
  680. html += '<option value="'+ i +'">' + bottle_list[i].title + bottle_list[i].diy_width +'mm *'+bottle_list[i].diy_height + 'mm </option>';
  681. }
  682. html+='</select>';
  683. $("#select_bottle_content").html(html);
  684. });
  685. }
  686. $(document).ready(function(){
  687. $(".do-nicescroll").niceScroll();
  688. var mainCanvas = null;
  689. //判断有无diy
  690. hasDiyId();
  691. //新建画布
  692. $("#save_plate_btn").click(function(){
  693. var bottleVal = $("#select_bottle").val();
  694. console.log(bottleVal);
  695. if(!bottleVal){
  696. alert('请选择瓶子');
  697. return false;
  698. }
  699. app.bottle = app.bottle_list[bottleVal];
  700. app.init(app.bottle.diy_width,app.bottle.diy_height);
  701. $("#plate_input").hide();
  702. });
  703. //文字显示下拉
  704. $(".diy_top_nav_item").each(function(){
  705. $(this).click(function(){
  706. if($(this).hasClass('active')){
  707. $(".diy_top_nav_item").removeClass('active');
  708. }else{
  709. $(".diy_top_nav_item").removeClass('active');
  710. $(this).addClass('active');
  711. }
  712. })
  713. });
  714. //添加文字
  715. $("#add_text_btn").click(function(){
  716. var title=prompt("请输入内容","这是标题");
  717. if (title!=null && title!=""){
  718. app.addText(title);
  719. }
  720. })
  721. //上传图片
  722. $("#diy_bg_img_file").change(function(){
  723. var imgVal = $(this).val();
  724. if(!imgVal){
  725. return false;
  726. }
  727. var formData = new FormData($("#diy_bg_img_form")[0]);
  728. formData.append("image", imgVal);
  729. sdzAjax.uploadImg(formData,function(data){
  730. app.addImg(data.img_url);
  731. $("#diy_bg_img_file").val('');
  732. });
  733. });
  734. //设置字体
  735. $("#changeFont > li").each(function(){
  736. $(this).click(function(){
  737. var fontVal = $(this).data('val');
  738. app.setActiveStyle('fontFamily',fontVal);
  739. $("#showFont").text(fontVal);
  740. var className = $(this).find('div')[0].className;
  741. $("#showFont").removeClass().addClass(className);
  742. });
  743. });
  744. //获取分类
  745. getMaterialType();
  746. //改变分类
  747. changeSelectMaterialType();
  748. //屏幕变化的时候
  749. windowResize();
  750. //自适应屏幕
  751. niceWindow();
  752. //改变元素的层级
  753. changeHierarchy();
  754. //复制
  755. app.cloneObject();
  756. //删除
  757. app.delObject();
  758. //设置颜色
  759. getColor()
  760. // 返回预览图
  761. previewCanvas();
  762. //关闭弹窗
  763. close_modal();
  764. //保存
  765. saveCanvas();
  766. });
  767. })(jQuery)