core.js 33 KB

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