addon.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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. },
  163. ]
  164. ],
  165. responseHandler: function (res) {
  166. $.each(res.rows, function (i, j) {
  167. j.addon = typeof Config.addons[j.name] != 'undefined' ? Config.addons[j.name] : null;
  168. });
  169. return res;
  170. },
  171. dataType: 'json',
  172. templateView: false,
  173. clickToSelect: false,
  174. search: true,
  175. showColumns: false,
  176. showToggle: false,
  177. showExport: false,
  178. showSearch: false,
  179. commonSearch: true,
  180. searchFormVisible: true,
  181. searchFormTemplate: 'searchformtpl',
  182. });
  183. // 为表格绑定事件
  184. Table.api.bindevent(table);
  185. // 离线安装
  186. require(['upload'], function (Upload) {
  187. Upload.api.upload("#faupload-addon", function (data, ret, up, file) {
  188. Config['addons'][data.addon.name] = data.addon;
  189. var addon = data.addon;
  190. var testdata = data.addon.testdata;
  191. operate(data.addon.name, 'enable', false, function (data, ret) {
  192. Layer.alert(__('Offline installed tips') + (testdata ? __('Testdata tips') : ""), {
  193. btn: testdata ? [__('Import testdata'), __('Skip testdata')] : [__('OK')],
  194. title: __('Warning'),
  195. yes: function (index) {
  196. if (testdata) {
  197. Fast.api.ajax({
  198. url: 'addon/testdata',
  199. data: {
  200. name: addon.name,
  201. version: addon.version,
  202. faversion: Config.faversion
  203. }
  204. }, function (data, ret) {
  205. Layer.close(index);
  206. });
  207. } else {
  208. Layer.close(index);
  209. }
  210. },
  211. icon: 1
  212. });
  213. });
  214. return false;
  215. }, function (data, ret, up, file) {
  216. if (ret.msg && ret.msg.match(/(login|登录)/g)) {
  217. return Layer.alert(ret.msg, {
  218. title: __('Warning'),
  219. btn: [__('Login now')],
  220. yes: function (index, layero) {
  221. $(".btn-userinfo").trigger("click");
  222. }
  223. });
  224. } else if (ret.code === -1) {
  225. Layer.confirm(__('Upgrade tips', data.title), {title: __('Warmtips')}, function (index, layero) {
  226. up.removeFile(file);
  227. file.force = true;
  228. up.uploadFile(file);
  229. Layer.close(index);
  230. });
  231. return false;
  232. }
  233. });
  234. // 检测是否登录
  235. $(document).on("mousedown", "#faupload-addon", function (e) {
  236. var userinfo = Controller.api.userinfo.get();
  237. var uid = userinfo ? userinfo.id : 0;
  238. if (parseInt(uid) === 0) {
  239. $(".btn-userinfo").trigger("click");
  240. return false;
  241. }
  242. });
  243. });
  244. // 查看插件首页
  245. $(document).on("click", ".btn-addonindex", function () {
  246. if ($(this).attr("href") == 'javascript:;') {
  247. Layer.msg(__('Not installed tips'), {icon: 7});
  248. } else if ($(this).closest(".operate").find("a.btn-enable").length > 0) {
  249. Layer.msg(__('Not enabled tips'), {icon: 7});
  250. return false;
  251. }
  252. });
  253. // 切换
  254. $(document).on("click", ".btn-switch", function () {
  255. $(".btn-switch").removeClass("active");
  256. $(this).addClass("active");
  257. $("form.form-commonsearch input[name='type']").val($(this).data("type"));
  258. var method = $(this).data("type") == 'local' ? 'hideColumn' : 'showColumn';
  259. table.bootstrapTable(method, 'price');
  260. table.bootstrapTable(method, 'downloads');
  261. table.bootstrapTable('refresh', {url: ($(this).data("url") ? $(this).data("url") : $.fn.bootstrapTable.defaults.extend.index_url), pageNumber: 1});
  262. return false;
  263. });
  264. // 切换分类
  265. $(document).on("click", ".nav-category li a", function () {
  266. $(".nav-category li").removeClass("active");
  267. $(this).parent().addClass("active");
  268. $("form.form-commonsearch input[name='category_id']").val($(this).data("id"));
  269. table.bootstrapTable('refresh', {url: $(this).data("url"), pageNumber: 1});
  270. return false;
  271. });
  272. var tables = [];
  273. $(document).on("click", "#droptables", function () {
  274. if ($(this).prop("checked")) {
  275. Fast.api.ajax({
  276. url: "addon/get_table_list",
  277. async: false,
  278. data: {name: $(this).data("name")}
  279. }, function (data) {
  280. tables = data.tables;
  281. return false;
  282. });
  283. var html;
  284. 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>'
  285. : '<div class="alert alert-warning-light droptablestips">' + __('The Addon did not create a data table') + '</div>';
  286. $(html).insertAfter($(this).closest("p"));
  287. } else {
  288. $(".droptablestips").remove();
  289. }
  290. $(window).resize();
  291. });
  292. // 会员信息
  293. $(document).on("click", ".btn-userinfo", function (e, name, version) {
  294. var that = this;
  295. var area = [$(window).width() > 800 ? '500px' : '95%', $(window).height() > 600 ? '400px' : '95%'];
  296. var userinfo = Controller.api.userinfo.get();
  297. if (!userinfo) {
  298. Fast.api.ajax({
  299. url: Config.api_url + '/user/logintpl',
  300. type: 'post',
  301. loading: false,
  302. data: {
  303. version: Config.faversion,
  304. sid: Controller.api.sid()
  305. }
  306. }, function (tpldata, ret) {
  307. Layer.open({
  308. content: Template.render(tpldata, {}),
  309. zIndex: 99,
  310. area: area,
  311. title: __('Login'),
  312. resize: false,
  313. btn: [__('Login')],
  314. yes: function (index, layero) {
  315. var data = $("form", layero).serializeArray();
  316. data.push({name: "faversion", value: Config.faversion});
  317. data.push({name: "sid", value: Controller.api.sid()});
  318. Fast.api.ajax({
  319. url: Config.api_url + '/user/login',
  320. type: 'post',
  321. data: data
  322. }, function (data, ret) {
  323. Controller.api.userinfo.set(data);
  324. Layer.closeAll();
  325. Layer.alert(ret.msg, {title: __('Warning'), icon: 1});
  326. return false;
  327. }, function (data, ret) {
  328. });
  329. },
  330. success: function (layero, index) {
  331. this.checkEnterKey = function (event) {
  332. if (event.keyCode === 13) {
  333. $(".layui-layer-btn0").trigger("click");
  334. return false;
  335. }
  336. };
  337. $(document).on('keydown', this.checkEnterKey);
  338. },
  339. end: function () {
  340. $(document).off('keydown', this.checkEnterKey);
  341. }
  342. });
  343. return false;
  344. });
  345. } else {
  346. Fast.api.ajax({
  347. url: Config.api_url + '/user/userinfotpl',
  348. type: 'post',
  349. data: {
  350. uid: userinfo.id,
  351. token: userinfo.token,
  352. version: Config.faversion,
  353. sid: Controller.api.sid()
  354. }
  355. }, function (tpldata, ret) {
  356. Layer.open({
  357. content: Template.render(tpldata, userinfo),
  358. area: area,
  359. title: __('Userinfo'),
  360. resize: false,
  361. btn: [__('Logout'), __('Close')],
  362. yes: function () {
  363. Fast.api.ajax({
  364. url: Config.api_url + '/user/logout',
  365. data: {
  366. uid: userinfo.id,
  367. token: userinfo.token,
  368. version: Config.faversion,
  369. sid: Controller.api.sid()
  370. }
  371. }, function (data, ret) {
  372. Controller.api.userinfo.set(null);
  373. Layer.closeAll();
  374. Layer.alert(ret.msg, {title: __('Warning'), icon: 0});
  375. }, function (data, ret) {
  376. Controller.api.userinfo.set(null);
  377. Layer.closeAll();
  378. Layer.alert(ret.msg, {title: __('Warning'), icon: 0});
  379. });
  380. }
  381. });
  382. return false;
  383. }, function (data) {
  384. Controller.api.userinfo.set(null);
  385. $(that).trigger('click');
  386. return false;
  387. });
  388. }
  389. });
  390. //刷新授权
  391. $(document).on("click", ".btn-authorization", function () {
  392. var userinfo = Controller.api.userinfo.get();
  393. if (!userinfo) {
  394. $(".btn-userinfo").trigger("click");
  395. return false;
  396. }
  397. Layer.confirm(__('Are you sure you want to refresh authorization?'), {icon: 3, title: __('Warmtips')}, function () {
  398. Fast.api.ajax({
  399. url: 'addon/authorization',
  400. data: {
  401. uid: userinfo.id,
  402. token: userinfo.token
  403. }
  404. }, function (data, ret) {
  405. $(".btn-refresh").trigger("click");
  406. Layer.closeAll();
  407. });
  408. });
  409. return false;
  410. });
  411. var install = function (name, version, force) {
  412. var userinfo = Controller.api.userinfo.get();
  413. var uid = userinfo ? userinfo.id : 0;
  414. var token = userinfo ? userinfo.token : '';
  415. Fast.api.ajax({
  416. url: 'addon/install',
  417. data: {
  418. name: name,
  419. force: force ? 1 : 0,
  420. uid: uid,
  421. token: token,
  422. version: version,
  423. faversion: Config.faversion
  424. }
  425. }, function (data, ret) {
  426. Layer.closeAll();
  427. Config['addons'][data.addon.name] = ret.data.addon;
  428. operate(data.addon.name, 'enable', false, function () {
  429. Layer.alert(__('Online installed tips') + (data.addon.testdata ? __('Testdata tips') : ""), {
  430. btn: data.addon.testdata ? [__('Import testdata'), __('Skip testdata')] : [__('OK')],
  431. title: __('Warning'),
  432. yes: function (index) {
  433. if (data.addon.testdata) {
  434. Fast.api.ajax({
  435. url: 'addon/testdata',
  436. data: {
  437. name: name,
  438. uid: uid,
  439. token: token,
  440. version: version,
  441. faversion: Config.faversion
  442. }
  443. }, function (data, ret) {
  444. Layer.close(index);
  445. });
  446. } else {
  447. Layer.close(index);
  448. }
  449. },
  450. icon: 1
  451. });
  452. Controller.api.refresh(table, name);
  453. });
  454. }, function (data, ret) {
  455. var area = Fast.config.openArea != undefined ? Fast.config.openArea : [$(window).width() > 650 ? '650px' : '95%', $(window).height() > 710 ? '710px' : '95%'];
  456. if (ret && ret.code === -2) {
  457. //如果登录已经超时,重新提醒登录
  458. if (uid && uid != ret.data.uid) {
  459. Controller.api.userinfo.set(null);
  460. $(".operate[data-name='" + name + "'] .btn-install").trigger("click");
  461. return;
  462. }
  463. top.Fast.api.open(ret.data.payurl, __('Pay now'), {
  464. area: area,
  465. end: function () {
  466. Fast.api.ajax({
  467. url: 'addon/isbuy',
  468. data: {
  469. name: name,
  470. force: force ? 1 : 0,
  471. uid: uid,
  472. token: token,
  473. version: version,
  474. faversion: Config.faversion
  475. }
  476. }, function () {
  477. top.Layer.alert(__('Pay successful tips'), {
  478. btn: [__('Continue installation')],
  479. title: __('Warning'),
  480. icon: 1,
  481. yes: function (index) {
  482. top.Layer.close(index);
  483. install(name, version);
  484. }
  485. });
  486. return false;
  487. }, function () {
  488. console.log(__('Canceled'));
  489. return false;
  490. });
  491. }
  492. });
  493. } else if (ret && ret.code === -3) {
  494. //插件目录发现影响全局的文件
  495. Layer.open({
  496. content: Template("conflicttpl", ret.data),
  497. shade: 0.8,
  498. area: area,
  499. title: __('Warning'),
  500. btn: [__('Continue install'), __('Cancel')],
  501. end: function () {
  502. },
  503. yes: function () {
  504. install(name, version, true);
  505. }
  506. });
  507. } else {
  508. Layer.alert(ret.msg, {title: __('Warning'), icon: 0});
  509. }
  510. return false;
  511. });
  512. };
  513. var uninstall = function (name, force, droptables) {
  514. Fast.api.ajax({
  515. url: 'addon/uninstall',
  516. data: {name: name, force: force ? 1 : 0, droptables: droptables ? 1 : 0}
  517. }, function (data, ret) {
  518. delete Config['addons'][name];
  519. Layer.closeAll();
  520. Controller.api.refresh(table, name);
  521. }, function (data, ret) {
  522. if (ret && ret.code === -3) {
  523. //插件目录发现影响全局的文件
  524. Layer.open({
  525. content: Template("conflicttpl", ret.data),
  526. shade: 0.8,
  527. area: area,
  528. title: __('Warning'),
  529. btn: [__('Continue uninstall'), __('Cancel')],
  530. end: function () {
  531. },
  532. yes: function () {
  533. uninstall(name, true, droptables);
  534. }
  535. });
  536. } else {
  537. Layer.alert(ret.msg, {title: __('Warning'), icon: 0});
  538. }
  539. return false;
  540. });
  541. };
  542. var operate = function (name, action, force, success) {
  543. Fast.api.ajax({
  544. url: 'addon/state',
  545. data: {name: name, action: action, force: force ? 1 : 0}
  546. }, function (data, ret) {
  547. var addon = Config['addons'][name];
  548. addon.state = action === 'enable' ? 1 : 0;
  549. Layer.closeAll();
  550. if (typeof success === 'function') {
  551. success(data, ret);
  552. }
  553. Controller.api.refresh(table, name);
  554. }, function (data, ret) {
  555. if (ret && ret.code === -3) {
  556. //插件目录发现影响全局的文件
  557. Layer.open({
  558. content: Template("conflicttpl", ret.data),
  559. shade: 0.8,
  560. area: area,
  561. title: __('Warning'),
  562. btn: [__('Continue operate'), __('Cancel')],
  563. end: function () {
  564. },
  565. yes: function () {
  566. operate(name, action, true, success);
  567. }
  568. });
  569. } else {
  570. Layer.alert(ret.msg, {title: __('Warning'), icon: 0});
  571. }
  572. return false;
  573. });
  574. };
  575. var upgrade = function (name, version) {
  576. var userinfo = Controller.api.userinfo.get();
  577. var uid = userinfo ? userinfo.id : 0;
  578. var token = userinfo ? userinfo.token : '';
  579. Fast.api.ajax({
  580. url: 'addon/upgrade',
  581. data: {name: name, uid: uid, token: token, version: version, faversion: Config.faversion}
  582. }, function (data, ret) {
  583. Config['addons'][name] = data.addon;
  584. Layer.closeAll();
  585. Controller.api.refresh(table, name);
  586. }, function (data, ret) {
  587. Layer.alert(ret.msg, {title: __('Warning')});
  588. return false;
  589. });
  590. };
  591. // 点击安装
  592. $(document).on("click", ".btn-install", function () {
  593. var that = this;
  594. var name = $(this).closest(".operate").data("name");
  595. var version = $(this).data("version");
  596. var userinfo = Controller.api.userinfo.get();
  597. var uid = userinfo ? userinfo.id : 0;
  598. if (parseInt(uid) === 0) {
  599. $(".btn-userinfo").trigger("click", name, version);
  600. return false;
  601. }
  602. install(name, version, false);
  603. });
  604. // 点击卸载
  605. $(document).on("click", ".btn-uninstall", function () {
  606. var name = $(this).closest(".operate").data('name');
  607. if (Config['addons'][name].state == 1) {
  608. Layer.alert(__('Please disable the add before trying to uninstall'), {icon: 7});
  609. return false;
  610. }
  611. Template.helper("__", __);
  612. tables = [];
  613. Layer.confirm(Template("uninstalltpl", {addon: Config['addons'][name]}), {focusBtn: false, title: __("Warning")}, function (index, layero) {
  614. uninstall(name, false, $("input[name='droptables']", layero).prop("checked"));
  615. });
  616. });
  617. // 点击配置
  618. $(document).on("click", ".btn-config", function () {
  619. var name = $(this).closest(".operate").data("name");
  620. Fast.api.open("addon/config?name=" + name, __('Setting'));
  621. });
  622. // 点击启用/禁用
  623. $(document).on("click", ".btn-enable,.btn-disable", function () {
  624. var name = $(this).data("name");
  625. var action = $(this).data("action");
  626. operate(name, action, false);
  627. });
  628. // 点击升级
  629. $(document).on("click", ".btn-upgrade", function () {
  630. var name = $(this).closest(".operate").data('name');
  631. if (Config['addons'][name].state == 1) {
  632. Layer.alert(__('Please disable the add before trying to upgrade'), {icon: 7});
  633. return false;
  634. }
  635. var version = $(this).data("version");
  636. Layer.confirm(__('Upgrade tips', Config['addons'][name].title), {title: __('Warmtips')}, function (index, layero) {
  637. upgrade(name, version);
  638. });
  639. });
  640. $(document).on("click", ".operate .btn-group .dropdown-toggle", function () {
  641. $(this).closest(".btn-group").toggleClass("dropup", $(document).height() - $(this).offset().top <= 200);
  642. });
  643. $(document).on("click", ".view-screenshots", function () {
  644. var row = Table.api.getrowbyindex(table, parseInt($(this).data("index")));
  645. var data = [];
  646. $.each(row.screenshots, function (i, j) {
  647. data.push({
  648. "src": j
  649. });
  650. });
  651. var json = {
  652. "title": row.title,
  653. "data": data
  654. };
  655. top.Layer.photos(top.JSON.parse(JSON.stringify({photos: json})));
  656. });
  657. },
  658. add: function () {
  659. Controller.api.bindevent();
  660. },
  661. config: function () {
  662. $(document).on("click", ".nav-group li a[data-toggle='tab']", function () {
  663. if ($(this).attr("href") == "#all") {
  664. $(".tab-pane").addClass("active in");
  665. }
  666. return;
  667. var type = $(this).attr("href").substring(1);
  668. if (type == 'all') {
  669. $(".table-config tr").show();
  670. } else {
  671. $(".table-config tr").hide();
  672. $(".table-config tr[data-group='" + type + "']").show();
  673. }
  674. });
  675. Controller.api.bindevent();
  676. },
  677. api: {
  678. formatter: {
  679. title: function (value, row, index) {
  680. if ($(".btn-switch.active").data("type") == "local") {
  681. // return value;
  682. }
  683. 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>';
  684. if (row.screenshots && row.screenshots.length > 0) {
  685. 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>';
  686. }
  687. return title;
  688. },
  689. intro: function (value, row, index) {
  690. 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>" : "");
  691. },
  692. operate: function (value, row, index) {
  693. return Template("operatetpl", {item: row, index: index});
  694. },
  695. toggle: function (value, row, index) {
  696. if (!row.addon) {
  697. return '';
  698. }
  699. 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>';
  700. },
  701. author: function (value, row, index) {
  702. var url = 'javascript:';
  703. if (typeof row.homepage !== 'undefined') {
  704. url = row.homepage;
  705. } else if (typeof row.qq !== 'undefined' && row.qq) {
  706. url = 'https://wpa.qq.com/msgrd?v=3&uin=' + row.qq + '&site=&menu=yes';
  707. }
  708. return '<a href="' + url + '" target="_blank" data-toggle="tooltip" class="text-primary">' + value + '</a>';
  709. },
  710. price: function (value, row, index) {
  711. if (isNaN(value)) {
  712. return value;
  713. }
  714. return parseFloat(value) == 0 ? '<span class="text-success">' + __('Free') + '</span>' : '<span class="text-danger">¥' + value + '</span>';
  715. },
  716. downloads: function (value, row, index) {
  717. return value;
  718. },
  719. version: function (value, row, index) {
  720. 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;
  721. },
  722. home: function (value, row, index) {
  723. 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>';
  724. },
  725. },
  726. bindevent: function () {
  727. Form.api.bindevent($("form[role=form]"));
  728. },
  729. userinfo: {
  730. get: function () {
  731. if (typeof $.cookie !== 'undefined') {
  732. var userinfo = $.cookie('fastadmin_userinfo');
  733. } else {
  734. var userinfo = sessionStorage.getItem("fastadmin_userinfo");
  735. }
  736. return userinfo ? JSON.parse(userinfo) : null;
  737. },
  738. set: function (data) {
  739. if (typeof $.cookie !== 'undefined') {
  740. if (data) {
  741. $.cookie("fastadmin_userinfo", JSON.stringify(data));
  742. } else {
  743. $.removeCookie("fastadmin_userinfo");
  744. }
  745. } else {
  746. if (data) {
  747. sessionStorage.setItem("fastadmin_userinfo", JSON.stringify(data));
  748. } else {
  749. sessionStorage.removeItem("fastadmin_userinfo");
  750. }
  751. }
  752. }
  753. },
  754. sid: function () {
  755. var sid = $.cookie('fastadmin_sid');
  756. if (!sid) {
  757. sid = Math.random().toString(20).substr(2, 12);
  758. $.cookie('fastadmin_sid', sid);
  759. }
  760. return sid;
  761. },
  762. refresh: function (table, name) {
  763. //刷新左侧边栏
  764. Fast.api.refreshmenu();
  765. //刷新行数据
  766. if ($(".operate[data-name='" + name + "']").length > 0) {
  767. var tr = $(".operate[data-name='" + name + "']").closest("tr[data-index]");
  768. var index = tr.data("index");
  769. var row = Table.api.getrowbyindex(table, index);
  770. row.addon = typeof Config['addons'][name] !== 'undefined' ? Config['addons'][name] : undefined;
  771. table.bootstrapTable("updateRow", {index: index, row: row});
  772. } else if ($(".btn-switch.active").data("type") == "local") {
  773. $(".btn-refresh").trigger("click");
  774. }
  775. }
  776. }
  777. };
  778. return Controller;
  779. });