core.js 32 KB

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