core.js 33 KB

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