core.js 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. /* ================================================================================
  2. * dever.js v1.0
  3. * http://git.shemic.com/dever/script
  4. * ================================================================================
  5. * Copyright 2017-2018 Dever(dever.cc)
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * ================================================================================
  19. */
  20. //<filter><script src="../script/lib/jquery/jquery.min.js"></script><script src="../script/dever/core.js"></script></filter>
  21. $(function()
  22. {
  23. Dever.Init();
  24. });
  25. //公共类库
  26. var Dever =
  27. {
  28. countdown : -1,
  29. modal : false,
  30. Init : function()
  31. {
  32. this.Import();
  33. this.Page().Init();
  34. this.User().Init();
  35. this.Editor().Init();
  36. this.Upload().Init();
  37. this.Change();
  38. this.LoadPage();
  39. this.Extend();
  40. //this.Target();
  41. //this.Pjax();
  42. }
  43. ,Pjax : function()
  44. {
  45. $(document).pjax('a', '.container');
  46. //$.pjax.reload('.container');
  47. }
  48. ,Page : function()
  49. {
  50. return _Dever_Page;
  51. }
  52. ,Template : function()
  53. {
  54. return _Dever_Template;
  55. }
  56. ,Editor : function()
  57. {
  58. return _Dever_Editor;
  59. }
  60. ,User : function()
  61. {
  62. return _Dever_User;
  63. }
  64. ,Modal : function()
  65. {
  66. return new _Dever_Modal();
  67. }
  68. ,Upload : function()
  69. {
  70. return _Dever_Upload;
  71. }
  72. ,Proxy : function(uri)
  73. {
  74. return config.proxy + 'proxy_method=' + encodeURIComponent(uri);
  75. }
  76. ,Jump : function(url)
  77. {
  78. location.href = url;
  79. }
  80. ,BackRun : function(url)
  81. {
  82. $.post(url);
  83. }
  84. ,Reload : function(e)
  85. {
  86. e.attr('src', e.attr('src'));
  87. }
  88. ,Target : function()
  89. {
  90. var link = config.host;
  91. $("a").each(function() {
  92. if (!$(this).attr('onclick')) {
  93. var url = $(this).attr('href');
  94. if (url && url.indexOf(link) == -1) {
  95. //$(this).attr('target', '_blank');
  96. }
  97. }
  98. })
  99. }
  100. ,Time : function(e, time, text)
  101. {
  102. var self = this;
  103. if (this.countdown < 0) {
  104. this.countdown = time;
  105. }
  106. if (this.countdown == 0) {
  107. e.attr("disabled", false);
  108. e.html(text);
  109. self.Init();
  110. this.countdown = time;
  111. } else {
  112. e.attr("disabled", true);
  113. e.html(this.countdown + 's后' + text);
  114. this.countdown--;
  115. e.unbind('click');
  116. setTimeout(function() {
  117. self.Time(e, time, text)
  118. },1000)
  119. }
  120. }
  121. ,Import : function()
  122. {
  123. var self = this;
  124. var include = $("include");
  125. var count = include.length-1;
  126. if (include.length) {
  127. include.each(function(i){
  128. var dom = $(this);
  129. var system = dom.attr('system');
  130. var path = dom.attr('path');
  131. var name = dom.attr('file');
  132. var file = '';
  133. if(path) file = path;
  134. if(system) file = file + '/' + system;
  135. file += name + '.html';
  136. dom.load(file, function(){
  137. $("html").find("include").eq(i).get(0).outerHTML = dom.html();
  138. if (i == count) {
  139. self.Template().Init();
  140. }
  141. });
  142. })
  143. } else {
  144. self.Template().Init();
  145. }
  146. }
  147. ,LoadPage : function()
  148. {
  149. if ($('.dever-loadpage').length) {
  150. $('.dever-loadpage').each(function() {
  151. var url = $(this).attr('dever-url');
  152. var self = $(this);
  153. $.getJSON(Dever.CallUrl(url), function(t) {
  154. self.html(t.msg);
  155. _Dever_Page.Ajax(self);
  156. });
  157. });
  158. }
  159. }
  160. ,CallUrl : function(url)
  161. {
  162. if (url.indexOf('callback') == -1) {
  163. if (url.indexOf('=') != -1) {
  164. url += '&json=1&callback=?';
  165. } else {
  166. url += '?json=1&callback=?';
  167. }
  168. }
  169. return url;
  170. }
  171. ,Change : function()
  172. {
  173. if ($('.dever-change').length) {
  174. $('.dever-change').change(function() {
  175. Dever.Jump($(this).attr('dever-change')+$(this).val());
  176. });
  177. //$('.dever-change').val($('.dever-change').attr('dever-value'));
  178. }
  179. }
  180. ,Host : function(host, uri)
  181. {
  182. var result = '';
  183. uri = uri ? uri : '';
  184. result = config.host.replace('www', host);
  185. result = result.replace('main', host);
  186. if (config.proxy) {
  187. return this.Proxy(host + '/' + uri);
  188. }
  189. return result + uri;
  190. }
  191. // 输出数据,提示框
  192. ,Out : function(content, callback, title)
  193. {
  194. if (content && content.indexOf('登录') != -1 && $("#login_url").length) {
  195. var href = $("#login_url").val();
  196. location.href = href;
  197. return;
  198. } else {
  199. if (this.modal) {
  200. this.modal.Alert(content, {'yes':callback, 'no':callback}, title);
  201. } else {
  202. this.Modal().Alert(content, {'yes':callback, 'no':callback}, title);
  203. }
  204. }
  205. }
  206. // 将数据解析,并进行下一步操作
  207. ,Msg : function(result, error_callback, success_callback)
  208. {
  209. if (result.status == 2) {
  210. if (result.data) {
  211. if (result.data.element) {
  212. if (result.data.attr) {
  213. $(result.data.element).attr(result.data.attr, result.data.value);
  214. } else {
  215. $(result.data.element).val(result.data.value);
  216. }
  217. } else if (result.data.url) {
  218. location.href = result.data.url;
  219. }
  220. }
  221. if (error_callback) {
  222. return error_callback(result.msg);
  223. }
  224. this.Out(result.msg);
  225. } else {
  226. if (typeof(result.data) != 'object') {
  227. result.msg = result.data;
  228. }
  229. if (success_callback) {
  230. return success_callback(result.msg);
  231. }
  232. if (result.msg.indexOf('http://') != -1) {
  233. this.Jump(result.msg);
  234. } else if (parseInt(result.msg) > 0) {
  235. this.Out('操作成功', function() {
  236. location.reload();
  237. });
  238. } else {
  239. this.Out(result.msg);
  240. }
  241. }
  242. }
  243. ,Confirm : function(content, callback, title)
  244. {
  245. if (this.modal) {
  246. this.modal.Confirm(content, {'yes':callback}, title);
  247. } else {
  248. this.Modal().Confirm(content, {'yes':callback}, title);
  249. }
  250. }
  251. ,Extend : function()
  252. {
  253. Array.prototype.indexIn = function(val) {
  254. for (var i = 0; i < this.length; i++) {
  255. if (this[i] == val) return i;
  256. }
  257. return -1;
  258. };
  259. Array.prototype.remove = function(val) {
  260. var index = this.indexIn(val);
  261. if (index > -1) {
  262. this.splice(index, 1);
  263. }
  264. };
  265. $("select option").each(function() {
  266. if ($(this).attr('select') && $(this).attr('select') == 'true') {
  267. $(this).attr('selected', 'true');
  268. } else {
  269. $(this).removeAttr('selected');
  270. }
  271. });
  272. }
  273. ,Ucfirst : function(str)
  274. {
  275. var str = str.toLowerCase();
  276. var strarr = str.split(' ');
  277. var result = '';
  278. for (var i in strarr){
  279. result += strarr[i].substring(0,1).toUpperCase()+strarr[i].substring(1)+' ';
  280. }
  281. return result;
  282. }
  283. };
  284. //瀑布流分页
  285. var _Dever_Page =
  286. {
  287. name : '',
  288. loading : '',
  289. Init : function()
  290. {
  291. if ($(this.name).length) {
  292. var self = this;
  293. $(window).scroll(function() {
  294. if ($(window).scrollTop() >= $(document).height() - $(window).height()) {
  295. self.Start(self.name, self.loading);
  296. }
  297. });
  298. }
  299. }
  300. ,Start : function(page, loading)
  301. {
  302. page = page ? page : this.name;
  303. loading = loading ? loading : this.loading;
  304. if ($(page).length) {
  305. $(page).hide();
  306. var url = $(page).attr('dever-page-link');
  307. if (url) {
  308. var key = $(page).attr('dever-page-list');
  309. if ($(loading).length) {
  310. $(loading).show();
  311. }
  312. $.get(url, function(t) {
  313. t = '<div>' + t + '</div>';
  314. var c = $(t).find(key).html();
  315. if ($(loading).length) {
  316. $(loading).hide();
  317. }
  318. if (c) {
  319. $(key).append(c);
  320. var l = $(t).find(page).attr('dever-page-link');
  321. $(page).attr('dever-page-link', l);
  322. }
  323. });
  324. }
  325. }
  326. }
  327. ,Ajax : function(e)
  328. {
  329. var self = this;
  330. e.find('a').each(function() {
  331. var url = $(this).attr('href');
  332. $(this).attr('href', 'javascript:;');
  333. $(this).unbind('click').bind('click', function() {
  334. $.get(url.replace('callback', ''), function(t) {
  335. e.html(t);
  336. self.Ajax(e);
  337. });
  338. });
  339. });
  340. }
  341. }
  342. //模态框 非静态类
  343. var _Dever_Modal = function()
  344. {
  345. var self = this;
  346. self.state = false;
  347. self.html = '';
  348. self.box = '';
  349. self.title = '';
  350. self.content = '';
  351. self.yes = '';
  352. self.no = '';
  353. self.setting = '';
  354. self.confirm = '';
  355. self.open = false;
  356. self.Init = function()
  357. {
  358. this.state = false;
  359. if (this.html) {
  360. $('body').append(this.html);
  361. }
  362. if (this.box && this.content) {
  363. this.box = $(this.box);
  364. if (this.box.length) {
  365. this.box.hide();
  366. this.content = $(this.content);
  367. if(this.title) this.title = $(this.title);
  368. if(this.yes) this.yes = $(this.yes);
  369. if(this.no) this.no = $(this.no);
  370. if(this.setting) this.setting = $(this.setting);
  371. if(this.confirm) this.confirm = $(this.confirm);
  372. if(!this.timeout) this.timeout = 3000;
  373. this.state = true;
  374. }
  375. }
  376. };
  377. self.Alert = function(content, callback, title)
  378. {
  379. if (self.open == true) {
  380. return;
  381. }
  382. self.open = true;
  383. if (this.state == false) {
  384. alert(content);
  385. if (callback) {
  386. callback.yes();
  387. }
  388. } else {
  389. if (this.setting.length && this.setting.html()) {
  390. if (this.confirm) this.confirm.hide();
  391. this.setting.show();
  392. } else {
  393. if (this.setting) this.setting.hide();
  394. if (this.confirm) this.confirm.show();
  395. }
  396. this.content.html(content);
  397. if (title) {
  398. this.title.html(title);
  399. }
  400. this.box.show();
  401. if (this.yes && callback.yes) {
  402. this.Bind(callback);
  403. } else {
  404. this.TimeOut(callback);
  405. }
  406. }
  407. };
  408. self.Confirm = function(content, callback, title)
  409. {
  410. if (self.open == true) {
  411. return;
  412. }
  413. self.open = true;
  414. if (this.state == false) {
  415. if (confirm(content)) {
  416. callback.yes();
  417. }
  418. } else {
  419. if (this.confirm) this.confirm.show();
  420. if (this.setting) this.setting.hide();
  421. this.content.html(content);
  422. if (title) {
  423. this.title.html(title);
  424. }
  425. this.box.show();
  426. if (callback.yes) {
  427. this.Bind(callback);
  428. }
  429. }
  430. }
  431. self.Bind = function(callback)
  432. {
  433. if (this.yes.length && callback.yes) {
  434. this.yes.unbind('click').bind('click', callback.yes);
  435. }
  436. if (this.no.length) {
  437. if (callback.no) {
  438. this.no.unbind('click').bind('click', callback.no);
  439. } else {
  440. var self = this;
  441. this.no.unbind('click').bind('click', function()
  442. {
  443. self.Close();
  444. });
  445. }
  446. }
  447. };
  448. self.TimeOut = function(callback)
  449. {
  450. var self = this;
  451. setTimeout(function()
  452. {
  453. if (callback && callback.yes) {
  454. callback.yes();
  455. } else {
  456. self.Close();
  457. }
  458. }, self.timeout);
  459. }
  460. self.Close = function()
  461. {
  462. this.open = false;
  463. this.box.hide();
  464. }
  465. return self;
  466. };
  467. //用户相关 静态类
  468. var _Dever_User =
  469. {
  470. config : [],
  471. uid : -1,
  472. name : '匿名用户',
  473. //按钮不可点击的样式
  474. disable : '',
  475. Init : function()
  476. {
  477. var self = this;
  478. if (this.config.click) {
  479. for (var i in this.config.click) {
  480. var e = self.config.click[i];
  481. var c = 1;
  482. if (e.indexOf('|') != -1) {
  483. var t = e.split('|');
  484. e = t[0];
  485. c = 2;
  486. }
  487. var e = $(e);
  488. if (e.length) {
  489. e.attr('data-i', i);
  490. e.attr('data-c', c);
  491. if (self.disable) {
  492. e.addClass(self.disable);
  493. self.DisableCheck(e);
  494. } else {
  495. self.Bind(e);
  496. }
  497. }
  498. }
  499. }
  500. }
  501. ,Bind : function(e)
  502. {
  503. var self = this;
  504. e.unbind('click').bind('click', function() {
  505. if ($(this).attr('data-c') == 2) {
  506. Dever.Confirm(function() {
  507. self.Save($(this), config);
  508. });
  509. } else {
  510. self.Save($(this), config);
  511. }
  512. });
  513. }
  514. ,DisableCheck : function(e)
  515. {
  516. var self = this;
  517. var key = e.attr('data-i');
  518. var config = self.config[key];
  519. var callback = function(msg,state) {
  520. e.addClass(self.disable);
  521. }
  522. var blur = function() {
  523. var send = self.Check(e, callback, config);
  524. if (!send) {
  525. e.addClass(self.disable);
  526. return;
  527. }
  528. e.removeClass(self.disable);
  529. self.Bind(e);
  530. }
  531. var send = self.Check(e, callback, config, blur);
  532. if (!send) {
  533. callback(config.error);
  534. return;
  535. }
  536. e.removeClass(self.disable);
  537. self.Bind(e);
  538. }
  539. ,Check : function(e, callback, config, blur)
  540. {
  541. var self = this;
  542. var send = {};
  543. if (typeof(config.input) != 'undefined') {
  544. if (typeof(config.input) == 'string' && config.input == 'parent') {
  545. if (e.attr('dever-send')) {
  546. send[e.attr('dever-send')] = e.attr('dever-' + e.attr('dever-send'));
  547. }
  548. send.id = e.attr('dever-id');
  549. send.value = e.parent().find('input').val();
  550. if (!send.id || !send.value) {
  551. callback(config.error);
  552. return false;
  553. }
  554. } else if (typeof(config.input) == 'string' && config.input == 'click') {
  555. if (e.attr('dever-send')) {
  556. var a = e.attr('dever-send').split('&');
  557. for (var i in a) {
  558. if (typeof(a[i]) == 'string') {
  559. var b = a[i].split('=');
  560. send[b[0]] = b[1];
  561. }
  562. }
  563. }
  564. } else if (typeof(config.input) == 'object') {
  565. if (blur) {
  566. for (var i in config.input) {
  567. var input = config.input[i];
  568. if (typeof(input) == 'string') {
  569. if (input.indexOf('|') != -1) {
  570. var t = input.split('|');
  571. input = t[0];
  572. }
  573. var el = $(input);
  574. el.on('input',function()
  575. {
  576. blur();
  577. });
  578. el.blur(function()
  579. {
  580. blur();
  581. });
  582. }
  583. }
  584. }
  585. for (var i in config.input) {
  586. var option = false;
  587. var input = config.input[i];
  588. if (typeof(input) == 'object' || typeof(input) == 'function') {
  589. send[i] = input();
  590. } else {
  591. var select = [];
  592. if (input.indexOf('|') != -1) {
  593. var t = input.split('|');
  594. input = t[0];
  595. option = true;
  596. if (t[1]) {
  597. select = t;
  598. }
  599. }
  600. var el = $(input);
  601. //e.get(0).tagName == 'TEXTAREA'
  602. send[i] = el.val();
  603. if (option == false) {
  604. state = self.Match(el, send[i], callback, config);
  605. if (!state) {
  606. return state;
  607. }
  608. } else if(select) {
  609. //只要有一个为真即可
  610. var result = false;
  611. if (select.length <= 0) {
  612. result = true;
  613. }
  614. for (var i in select) {
  615. if (typeof(select[i]) == 'string') {
  616. var el = $(select[i]);
  617. state = self.Match($(el), $(el).val(), false, config);
  618. if (state) {
  619. result = true;
  620. }
  621. }
  622. }
  623. if (result == false) {
  624. return result;
  625. }
  626. }
  627. }
  628. }
  629. }
  630. }
  631. return send;
  632. }
  633. ,Match : function(e, value, callback, config)
  634. {
  635. var m = true;
  636. if (value && e.attr('dever-match')) {
  637. var r = new RegExp(e.attr('dever-match'));
  638. m = r.test(value);
  639. }
  640. if (value && e.attr('dever-value')) {
  641. m = $(e.attr('dever-value')).val() == value;
  642. }
  643. if (!value || m != true) {
  644. if (callback) {
  645. var error = e.attr('dever-error') ? e.attr('dever-error') : config.error;
  646. callback(error);
  647. }
  648. return false;
  649. }
  650. return true;
  651. }
  652. //通用的保存数据功能
  653. ,Save : function(e, setting)
  654. {
  655. var self = this;
  656. var key = e.attr('data-i');
  657. var config = self.config[key];
  658. if (config.start) {
  659. config.start.call();
  660. }
  661. var callback = function(msg,state) {
  662. if (config.status && $(config.status).length) {
  663. //$(config.status).html(msg).css('visibility','initial');
  664. state ? $(config.status).html(msg).hide() : $(config.status).html(msg).show();
  665. } else if(!state) {
  666. Dever.Out(msg);
  667. }
  668. }
  669. var send = self.Check(e, callback, config);
  670. if (!send) {
  671. callback(config.error);
  672. return;
  673. }
  674. if (!config.url) {
  675. config.url = e.attr('dever-send');
  676. }
  677. if (e.attr('dever-refresh')) {
  678. config.url = e.attr('dever-refresh');
  679. }
  680. callback(1,1);
  681. config.url = Dever.CallUrl(config.url);
  682. if (setting.proxy) {
  683. $.post(config.url, send, function(t) {
  684. t = eval('(' + t + ')');
  685. Dever.Msg(t, callback, (config.callback ? function(msg){config.callback(e,msg,t)} : false));
  686. });
  687. } else {
  688. $.getJSON(config.url, send, function(t) {
  689. Dever.Msg(t, callback, (config.callback ? function(msg){config.callback(e,msg,t)} : false));
  690. });
  691. }
  692. }
  693. };
  694. //编辑器相关
  695. var _Dever_Editor =
  696. {
  697. state : false,
  698. editors : [],
  699. Init : function()
  700. {
  701. if (typeof marked == 'function') {
  702. this.state = true;
  703. } else {
  704. this.state = false;
  705. }
  706. this.SetMark();
  707. var self = this;
  708. if ($('.dever-note').length) {
  709. $('.dever-note').each(function() {
  710. self.InitEditor($(this));
  711. })
  712. }
  713. }
  714. ,InitEditor : function(e)
  715. {
  716. var id = e.attr('id');
  717. var key = e.attr('key');
  718. if (config.uid && config.uid > 0) {
  719. var toolbar = [
  720. 'title'
  721. ,'bold'
  722. ,'italic'
  723. ,'underline'
  724. ,'strikethrough'
  725. ,'fontScale'
  726. ,'color'
  727. ,'ol'
  728. ,'ul'
  729. ,'blockquote'
  730. ,'code'
  731. ,'table'
  732. ,'link'
  733. ,'image'
  734. ,'hr'
  735. ,'indent'
  736. ,'outdent'
  737. ,'alignment'
  738. //,'emoji'
  739. ];
  740. } else {
  741. var toolbar = [
  742. 'title'
  743. ,'bold'
  744. ,'fontScale'
  745. ,'color'
  746. ,'ol'
  747. ,'ul'
  748. ,'blockquote'
  749. ,'indent'
  750. ,'outdent'
  751. ,'alignment'
  752. ];
  753. }
  754. if (typeof(this.editors[id]) == "undefined") {
  755. this.editors[id] = new Simditor({
  756. textarea: e,
  757. upload:
  758. {
  759. url: config.upload + '.simditor',
  760. params: {key:key},
  761. fileKey: 'file',
  762. connectionCount: 10,
  763. leaveConfirm: 'Uploading is in progress, are you sure to leave this page?'
  764. },
  765. toolbar : toolbar
  766. /*
  767. ,emoji:
  768. {
  769. imagePath: config.lib + 'simditor/plugins/emoji/images/emoji/'
  770. }
  771. */
  772. });
  773. }
  774. }
  775. ,SetMark : function()
  776. {
  777. return;
  778. }
  779. ,GetMark : function(content)
  780. {
  781. if (this.state == true) {
  782. return marked(content);
  783. }
  784. return content;
  785. }
  786. };
  787. //模板
  788. var _Dever_Template =
  789. {
  790. state : false,
  791. method : '',
  792. element : '',
  793. data : '',
  794. param : '',
  795. attr : '',
  796. Init : function()
  797. {
  798. if (this.Check()) {
  799. var url = config.proxyTemplateUrl;
  800. var send = {};
  801. var parsing = [];
  802. $("dever").each(function(){
  803. parsing.push($(this).html());
  804. $(this).remove();
  805. });
  806. send.url = location.href;
  807. send.parsing = parsing.join('');
  808. send.html = $("html").html();
  809. //$("html").html('dever template loading...');
  810. $.ajax({
  811. type: "POST",
  812. url: url,
  813. data: send,
  814. dataType: "json",
  815. success: function(result){
  816. $("html").html(result.data);
  817. }
  818. });
  819. }
  820. }
  821. ,Check : function()
  822. {
  823. if ($("dever").length) {
  824. this.state = true;
  825. } else {
  826. this.state = false;
  827. }
  828. return this.state;
  829. }
  830. }
  831. //上传 请载入layui
  832. var _Dever_Upload =
  833. {
  834. upload : [],
  835. upload_file : [],
  836. upload_pic : [],
  837. callback : {},
  838. Init : function()
  839. {
  840. var self = this;
  841. if ($(".dever-upload-drag").length) {
  842. $(".dever-upload-drag").each(function() {
  843. var key = $(this).attr('key');
  844. if (config.proxy) {
  845. var url = Dever.Proxy('upload/save.drag') + '&key=' + key;
  846. } else {
  847. var url = config.upload + '.drag?key='+ key;
  848. }
  849. $(this).inlineattachment({
  850. uploadUrl: url,
  851. progressText: '![文件上传中...]()',
  852. urlText: "![文件描述]({filename})\n",
  853. errorText: '上传失败'
  854. });
  855. });
  856. }
  857. if ($(".dever-upload").length) {
  858. $(".dever-upload").each(function(i) {
  859. self.Load(i,$(this));
  860. })
  861. }
  862. }
  863. ,Load : function(i,e)
  864. {
  865. var self = this;
  866. var id = e.attr('id');
  867. var value = e.attr('dever-upload-value');
  868. var key = e.attr('dever-upload-key');
  869. var type = e.attr('dever-upload-type');
  870. var callback = e.attr('dever-upload-callback');
  871. var input = $("#" + value);
  872. if (!input.length) {
  873. return;
  874. }
  875. var url = config.upload + '.start';
  876. if (typeof(this.upload[value]) != "undefined") {
  877. return;
  878. }
  879. if (!key) {
  880. key = 1;
  881. }
  882. self.upload[value] = true;
  883. self.upload_pic[value] = [];
  884. self.upload_file[value] = [];
  885. var input_value = input.val();
  886. if (input_value) {
  887. var input_value_array = input_value.split(',');
  888. for (var i in input_value_array) {
  889. self.Set(type, e, input, value, callback, input_value_array[i]);
  890. }
  891. }
  892. layui.use(['upload','layer'], function() {
  893. var layer = layui.layer;
  894. var layuiUpload = layui.upload;
  895. var uploadInst = layuiUpload.render({
  896. elem: '#' + id
  897. ,data: {'key' : key}
  898. ,field: 'file'
  899. ,url: url
  900. ,multiple: true
  901. ,before: function(obj) {
  902. layer.load();
  903. }
  904. ,done: function(data) {
  905. layer.closeAll('loading');
  906. if (data.status == 1) {
  907. self.Set(type, e, input, value, callback, data.url);
  908. } else {
  909. layer.msg(data.msg, {icon: 4});
  910. return false;
  911. }
  912. }
  913. ,error: function() {
  914. layer.closeAll('loading');
  915. layer.msg('上传失败', {icon: 4});
  916. return false;
  917. }
  918. });
  919. });
  920. }
  921. ,Set : function(type, e, input, value, callback, url)
  922. {
  923. if (type == 'mul') {
  924. this.SetMul(e, input, value, callback, url);
  925. } else {
  926. this.SetOne(e, input, value, callback, url);
  927. }
  928. }
  929. ,SetOne : function(e, input, value, callback, url)
  930. {
  931. var html = '';
  932. if (this.callback && this.callback[callback]) {
  933. html = this.callback[callback].call(this, e, url, 'dever-upload-close');
  934. } else {
  935. html = '<img src="'+url+'" class="pics">';
  936. }
  937. if (html) {
  938. e.html(html);
  939. }
  940. var input_value = input.val();
  941. if (input_value != url) {
  942. input.val(url);
  943. this.Blur(input);
  944. }
  945. }
  946. ,SetMul : function(e, input, value, callback, url)
  947. {
  948. var html = '';
  949. var input_value = input.val();
  950. if (this.upload_pic[value].length <= 0 && input_value) {
  951. this.upload_pic[value] = input_value.split(',');
  952. }
  953. if (this.callback && this.callback[callback]) {
  954. html += this.callback[callback].call(this, e, url, 'dever-upload-close');
  955. } else {
  956. html += '<li><img src="'+url+'" class="pics"><em class="dever-upload-close"></em></li>';
  957. }
  958. if (html) {
  959. e.before(html);
  960. }
  961. this.Close(value);
  962. if (input_value.indexOf(url) == -1) {
  963. this.upload_pic[value].push(url);
  964. input.val(this.upload_pic[value].join(','));
  965. this.Blur(input);
  966. }
  967. }
  968. ,Blur : function(input)
  969. {
  970. var events = $._data(input[0], 'events');
  971. if (events && events["blur"]) {
  972. input.blur();
  973. }
  974. }
  975. ,Close : function(v)
  976. {
  977. var self = this;
  978. $('.dever-upload-close').each(function()
  979. {
  980. $(this).unbind('click').bind('click', function() {
  981. var e = $(this).parent();
  982. var p = e.find('img').attr('src');
  983. e.remove();
  984. self.upload_pic[v].remove(p);
  985. $("#" + v).val(self.upload_pic[v].join(','));
  986. });
  987. });
  988. }
  989. }