core.js 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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. init();
  340. }
  341. });
  342. }
  343. }
  344. }
  345. ,Ajax : function(e)
  346. {
  347. var self = this;
  348. e.find('a').each(function() {
  349. var url = $(this).attr('href');
  350. $(this).attr('href', 'javascript:;');
  351. $(this).unbind('click').bind('click', function() {
  352. $.get(url.replace('callback', ''), function(t) {
  353. e.html(t);
  354. self.Ajax(e);
  355. });
  356. });
  357. });
  358. }
  359. }
  360. //模态框 非静态类
  361. var _Dever_Modal = function()
  362. {
  363. var self = this;
  364. self.state = false;
  365. self.html = '';
  366. self.box = '';
  367. self.title = '';
  368. self.content = '';
  369. self.yes = '';
  370. self.no = '';
  371. self.setting = '';
  372. self.confirm = '';
  373. self.open = false;
  374. self.Init = function()
  375. {
  376. this.state = false;
  377. if (this.html) {
  378. $('body').append(this.html);
  379. }
  380. if (this.box && this.content) {
  381. this.box = $(this.box);
  382. if (this.box.length) {
  383. this.box.hide();
  384. this.content = $(this.content);
  385. if(this.title) this.title = $(this.title);
  386. if(this.yes) this.yes = $(this.yes);
  387. if(this.no) this.no = $(this.no);
  388. if(this.setting) this.setting = $(this.setting);
  389. if(this.confirm) this.confirm = $(this.confirm);
  390. if(!this.timeout) this.timeout = 3000;
  391. this.state = true;
  392. }
  393. }
  394. };
  395. self.Alert = function(content, callback, title)
  396. {
  397. if (self.open == true) {
  398. return;
  399. }
  400. self.open = true;
  401. if (this.state == false) {
  402. alert(content);
  403. if (callback) {
  404. callback.yes();
  405. }
  406. } else {
  407. if (this.setting.length && this.setting.html()) {
  408. if (this.confirm) this.confirm.hide();
  409. this.setting.show();
  410. } else {
  411. if (this.setting) this.setting.hide();
  412. if (this.confirm) this.confirm.show();
  413. }
  414. this.content.html(content);
  415. if (title) {
  416. this.title.html(title);
  417. }
  418. this.box.show();
  419. if (this.yes && callback.yes) {
  420. this.Bind(callback);
  421. } else {
  422. this.TimeOut(callback);
  423. }
  424. }
  425. };
  426. self.Confirm = function(content, callback, title)
  427. {
  428. if (self.open == true) {
  429. return;
  430. }
  431. self.open = true;
  432. if (this.state == false) {
  433. if (confirm(content)) {
  434. callback.yes();
  435. }
  436. } else {
  437. if (this.confirm) this.confirm.show();
  438. if (this.setting) this.setting.hide();
  439. this.content.html(content);
  440. if (title) {
  441. this.title.html(title);
  442. }
  443. this.box.show();
  444. if (callback.yes) {
  445. this.Bind(callback);
  446. }
  447. }
  448. }
  449. self.Bind = function(callback)
  450. {
  451. if (this.yes.length && callback.yes) {
  452. this.yes.unbind('click').bind('click', callback.yes);
  453. }
  454. if (this.no.length) {
  455. if (callback.no) {
  456. this.no.unbind('click').bind('click', callback.no);
  457. } else {
  458. var self = this;
  459. this.no.unbind('click').bind('click', function()
  460. {
  461. self.Close();
  462. });
  463. }
  464. }
  465. };
  466. self.TimeOut = function(callback)
  467. {
  468. var self = this;
  469. setTimeout(function()
  470. {
  471. if (callback && callback.yes) {
  472. callback.yes();
  473. } else {
  474. self.Close();
  475. }
  476. }, self.timeout);
  477. }
  478. self.Close = function()
  479. {
  480. this.open = false;
  481. this.box.hide();
  482. }
  483. return self;
  484. };
  485. //用户相关 静态类
  486. var _Dever_User =
  487. {
  488. config : [],
  489. uid : -1,
  490. name : '匿名用户',
  491. //按钮不可点击的样式
  492. disable : '',
  493. Init : function()
  494. {
  495. var self = this;
  496. if (this.config.click) {
  497. for (var i in this.config.click) {
  498. var e = self.config.click[i];
  499. var c = 1;
  500. if (e.indexOf('|') != -1) {
  501. var t = e.split('|');
  502. e = t[0];
  503. c = 2;
  504. }
  505. var e = $(e);
  506. if (e.length) {
  507. e.attr('data-i', i);
  508. e.attr('data-c', c);
  509. if (self.disable) {
  510. e.addClass(self.disable);
  511. self.DisableCheck(e);
  512. } else {
  513. self.Bind(e);
  514. }
  515. }
  516. }
  517. }
  518. }
  519. ,Bind : function(e)
  520. {
  521. var self = this;
  522. e.unbind('click').bind('click', function() {
  523. if ($(this).attr('data-c') == 2) {
  524. Dever.Confirm(function() {
  525. self.Save($(this), config);
  526. });
  527. } else {
  528. self.Save($(this), config);
  529. }
  530. });
  531. }
  532. ,DisableCheck : function(e)
  533. {
  534. var self = this;
  535. var key = e.attr('data-i');
  536. var config = self.config[key];
  537. var callback = function(msg,state) {
  538. e.addClass(self.disable);
  539. }
  540. var blur = function() {
  541. var send = self.Check(e, callback, config);
  542. if (!send) {
  543. e.addClass(self.disable);
  544. return;
  545. }
  546. e.removeClass(self.disable);
  547. self.Bind(e);
  548. }
  549. var send = self.Check(e, callback, config, blur);
  550. if (!send) {
  551. callback(config.error);
  552. return;
  553. }
  554. e.removeClass(self.disable);
  555. self.Bind(e);
  556. }
  557. ,Check : function(e, callback, config, blur)
  558. {
  559. var self = this;
  560. var send = {};
  561. if (typeof(config.input) != 'undefined') {
  562. if (typeof(config.input) == 'string' && config.input == 'parent') {
  563. if (e.attr('dever-send')) {
  564. send[e.attr('dever-send')] = e.attr('dever-' + e.attr('dever-send'));
  565. }
  566. send.id = e.attr('dever-id');
  567. send.value = e.parent().find('input').val();
  568. if (!send.id || !send.value) {
  569. callback(config.error);
  570. return false;
  571. }
  572. } else if (typeof(config.input) == 'string' && config.input == 'click') {
  573. if (e.attr('dever-send')) {
  574. var a = e.attr('dever-send').split('&');
  575. for (var i in a) {
  576. if (typeof(a[i]) == 'string') {
  577. var b = a[i].split('=');
  578. send[b[0]] = b[1];
  579. }
  580. }
  581. }
  582. } else if (typeof(config.input) == 'object') {
  583. if (blur) {
  584. for (var i in config.input) {
  585. var input = config.input[i];
  586. if (typeof(input) == 'string') {
  587. if (input.indexOf('|') != -1) {
  588. var t = input.split('|');
  589. input = t[0];
  590. }
  591. var el = $(input);
  592. el.on('input',function()
  593. {
  594. blur();
  595. });
  596. el.blur(function()
  597. {
  598. blur();
  599. });
  600. }
  601. }
  602. }
  603. for (var i in config.input) {
  604. var option = false;
  605. var input = config.input[i];
  606. if (typeof(input) == 'object' || typeof(input) == 'function') {
  607. send[i] = input();
  608. } else {
  609. var select = [];
  610. if (input.indexOf('|') != -1) {
  611. var t = input.split('|');
  612. input = t[0];
  613. option = true;
  614. if (t[1]) {
  615. select = t;
  616. }
  617. }
  618. var el = $(input);
  619. //e.get(0).tagName == 'TEXTAREA'
  620. send[i] = el.val();
  621. if (option == false) {
  622. state = self.Match(el, send[i], callback, config);
  623. if (!state) {
  624. return state;
  625. }
  626. } else if(select) {
  627. //只要有一个为真即可
  628. var result = false;
  629. if (select.length <= 0) {
  630. result = true;
  631. }
  632. for (var i in select) {
  633. if (typeof(select[i]) == 'string') {
  634. var el = $(select[i]);
  635. state = self.Match($(el), $(el).val(), false, config);
  636. if (state) {
  637. result = true;
  638. }
  639. }
  640. }
  641. if (result == false) {
  642. return result;
  643. }
  644. }
  645. }
  646. }
  647. }
  648. }
  649. return send;
  650. }
  651. ,Match : function(e, value, callback, config)
  652. {
  653. var m = true;
  654. if (value && e.attr('dever-match')) {
  655. var r = new RegExp(e.attr('dever-match'));
  656. m = r.test(value);
  657. }
  658. if (value && e.attr('dever-value')) {
  659. m = $(e.attr('dever-value')).val() == value;
  660. }
  661. if (!value || m != true) {
  662. if (callback) {
  663. var error = e.attr('dever-error') ? e.attr('dever-error') : config.error;
  664. callback(error);
  665. }
  666. return false;
  667. }
  668. return true;
  669. }
  670. //通用的保存数据功能
  671. ,Save : function(e, setting)
  672. {
  673. var self = this;
  674. var key = e.attr('data-i');
  675. var config = self.config[key];
  676. if (config.start) {
  677. config.start.call();
  678. }
  679. var callback = function(msg,state) {
  680. if (config.status && $(config.status).length) {
  681. //$(config.status).html(msg).css('visibility','initial');
  682. state ? $(config.status).html(msg).hide() : $(config.status).html(msg).show();
  683. } else if(!state) {
  684. Dever.Out(msg);
  685. }
  686. }
  687. var send = self.Check(e, callback, config);
  688. if (!send) {
  689. callback(config.error);
  690. return;
  691. }
  692. if (!config.url) {
  693. config.url = e.attr('dever-send');
  694. }
  695. if (e.attr('dever-refresh')) {
  696. config.url = e.attr('dever-refresh');
  697. }
  698. callback(1,1);
  699. config.url = Dever.CallUrl(config.url);
  700. if (setting.proxy) {
  701. $.post(config.url, send, function(t) {
  702. t = eval('(' + t + ')');
  703. Dever.Msg(t, callback, (config.callback ? function(msg){config.callback(e,msg,t)} : false));
  704. });
  705. } else {
  706. $.getJSON(config.url, send, function(t) {
  707. Dever.Msg(t, callback, (config.callback ? function(msg){config.callback(e,msg,t)} : false));
  708. });
  709. }
  710. }
  711. };
  712. //编辑器相关
  713. var _Dever_Editor =
  714. {
  715. state : false,
  716. editors : [],
  717. Init : function()
  718. {
  719. if (typeof marked == 'function') {
  720. this.state = true;
  721. } else {
  722. this.state = false;
  723. }
  724. this.SetMark();
  725. var self = this;
  726. if ($('.dever-note').length) {
  727. $('.dever-note').each(function() {
  728. self.InitEditor($(this));
  729. })
  730. }
  731. }
  732. ,InitEditor : function(e)
  733. {
  734. var id = e.attr('id');
  735. var key = e.attr('key');
  736. if (config.uid && config.uid > 0) {
  737. var toolbar = [
  738. 'title'
  739. ,'bold'
  740. ,'italic'
  741. ,'underline'
  742. ,'strikethrough'
  743. ,'fontScale'
  744. ,'color'
  745. ,'ol'
  746. ,'ul'
  747. ,'blockquote'
  748. ,'code'
  749. ,'table'
  750. ,'link'
  751. ,'image'
  752. ,'hr'
  753. ,'indent'
  754. ,'outdent'
  755. ,'alignment'
  756. //,'emoji'
  757. ];
  758. } else {
  759. var toolbar = [
  760. 'title'
  761. ,'bold'
  762. ,'fontScale'
  763. ,'color'
  764. ,'ol'
  765. ,'ul'
  766. ,'blockquote'
  767. ,'indent'
  768. ,'outdent'
  769. ,'alignment'
  770. ];
  771. }
  772. if (typeof(this.editors[id]) == "undefined") {
  773. this.editors[id] = new Simditor({
  774. textarea: e,
  775. upload:
  776. {
  777. url: config.upload + '.simditor',
  778. params: {key:key},
  779. fileKey: 'file',
  780. connectionCount: 10,
  781. leaveConfirm: 'Uploading is in progress, are you sure to leave this page?'
  782. },
  783. toolbar : toolbar
  784. /*
  785. ,emoji:
  786. {
  787. imagePath: config.lib + 'simditor/plugins/emoji/images/emoji/'
  788. }
  789. */
  790. });
  791. }
  792. }
  793. ,SetMark : function()
  794. {
  795. return;
  796. }
  797. ,GetMark : function(content)
  798. {
  799. if (this.state == true) {
  800. return marked(content);
  801. }
  802. return content;
  803. }
  804. };
  805. //模板
  806. var _Dever_Template =
  807. {
  808. state : false,
  809. method : '',
  810. element : '',
  811. data : '',
  812. param : '',
  813. attr : '',
  814. Init : function()
  815. {
  816. if (this.Check()) {
  817. var url = config.proxyTemplateUrl;
  818. var send = {};
  819. var parsing = [];
  820. $("dever").each(function(){
  821. parsing.push($(this).html());
  822. $(this).remove();
  823. });
  824. send.url = location.href;
  825. send.parsing = parsing.join('');
  826. send.html = $("html").html();
  827. //$("html").html('dever template loading...');
  828. $.ajax({
  829. type: "POST",
  830. url: url,
  831. data: send,
  832. dataType: "json",
  833. success: function(result){
  834. $("html").html(result.data);
  835. }
  836. });
  837. }
  838. }
  839. ,Check : function()
  840. {
  841. if ($("dever").length) {
  842. this.state = true;
  843. } else {
  844. this.state = false;
  845. }
  846. return this.state;
  847. }
  848. }
  849. //上传 请载入layui
  850. var _Dever_Upload =
  851. {
  852. upload : [],
  853. upload_file : [],
  854. upload_pic : [],
  855. callback : {},
  856. Init : function()
  857. {
  858. var self = this;
  859. if ($(".dever-upload-drag").length) {
  860. $(".dever-upload-drag").each(function() {
  861. var key = $(this).attr('key');
  862. if (config.proxy) {
  863. var url = Dever.Proxy('upload/save.drag') + '&key=' + key;
  864. } else {
  865. var url = config.upload + '.drag?key='+ key;
  866. }
  867. $(this).inlineattachment({
  868. uploadUrl: url,
  869. progressText: '![文件上传中...]()',
  870. urlText: "![文件描述]({filename})\n",
  871. errorText: '上传失败'
  872. });
  873. });
  874. }
  875. if ($(".dever-upload").length) {
  876. $(".dever-upload").each(function(i) {
  877. self.Load(i,$(this));
  878. })
  879. }
  880. }
  881. ,Load : function(i,e)
  882. {
  883. var self = this;
  884. var id = e.attr('id');
  885. var value = e.attr('dever-upload-value');
  886. var key = e.attr('dever-upload-key');
  887. var pic = e.attr('dever-upload-pic');
  888. var type = e.attr('dever-upload-type');
  889. var callback = e.attr('dever-upload-callback');
  890. var input = $("#" + value);
  891. if (!input.length) {
  892. return;
  893. }
  894. var url = config.upload + '.start';
  895. if (typeof(this.upload[value]) != "undefined") {
  896. return;
  897. }
  898. if (!key) {
  899. key = 1;
  900. }
  901. self.upload[value] = true;
  902. self.upload_pic[value] = [];
  903. self.upload_file[value] = [];
  904. var input_value = input.val();
  905. if (input_value && type == 'mul') {
  906. var input_value_array = input_value.split(',');
  907. for (var i in input_value_array) {
  908. self.Set(type, e, input, value, callback, input_value_array[i]);
  909. }
  910. }
  911. layui.use(['upload','layer'], function() {
  912. var layer = layui.layer;
  913. var layuiUpload = layui.upload;
  914. var uploadInst = layuiUpload.render({
  915. elem: '#' + id
  916. ,data: {'key' : key}
  917. ,field: 'file'
  918. ,url: url
  919. ,multiple: true
  920. ,xhr:xhrOnProgress
  921. ,before: function(obj) {
  922. layer.load();
  923. }
  924. ,done: function(data) {
  925. layer.closeAll('loading');
  926. if (data.status == 1) {
  927. self.Set(type, e, input, value, callback, data.url, pic);
  928. } else {
  929. layer.msg(data.msg, {icon: 4});
  930. return false;
  931. }
  932. }
  933. ,error: function() {
  934. layer.closeAll('loading');
  935. layer.msg('上传失败', {icon: 4});
  936. return false;
  937. }
  938. });
  939. });
  940. }
  941. ,Set : function(type, e, input, value, callback, url, pic)
  942. {
  943. if (type == 'mul') {
  944. this.SetMul(e, input, value, callback, url);
  945. } else {
  946. this.SetOne(e, input, value, callback, url, pic);
  947. }
  948. }
  949. ,SetOne : function(e, input, value, callback, url, pic)
  950. {
  951. var html = '';
  952. if (this.callback && this.callback[callback]) {
  953. html = this.callback[callback].call(this, e, url, 'dever-upload-close');
  954. } else {
  955. pic = $("#" + pic);
  956. if (pic.length) {
  957. pic.attr('src', url).show();
  958. } else {
  959. html = '<img src="'+url+'" class="dever-upload-pic">';
  960. }
  961. }
  962. if (html) {
  963. e.html(html);
  964. }
  965. var input_value = input.val();
  966. if (input_value != url) {
  967. input.val(url);
  968. this.Blur(input);
  969. }
  970. }
  971. ,SetMul : function(e, input, value, callback, url)
  972. {
  973. var html = '';
  974. var input_value = input.val();
  975. if (this.upload_pic[value].length <= 0 && input_value) {
  976. this.upload_pic[value] = input_value.split(',');
  977. }
  978. if (this.callback && this.callback[callback]) {
  979. html += this.callback[callback].call(this, e, url, 'dever-upload-close');
  980. } else {
  981. html += '<li><img src="'+url+'" class="dever-upload-pics"><em class="dever-upload-close"></em></li>';
  982. }
  983. if (html) {
  984. e.before(html);
  985. }
  986. this.Close(value);
  987. if (input_value.indexOf(url) == -1) {
  988. this.upload_pic[value].push(url);
  989. input.val(this.upload_pic[value].join(','));
  990. this.Blur(input);
  991. }
  992. }
  993. ,Blur : function(input)
  994. {
  995. var events = $._data(input[0], 'events');
  996. if (events && events["blur"]) {
  997. input.blur();
  998. }
  999. }
  1000. ,Close : function(v)
  1001. {
  1002. var self = this;
  1003. $('.dever-upload-close').each(function()
  1004. {
  1005. $(this).unbind('click').bind('click', function() {
  1006. var e = $(this).parent();
  1007. var p = e.find('img').attr('src');
  1008. e.remove();
  1009. self.upload_pic[v].remove(p);
  1010. $("#" + v).val(self.upload_pic[v].join(','));
  1011. });
  1012. });
  1013. }
  1014. }
  1015. //创建监听函数
  1016. var xhrOnProgress=function(fun) {
  1017. xhrOnProgress.onprogress = fun; //绑定监听
  1018. //使用闭包实现监听绑
  1019. return function() {
  1020. //通过$.ajaxSettings.xhr();获得XMLHttpRequest对象
  1021. var xhr = $.ajaxSettings.xhr();
  1022. //判断监听函数是否为函数
  1023. if (typeof xhrOnProgress.onprogress !== 'function') return xhr;
  1024. //如果有监听函数并且xhr对象支持绑定时就把监听函数绑定上去
  1025. if (xhrOnProgress.onprogress && xhr.upload) {
  1026. xhr.upload.onprogress = xhrOnProgress.onprogress;
  1027. }
  1028. return xhr;
  1029. }
  1030. }