core.js 30 KB

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