addon.js 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template', 'cookie'], function ($, undefined, Backend, Table, Form, Template, undefined) {
  2. $.cookie.prototype.defaults = {path: Config.moduleurl};
  3. var Controller = {
  4. index: function () {
  5. // 初始化表格参数配置
  6. Table.api.init({
  7. extend: {
  8. index_url: Config.api_url ? Config.api_url + '/addon/index' : "addon/downloaded",
  9. add_url: '',
  10. edit_url: '',
  11. del_url: '',
  12. multi_url: ''
  13. }
  14. });
  15. var table = $("#table");
  16. // 弹窗自适应宽高
  17. var area = Fast.config.openArea != undefined ? Fast.config.openArea : [$(window).width() > 800 ? '800px' : '95%', $(window).height() > 600 ? '600px' : '95%'];
  18. var switch_local = function () {
  19. if ($(".btn-switch.active").data("type") != "local") {
  20. Layer.confirm(__('Store not available tips'), {
  21. title: __('Warmtips'),
  22. btn: [__('Switch to the local'), __('Try to reload')]
  23. }, function (index) {
  24. layer.close(index);
  25. $(".panel .nav-tabs").hide();
  26. $(".toolbar > *:not(:first)").hide();
  27. $(".btn-switch[data-type='local']").trigger("click");
  28. }, function (index) {
  29. layer.close(index);
  30. table.bootstrapTable('refresh');
  31. });
  32. return false;
  33. }
  34. };
  35. table.on('load-success.bs.table', function (e, json) {
  36. if (json && typeof json.category != 'undefined' && $(".nav-category li").length == 2) {
  37. $.each(json.category, function (i, j) {
  38. $("<li><a href='javascript:;' data-id='" + j.id + "'>" + j.name + "</a></li>").insertBefore($(".nav-category li:last"));
  39. });
  40. }
  41. if (typeof json.rows === 'undefined' && typeof json.code != 'undefined') {
  42. switch_local();
  43. }
  44. });
  45. table.on('load-error.bs.table', function (e, status, res) {
  46. console.log(e, status, res);
  47. switch_local();
  48. });
  49. table.on('post-body.bs.table', function (e, settings, json, xhr) {
  50. var parenttable = table.closest('.bootstrap-table');
  51. var d = $(".fixed-table-toolbar", parenttable).find(".search input");
  52. d.off("keyup drop blur");
  53. d.on("keyup", function (e) {
  54. if (e.keyCode == 13) {
  55. var that = this;
  56. var options = table.bootstrapTable('getOptions');
  57. var queryParams = options.queryParams;
  58. options.pageNumber = 1;
  59. options.queryParams = function (params) {
  60. var params = queryParams(params);
  61. params.search = $(that).val();
  62. return params;
  63. };
  64. table.bootstrapTable('refresh', {});
  65. }
  66. });
  67. });
  68. Template.helper("Moment", Moment);
  69. Template.helper("addons", Config['addons']);
  70. $("#faupload-addon").data("params", function (files, xhr) {
  71. var userinfo = Controller.api.userinfo.get();
  72. return {
  73. uid: userinfo ? userinfo.id : '',
  74. token: userinfo ? userinfo.token : '',
  75. version: Config.faversion,
  76. force: (files[0].force || false) ? 1 : 0
  77. };
  78. });
  79. // 初始化表格
  80. table.bootstrapTable({
  81. url: $.fn.bootstrapTable.defaults.extend.index_url,
  82. pageSize: 50,
  83. queryParams: function (params) {
  84. var userinfo = Controller.api.userinfo.get();
  85. $.extend(params, {
  86. uid: userinfo ? userinfo.id : '',
  87. token: userinfo ? userinfo.token : '',
  88. domain: Config.domain,
  89. version: Config.faversion,
  90. sid: Controller.api.sid()
  91. });
  92. return params;
  93. },
  94. columns: [
  95. [
  96. {field: 'id', title: 'ID', operate: false, visible: false},
  97. {
  98. field: 'home',
  99. title: __('Index'),
  100. width: '50px',
  101. formatter: Controller.api.formatter.home
  102. },
  103. {field: 'name', title: __('Name'), operate: false, visible: false, width: '120px'},
  104. {
  105. field: 'title',
  106. title: __('Title'),
  107. operate: 'LIKE',
  108. align: 'left',
  109. formatter: Controller.api.formatter.title
  110. },
  111. {
  112. field: 'intro',
  113. title: __('Intro'),
  114. operate: 'LIKE',
  115. align: 'left',
  116. class: 'visible-lg',
  117. formatter: Controller.api.formatter.intro
  118. },
  119. {
  120. field: 'author',
  121. title: __('Author'),
  122. operate: 'LIKE',
  123. width: '100px',
  124. formatter: Controller.api.formatter.author
  125. },
  126. {
  127. field: 'price',
  128. title: __('Price'),
  129. operate: 'LIKE',
  130. width: '100px',
  131. align: 'center',
  132. formatter: Controller.api.formatter.price
  133. },
  134. {
  135. field: 'downloads',
  136. title: __('Downloads'),
  137. operate: 'LIKE',
  138. width: '80px',
  139. align: 'center',
  140. formatter: Controller.api.formatter.downloads
  141. },
  142. {
  143. field: 'version',
  144. title: __('Version'),
  145. operate: 'LIKE',
  146. width: '80px',
  147. align: 'center',
  148. formatter: Controller.api.formatter.version
  149. },
  150. {
  151. field: 'toggle',
  152. title: __('Status'),
  153. width: '80px',
  154. formatter: Controller.api.formatter.toggle
  155. },
  156. {
  157. field: 'id',
  158. title: __('Operate'),
  159. table: table,
  160. formatter: Controller.api.formatter.operate,
  161. align: 'right',
  162. cellStyle: function (value, row, index) {
  163. return {css: {'min-width': '158px'}};
  164. }
  165. },
  166. ]
  167. ],
  168. responseHandler: function (res) {
  169. $.each(res.rows, function (i, j) {
  170. j.addon = typeof Config.addons[j.name] != 'undefined' ? Config.addons[j.name] : null;
  171. });
  172. return res;
  173. },
  174. dataType: 'jsonp',
  175. templateView: false,
  176. clickToSelect: false,
  177. search: true,
  178. showColumns: false,
  179. showToggle: false,
  180. showExport: false,
  181. showSearch: false,
  182. commonSearch: true,
  183. searchFormVisible: true,
  184. searchFormTemplate: 'searchformtpl',
  185. });
  186. // 为表格绑定事件
  187. Table.api.bindevent(table);
  188. // 离线安装
  189. require(['upload'], function (Upload) {
  190. Upload.api.upload("#faupload-addon", function (data, ret, up, file) {
  191. Config['addons'][data.addon.name] = data.addon;
  192. var addon = data.addon;
  193. var testdata = data.addon.testdata;
  194. operate(data.addon.name, 'enable', false, function (data, ret) {
  195. Layer.alert(__('Offline installed tips') + (testdata ? __('Testdata tips') : ""), {
  196. btn: testdata ? [__('Import testdata'), __('Skip testdata')] : [__('OK')],
  197. title: __('Warning'),
  198. yes: function (index) {
  199. if (testdata) {
  200. Fast.api.ajax({
  201. url: 'addon/testdata',
  202. data: {
  203. name: addon.name,
  204. version: addon.version,
  205. faversion: Config.faversion
  206. }
  207. }, function (data, ret) {
  208. Layer.close(index);
  209. });
  210. } else {
  211. Layer.close(index);
  212. }
  213. },
  214. icon: 1
  215. });
  216. });
  217. return false;
  218. }, function (data, ret, up, file) {
  219. if (ret.msg && ret.msg.match(/(login|登录)/g)) {
  220. return Layer.alert(ret.msg, {
  221. title: __('Warning'),
  222. btn: [__('Login now')],
  223. yes: function (index, layero) {
  224. $(".btn-userinfo").trigger("click");
  225. }
  226. });
  227. } else if (ret.code === -1) {
  228. Layer.confirm(__('Upgrade tips', data.title), {title: __('Warmtips')}, function (index, layero) {
  229. up.removeFile(file);
  230. file.force = true;
  231. up.uploadFile(file);
  232. Layer.close(index);
  233. });
  234. return false;
  235. } else if (ret && ret.code === -3) {
  236. //插件目录发现影响全局的文件
  237. Layer.open({
  238. content: Template("conflicttpl", ret.data),
  239. shade: 0.8,
  240. area: area,
  241. title: __('Warning'),
  242. btn: [__('Continue install'), __('Cancel')],
  243. end: function () {
  244. },
  245. yes: function (index) {
  246. up.removeFile(file);
  247. file.force = true;
  248. up.uploadFile(file);
  249. Layer.close(index);
  250. }
  251. });
  252. } else {
  253. Layer.alert(ret.msg, {title: __('Warning'), icon: 0});
  254. }
  255. });
  256. // 检测是否登录
  257. $(document).on("mousedown", "#faupload-addon", function (e) {
  258. var userinfo = Controller.api.userinfo.get();
  259. var uid = userinfo ? userinfo.id : 0;
  260. var uploadBtn = Upload.list['faupload-addon'];
  261. if (parseInt(uid) === 0) {
  262. uploadBtn.disable();
  263. $(".btn-userinfo").trigger("click");
  264. return false;
  265. } else {
  266. if (uploadBtn.disabled) {
  267. uploadBtn.enable();
  268. }
  269. }
  270. });
  271. });
  272. // 查看插件首页
  273. $(document).on("click", ".btn-addonindex", function () {
  274. if ($(this).attr("href") == 'javascript:;') {
  275. Layer.msg(__('Not installed tips'), {icon: 7});
  276. } else if ($(this).closest(".operate").find("a.btn-enable").length > 0) {
  277. Layer.msg(__('Not enabled tips'), {icon: 7});
  278. return false;
  279. }
  280. });
  281. // 切换
  282. $(document).on("click", ".btn-switch", function () {
  283. $(".btn-switch").removeClass("active");
  284. $(this).addClass("active");
  285. $("form.form-commonsearch input[name='type']").val($(this).data("type"));
  286. var method = $(this).data("type") == 'local' ? 'hideColumn' : 'showColumn';
  287. table.bootstrapTable(method, 'price');
  288. table.bootstrapTable(method, 'downloads');
  289. table.bootstrapTable('refresh', {url: ($(this).data("url") ? $(this).data("url") : $.fn.bootstrapTable.defaults.extend.index_url), pageNumber: 1});
  290. return false;
  291. });
  292. // 切换分类
  293. $(document).on("click", ".nav-category li a", function () {
  294. $(".nav-category li").removeClass("active");
  295. $(this).parent().addClass("active");
  296. $("form.form-commonsearch input[name='category_id']").val($(this).data("id"));
  297. table.bootstrapTable('refresh', {url: $(this).data("url"), pageNumber: 1});
  298. return false;
  299. });
  300. var tables = [];
  301. $(document).on("click", "#droptables", function () {
  302. if ($(this).prop("checked")) {
  303. Fast.api.ajax({
  304. url: "addon/get_table_list",
  305. async: false,
  306. data: {name: $(this).data("name")}
  307. }, function (data) {
  308. tables = data.tables;
  309. return false;
  310. });
  311. var html;
  312. html = tables.length > 0 ? '<div class="alert alert-warning-light droptablestips" style="max-width:480px;max-height:300px;overflow-y: auto;">' + __('The following data tables will be deleted') + ':<br>' + tables.join("<br>") + '</div>'
  313. : '<div class="alert alert-warning-light droptablestips">' + __('The Addon did not create a data table') + '</div>';
  314. $(html).insertAfter($(this).closest("p"));
  315. } else {
  316. $(".droptablestips").remove();
  317. }
  318. $(window).resize();
  319. });
  320. // 会员信息
  321. $(document).on("click", ".btn-userinfo", function (e, name, version) {
  322. var that = this;
  323. var area = [$(window).width() > 800 ? '500px' : '95%', $(window).height() > 600 ? '400px' : '95%'];
  324. var userinfo = Controller.api.userinfo.get();
  325. if (!userinfo) {
  326. Fast.api.ajax({
  327. url: Config.api_url + '/user/logintpl',
  328. type: 'post',
  329. loading: false,
  330. data: {
  331. version: Config.faversion,
  332. sid: Controller.api.sid()
  333. }
  334. }, function (tpldata, ret) {
  335. Layer.open({
  336. content: Template.render(tpldata, {}),
  337. zIndex: 99,
  338. area: area,
  339. title: __('Login'),
  340. resize: false,
  341. btn: [__('Login')],
  342. yes: function (index, layero) {
  343. var data = $("form", layero).serializeArray();
  344. data.push({name: "faversion", value: Config.faversion});
  345. data.push({name: "sid", value: Controller.api.sid()});
  346. Fast.api.ajax({
  347. url: Config.api_url + '/user/login',
  348. type: 'post',
  349. data: data
  350. }, function (data, ret) {
  351. Controller.api.userinfo.set(data);
  352. Layer.closeAll();
  353. Layer.alert(ret.msg, {title: __('Warning'), icon: 1});
  354. return false;
  355. }, function (data, ret) {
  356. });
  357. },
  358. success: function (layero, index) {
  359. this.checkEnterKey = function (event) {
  360. if (event.keyCode === 13) {
  361. $(".layui-layer-btn0").trigger("click");
  362. return false;
  363. }
  364. };
  365. $(document).on('keydown', this.checkEnterKey);
  366. },
  367. end: function () {
  368. $(document).off('keydown', this.checkEnterKey);
  369. }
  370. });
  371. return false;
  372. });
  373. } else {
  374. Fast.api.ajax({
  375. url: Config.api_url + '/user/userinfotpl',
  376. type: 'post',
  377. data: {
  378. uid: userinfo.id,
  379. token: userinfo.token,
  380. version: Config.faversion,
  381. sid: Controller.api.sid()
  382. }
  383. }, function (tpldata, ret) {
  384. Layer.open({
  385. content: Template.render(tpldata, userinfo),
  386. area: area,
  387. title: __('Userinfo'),
  388. resize: false,
  389. btn: [__('Logout'), __('Close')],
  390. yes: function () {
  391. Fast.api.ajax({
  392. url: Config.api_url + '/user/logout',
  393. data: {
  394. uid: userinfo.id,
  395. token: userinfo.token,
  396. version: Config.faversion,
  397. sid: Controller.api.sid()
  398. }
  399. }, function (data, ret) {
  400. Controller.api.userinfo.set(null);
  401. Layer.closeAll();
  402. Layer.alert(ret.msg, {title: __('Warning'), icon: 0});
  403. }, function (data, ret) {
  404. Controller.api.userinfo.set(null);
  405. Layer.closeAll();
  406. Layer.alert(ret.msg, {title: __('Warning'), icon: 0});
  407. });
  408. }
  409. });
  410. return false;
  411. }, function (data) {
  412. Controller.api.userinfo.set(null);
  413. $(that).trigger('click');
  414. return false;
  415. });
  416. }
  417. });
  418. //刷新授权
  419. $(document).on("click", ".btn-authorization", function () {
  420. var userinfo = Controller.api.userinfo.get();
  421. if (!userinfo) {
  422. $(".btn-userinfo").trigger("click");
  423. return false;
  424. }
  425. Layer.confirm(__('Are you sure you want to refresh authorization?'), {icon: 3, title: __('Warmtips')}, function () {
  426. Fast.api.ajax({
  427. url: 'addon/authorization',
  428. data: {
  429. uid: userinfo.id,
  430. token: userinfo.token
  431. }
  432. }, function (data, ret) {
  433. $(".btn-refresh").trigger("click");
  434. Layer.closeAll();
  435. });
  436. });
  437. return false;
  438. });
  439. var install = function (name, version, force) {
  440. var userinfo = Controller.api.userinfo.get();
  441. var uid = userinfo ? userinfo.id : 0;
  442. var token = userinfo ? userinfo.token : '';
  443. Fast.api.ajax({
  444. url: 'addon/install',
  445. data: {
  446. name: name,
  447. force: force ? 1 : 0,
  448. uid: uid,
  449. token: token,
  450. version: version,
  451. faversion: Config.faversion
  452. }
  453. }, function (data, ret) {
  454. Layer.closeAll();
  455. Config['addons'][data.addon.name] = ret.data.addon;
  456. operate(data.addon.name, 'enable', false, function () {
  457. Layer.alert(__('Online installed tips') + (data.addon.testdata ? __('Testdata tips') : ""), {
  458. btn: data.addon.testdata ? [__('Import testdata'), __('Skip testdata')] : [__('OK')],
  459. title: __('Warning'),
  460. yes: function (index) {
  461. if (data.addon.testdata) {
  462. Fast.api.ajax({
  463. url: 'addon/testdata',
  464. data: {
  465. name: name,
  466. uid: uid,
  467. token: token,
  468. version: version,
  469. faversion: Config.faversion
  470. }
  471. }, function (data, ret) {
  472. Layer.close(index);
  473. });
  474. } else {
  475. Layer.close(index);
  476. }
  477. },
  478. icon: 1
  479. });
  480. Controller.api.refresh(table, name);
  481. });
  482. }, function (data, ret) {
  483. var area = Fast.config.openArea != undefined ? Fast.config.openArea : [$(window).width() > 650 ? '650px' : '95%', $(window).height() > 710 ? '710px' : '95%'];
  484. if (ret && ret.code === -2) {
  485. //如果登录已经超时,重新提醒登录
  486. if (uid && uid != ret.data.uid) {
  487. Controller.api.userinfo.set(null);
  488. $(".operate[data-name='" + name + "'] .btn-install:first").trigger("click");
  489. return;
  490. }
  491. top.Fast.api.open(ret.data.payurl, __('Pay now'), {
  492. area: area,
  493. end: function () {
  494. Fast.api.ajax({
  495. url: 'addon/isbuy',
  496. data: {
  497. name: name,
  498. force: force ? 1 : 0,
  499. uid: uid,
  500. token: token,
  501. version: version,
  502. faversion: Config.faversion
  503. }
  504. }, function () {
  505. top.Layer.alert(__('Pay successful tips'), {
  506. btn: [__('Continue installation')],
  507. title: __('Warning'),
  508. icon: 1,
  509. yes: function (index) {
  510. top.Layer.close(index);
  511. install(name, version);
  512. }
  513. });
  514. return false;
  515. }, function () {
  516. console.log(__('Canceled'));
  517. return false;
  518. });
  519. }
  520. });
  521. } else if (ret && ret.code === -3) {
  522. //插件目录发现影响全局的文件
  523. Layer.open({
  524. content: Template("conflicttpl", ret.data),
  525. shade: 0.8,
  526. area: area,
  527. title: __('Warning'),
  528. btn: [__('Continue install'), __('Cancel')],
  529. end: function () {
  530. },
  531. yes: function () {
  532. install(name, version, true);
  533. }
  534. });
  535. } else {
  536. Layer.alert(ret.msg, {title: __('Warning'), icon: 0});
  537. }
  538. return false;
  539. });
  540. };
  541. var uninstall = function (name, force, droptables) {
  542. Fast.api.ajax({
  543. url: 'addon/uninstall',
  544. data: {name: name, force: force ? 1 : 0, droptables: droptables ? 1 : 0}
  545. }, function (data, ret) {
  546. delete Config['addons'][name];
  547. Layer.closeAll();
  548. Controller.api.refresh(table, name);
  549. }, function (data, ret) {
  550. if (ret && ret.code === -3) {
  551. //插件目录发现影响全局的文件
  552. Layer.open({
  553. content: Template("conflicttpl", ret.data),
  554. shade: 0.8,
  555. area: area,
  556. title: __('Warning'),
  557. btn: [__('Continue uninstall'), __('Cancel')],
  558. end: function () {
  559. },
  560. yes: function () {
  561. uninstall(name, true, droptables);
  562. }
  563. });
  564. } else {
  565. Layer.alert(ret.msg, {title: __('Warning'), icon: 0});
  566. }
  567. return false;
  568. });
  569. };
  570. var operate = function (name, action, force, success) {
  571. Fast.api.ajax({
  572. url: 'addon/state',
  573. data: {name: name, action: action, force: force ? 1 : 0}
  574. }, function (data, ret) {
  575. var addon = Config['addons'][name];
  576. addon.state = action === 'enable' ? 1 : 0;
  577. Layer.closeAll();
  578. if (typeof success === 'function') {
  579. success(data, ret);
  580. }
  581. Controller.api.refresh(table, name);
  582. }, function (data, ret) {
  583. if (ret && ret.code === -3) {
  584. //插件目录发现影响全局的文件
  585. Layer.open({
  586. content: Template("conflicttpl", ret.data),
  587. shade: 0.8,
  588. area: area,
  589. title: __('Warning'),
  590. btn: [__('Continue operate'), __('Cancel')],
  591. end: function () {
  592. },
  593. yes: function () {
  594. operate(name, action, true, success);
  595. }
  596. });
  597. } else {
  598. Layer.alert(ret.msg, {title: __('Warning'), icon: 0});
  599. }
  600. return false;
  601. });
  602. };
  603. var upgrade = function (name, version) {
  604. var userinfo = Controller.api.userinfo.get();
  605. var uid = userinfo ? userinfo.id : 0;
  606. var token = userinfo ? userinfo.token : '';
  607. Fast.api.ajax({
  608. url: 'addon/upgrade',
  609. data: {name: name, uid: uid, token: token, version: version, faversion: Config.faversion}
  610. }, function (data, ret) {
  611. Config['addons'][name] = data.addon;
  612. Layer.closeAll();
  613. Controller.api.refresh(table, name);
  614. }, function (data, ret) {
  615. Layer.alert(ret.msg, {title: __('Warning')});
  616. return false;
  617. });
  618. };
  619. // 点击安装
  620. $(document).on("click", ".btn-install", function () {
  621. var that = this;
  622. var name = $(this).closest(".operate").data("name");
  623. var version = $(this).data("version");
  624. var userinfo = Controller.api.userinfo.get();
  625. var uid = userinfo ? userinfo.id : 0;
  626. if (parseInt(uid) === 0) {
  627. $(".btn-userinfo").trigger("click", name, version);
  628. return false;
  629. }
  630. install(name, version, false);
  631. });
  632. // 点击卸载
  633. $(document).on("click", ".btn-uninstall", function () {
  634. var name = $(this).closest(".operate").data('name');
  635. if (Config['addons'][name].state == 1) {
  636. Layer.alert(__('Please disable the add before trying to uninstall'), {icon: 7});
  637. return false;
  638. }
  639. Template.helper("__", __);
  640. tables = [];
  641. Layer.confirm(Template("uninstalltpl", {addon: Config['addons'][name]}), {focusBtn: false, title: __("Warning")}, function (index, layero) {
  642. uninstall(name, false, $("input[name='droptables']", layero).prop("checked"));
  643. });
  644. });
  645. // 点击配置
  646. $(document).on("click", ".btn-config", function () {
  647. var name = $(this).closest(".operate").data("name");
  648. Fast.api.open("addon/config?name=" + name, __('Setting'));
  649. });
  650. // 点击启用/禁用
  651. $(document).on("click", ".btn-enable,.btn-disable", function () {
  652. var name = $(this).data("name");
  653. var action = $(this).data("action");
  654. operate(name, action, false);
  655. });
  656. // 点击升级
  657. $(document).on("click", ".btn-upgrade", function () {
  658. var name = $(this).closest(".operate").data('name');
  659. if (Config['addons'][name].state == 1) {
  660. Layer.alert(__('Please disable the add before trying to upgrade'), {icon: 7});
  661. return false;
  662. }
  663. var version = $(this).data("version");
  664. Layer.confirm(__('Upgrade tips', Config['addons'][name].title), {title: __('Warmtips')}, function (index, layero) {
  665. upgrade(name, version);
  666. });
  667. });
  668. $(document).on("click", ".operate .btn-group .dropdown-toggle", function () {
  669. $(this).closest(".btn-group").toggleClass("dropup", $(document).height() - $(this).offset().top <= 200);
  670. });
  671. $(document).on("click", ".view-screenshots", function () {
  672. var row = Table.api.getrowbyindex(table, parseInt($(this).data("index")));
  673. var data = [];
  674. $.each(row.screenshots, function (i, j) {
  675. data.push({
  676. "src": j
  677. });
  678. });
  679. var json = {
  680. "title": row.title,
  681. "data": data
  682. };
  683. top.Layer.photos(top.JSON.parse(JSON.stringify({photos: json})));
  684. });
  685. },
  686. add: function () {
  687. Controller.api.bindevent();
  688. },
  689. config: function () {
  690. $(document).on("click", ".nav-group li a[data-toggle='tab']", function () {
  691. if ($(this).attr("href") == "#all") {
  692. $(".tab-pane").addClass("active in");
  693. }
  694. return;
  695. var type = $(this).attr("href").substring(1);
  696. if (type == 'all') {
  697. $(".table-config tr").show();
  698. } else {
  699. $(".table-config tr").hide();
  700. $(".table-config tr[data-group='" + type + "']").show();
  701. }
  702. });
  703. Controller.api.bindevent();
  704. },
  705. api: {
  706. formatter: {
  707. title: function (value, row, index) {
  708. if ($(".btn-switch.active").data("type") == "local") {
  709. // return value;
  710. }
  711. var title = '<a class="title" href="' + row.url + '" data-toggle="tooltip" title="' + __('View addon home page') + '" target="_blank"><span class="' + Fast.api.escape(row.color) + '">' + value + '</span></a>';
  712. if (row.screenshots && row.screenshots.length > 0) {
  713. title += ' <a href="javascript:;" data-index="' + index + '" class="view-screenshots text-success" title="' + __('View addon screenshots') + '" data-toggle="tooltip"><i class="fa fa-image"></i></a>';
  714. }
  715. return title;
  716. },
  717. intro: function (value, row, index) {
  718. return row.intro + (row.extend ? "<a href='" + Fast.api.escape(row.extend[1]) + "' class='" + Fast.api.escape(row.extend[2]) + "'>" + Fast.api.escape(row.extend[0]) + "</a>" : "");
  719. },
  720. operate: function (value, row, index) {
  721. return Template("operatetpl", {item: row, index: index});
  722. },
  723. toggle: function (value, row, index) {
  724. if (!row.addon) {
  725. return '';
  726. }
  727. return '<a href="javascript:;" data-toggle="tooltip" title="' + __('Click to toggle status') + '" class="btn btn-toggle btn-' + (row.addon.state == 1 ? "disable" : "enable") + '" data-action="' + (row.addon.state == 1 ? "disable" : "enable") + '" data-name="' + row.name + '"><i class="fa ' + (row.addon.state == 0 ? 'fa-toggle-on fa-rotate-180 text-gray' : 'fa-toggle-on text-success') + ' fa-2x"></i></a>';
  728. },
  729. author: function (value, row, index) {
  730. var url = 'javascript:';
  731. if (typeof row.homepage !== 'undefined') {
  732. url = row.homepage;
  733. } else if (typeof row.qq !== 'undefined' && row.qq) {
  734. url = 'https://wpa.qq.com/msgrd?v=3&uin=' + row.qq + '&site=&menu=yes';
  735. }
  736. return '<a href="' + url + '" target="_blank" data-toggle="tooltip" class="text-primary">' + value + '</a>';
  737. },
  738. price: function (value, row, index) {
  739. if (isNaN(value)) {
  740. return value;
  741. }
  742. return parseFloat(value) == 0 ? '<span class="text-success">' + __('Free') + '</span>' : '<span class="text-danger">¥' + value + '</span>';
  743. },
  744. downloads: function (value, row, index) {
  745. return value;
  746. },
  747. version: function (value, row, index) {
  748. return row.addon && row.addon.version != row.version ? '<a href="' + row.url + '?version=' + row.version + '" target="_blank"><span class="releasetips text-primary" data-toggle="tooltip" title="' + __('New version tips', row.version) + '">' + row.addon.version + '<i></i></span></a>' : row.version;
  749. },
  750. home: function (value, row, index) {
  751. return row.addon && parseInt(row.addon.state) > 0 ? '<a href="' + row.addon.url + '" data-toggle="tooltip" title="' + __('View addon index page') + '" target="_blank"><i class="fa fa-home text-primary"></i></a>' : '<a href="javascript:;"><i class="fa fa-home text-gray"></i></a>';
  752. },
  753. },
  754. bindevent: function () {
  755. Form.api.bindevent($("form[role=form]"));
  756. },
  757. userinfo: {
  758. get: function () {
  759. if (typeof $.cookie !== 'undefined') {
  760. var userinfo = $.cookie('fastadmin_userinfo');
  761. } else {
  762. var userinfo = sessionStorage.getItem("fastadmin_userinfo");
  763. }
  764. return userinfo ? JSON.parse(userinfo) : null;
  765. },
  766. set: function (data) {
  767. if (typeof $.cookie !== 'undefined') {
  768. if (data) {
  769. $.cookie("fastadmin_userinfo", JSON.stringify(data));
  770. } else {
  771. $.removeCookie("fastadmin_userinfo");
  772. }
  773. } else {
  774. if (data) {
  775. sessionStorage.setItem("fastadmin_userinfo", JSON.stringify(data));
  776. } else {
  777. sessionStorage.removeItem("fastadmin_userinfo");
  778. }
  779. }
  780. }
  781. },
  782. sid: function () {
  783. var sid = $.cookie('fastadmin_sid');
  784. if (!sid) {
  785. sid = Math.random().toString(20).substr(2, 12);
  786. $.cookie('fastadmin_sid', sid);
  787. }
  788. return sid;
  789. },
  790. refresh: function (table, name) {
  791. //刷新左侧边栏
  792. Fast.api.refreshmenu();
  793. //刷新行数据
  794. if ($(".operate[data-name='" + name + "']").length > 0) {
  795. var tr = $(".operate[data-name='" + name + "']").closest("tr[data-index]");
  796. var index = tr.data("index");
  797. var row = Table.api.getrowbyindex(table, index);
  798. row.addon = typeof Config['addons'][name] !== 'undefined' ? Config['addons'][name] : undefined;
  799. table.bootstrapTable("updateRow", {index: index, row: row});
  800. } else if ($(".btn-switch.active").data("type") == "local") {
  801. $(".btn-refresh").trigger("click");
  802. }
  803. }
  804. }
  805. };
  806. return Controller;
  807. });