core.js 33 KB

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