file.library.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. ;(function ($, window, document, undefined) {
  2. /**
  3. * 文件库模块
  4. * @param trigger
  5. * @param options
  6. * @constructor
  7. */
  8. function FileLibrary(trigger, options) {
  9. // 配置项
  10. var defaults = {
  11. type: 'image',
  12. layerId: 'file-library'
  13. , layerSkin: 'file-library'
  14. };
  15. this.options = $.extend({}, defaults, options);
  16. // 触发对象
  17. this.$trigger = trigger;
  18. this.$touch = null; // 当前触发元素
  19. // 容器元素
  20. this.$element = null;
  21. // 初始化对象事件
  22. this.init();
  23. }
  24. FileLibrary.prototype = {
  25. /**
  26. * 初始化
  27. */
  28. init: function () {
  29. var _this = this;
  30. // 打开文件库事件
  31. _this.triggerEvent();
  32. },
  33. /**
  34. * 打开文件库事件
  35. */
  36. triggerEvent: function () {
  37. var _this = this;
  38. if (_this.$trigger !== false) {
  39. // 点击开启文件库弹窗
  40. _this.$trigger.unbind().click(function () {
  41. _this.$touch = $(this);
  42. _this.showLibraryModal();
  43. });
  44. } else {
  45. _this.showLibraryModal();
  46. }
  47. },
  48. /**
  49. * 显示文件库弹窗
  50. */
  51. showLibraryModal: function () {
  52. var _this = this;
  53. _this.getJsonData({group_id: -1}, function (data) {
  54. data.is_default = true;
  55. // 捕获页
  56. layer.open({
  57. type: 1
  58. , id: _this.options.layerId
  59. , title: '图片库'
  60. , skin: _this.options.layerSkin
  61. , area: '840px'
  62. , offset: 'auto'
  63. , anim: 1
  64. , closeBtn: 1
  65. , shade: 0.3
  66. , btn: ['确定', '取消']
  67. , content: template('tpl-file-library', data)
  68. , success: function (layero) {
  69. // 初始化文件库弹窗
  70. _this.initModal(layero);
  71. }
  72. , yes: function (index) {
  73. // 确认回调
  74. _this.done();
  75. layer.close(index);
  76. }
  77. });
  78. });
  79. },
  80. /**
  81. * 初始化文件库弹窗
  82. */
  83. initModal: function (element) {
  84. var _this = this;
  85. _this.$element = element;
  86. // 注册分组下拉选择组件
  87. _this.selectDropdown();
  88. // 注册分类切换事件
  89. _this.switchClassEvent();
  90. // 注册文件点击选中事件
  91. _this.selectFilesEvent();
  92. // 新增分组事件
  93. _this.addGroupEvent();
  94. // 编辑分组事件
  95. _this.editGroupEvent();
  96. // 删除分组事件
  97. _this.deleteGroupEvent();
  98. // 注册文件上传事件
  99. _this.uploadImagesEvent();
  100. // 注册文件删除事件
  101. _this.deleteFilesEvent();
  102. // 注册文件移动事件
  103. _this.moveFilesEvent();
  104. // 注册文件列表分页事件
  105. _this.fileListPage();
  106. },
  107. /**
  108. * 注册文件列表分页事件
  109. */
  110. fileListPage: function () {
  111. var _this = this;
  112. _this.$element.find('#file-list-body').on('click', '.switch-page', function () {
  113. var page = $(this).data('page');
  114. _this.renderFileList(page);
  115. });
  116. },
  117. /**
  118. * 文件移动事件
  119. */
  120. moveFilesEvent: function () {
  121. var _this = this
  122. , $groupSelect = _this.$element.find('.group-select');
  123. // 绑定文件选中事件
  124. $groupSelect.on('click', '.move-file-group', function () {
  125. $groupSelect.dropdown('close');
  126. var groupId = $(this).data('group-id')
  127. , fileIds = _this.getSelectedFileIds();
  128. if (fileIds.length === 0) {
  129. layer.msg('您还没有选择任何文件~', {offset: 't', anim: 6});
  130. return false;
  131. }
  132. layer.confirm('确定移动选中的文件吗?', {title: '友情提示'}, function (index) {
  133. var load = layer.load();
  134. $.post(STORE_URL + '/upload.library/moveFiles', {
  135. group_id: groupId
  136. , fileIds: fileIds
  137. }, function (result) {
  138. layer.msg(result.msg);
  139. if (result.code === 1) {
  140. _this.renderFileList();
  141. }
  142. layer.close(load);
  143. });
  144. layer.close(index);
  145. });
  146. });
  147. },
  148. /**
  149. * 删除选中的文件
  150. */
  151. deleteFilesEvent: function () {
  152. var _this = this;
  153. _this.$element.on('click', '.file-delete', function () {
  154. var fileIds = _this.getSelectedFileIds();
  155. if (fileIds.length === 0) {
  156. layer.msg('您还没有选择任何文件~', {offset: 't', anim: 6});
  157. return;
  158. }
  159. layer.confirm('确定删除选中的文件吗?', {title: '友情提示'}, function (index) {
  160. var load = layer.load();
  161. $.post(STORE_URL + '/upload.library/deleteFiles', {
  162. fileIds: fileIds
  163. }, function (result) {
  164. layer.close(load);
  165. if (result.code === 1) {
  166. _this.renderFileList();
  167. }
  168. });
  169. layer.close(index);
  170. });
  171. });
  172. },
  173. /**
  174. * 文件上传 (多文件)
  175. */
  176. uploadImagesEvent: function () {
  177. var _this = this;
  178. var loadIndex = null;
  179. // 文件大小
  180. var maxSize = 2;
  181. // 初始化Web Uploader
  182. var uploader = WebUploader.create({
  183. // 选完文件后,是否自动上传。
  184. auto: true,
  185. // 文件接收服务端。
  186. server: STORE_URL + '/upload/image',
  187. // 选择文件的按钮。可选。
  188. // 内部根据当前运行是创建,可能是input元素,也可能是flash.
  189. pick: {
  190. id: '.j-upload',
  191. multiple: true
  192. },
  193. // 文件上传域的name
  194. fileVal: 'iFile',
  195. // 图片上传前不进行压缩
  196. compress: false,
  197. // 允许重复
  198. duplicate: true,
  199. // 文件总数量
  200. // fileNumLimit: 10,
  201. // 文件大小2m => 2097152
  202. fileSingleSizeLimit: maxSize * 1024 * 1024,
  203. // 只允许选择图片文件。
  204. accept: {
  205. title: 'Images',
  206. extensions: 'gif,jpg,jpeg,bmp,png',
  207. mimeTypes: 'image/*'
  208. },
  209. // 文件上传header扩展
  210. headers: {
  211. 'Accept': 'application/json, text/javascript, */*; q=0.01',
  212. 'X-Requested-With': 'XMLHttpRequest'
  213. }
  214. });
  215. // 验证大小
  216. uploader.on('error', function (type) {
  217. if (type === 'F_DUPLICATE') {
  218. console.log('请不要重复选择文件!');
  219. } else if (type === 'F_EXCEED_SIZE') {
  220. alert('文件大小不可超过' + maxSize + 'm 哦!换个小点的文件吧!');
  221. }
  222. });
  223. // 文件上传前触发,添加附带参数
  224. uploader.on('uploadBeforeSend', function (obj, data) {
  225. data.group_id = _this.getCurrentGroupId();
  226. });
  227. // 文件上传成功,给item添加成功class, 用样式标记上传成功。
  228. uploader.on('uploadSuccess', function (file, response) {
  229. if (response.code === 1) {
  230. var $list = _this.$element.find('ul.file-list-item');
  231. $list.prepend(template('tpl-file-list-item', [response.data]));
  232. } else {
  233. uploader.uploadError(file, response);
  234. }
  235. });
  236. // 监听文件上传失败
  237. uploader.on('uploadError', function (file, reason) {
  238. uploader.uploadError(file, reason);
  239. });
  240. // 文件上传失败回调函数
  241. uploader.uploadError = function (file, reason) {
  242. layer.msg(reason.msg, {anim: 6});
  243. };
  244. // 文件开始上传
  245. uploader.on('startUpload', function () {
  246. loadIndex = layer.load();
  247. });
  248. // 文件上传结束
  249. uploader.on('uploadFinished', function () {
  250. layer.close(loadIndex);
  251. });
  252. },
  253. /**
  254. * 新增分组事件
  255. */
  256. addGroupEvent: function () {
  257. var _this = this
  258. , $groupList = _this.$element.find('.file-group > ul');
  259. _this.$element.on('click', '.group-add', function () {
  260. layer.prompt({title: '请输入新分组名称'}, function (value, index) {
  261. var load = layer.load();
  262. $.post(STORE_URL + '/upload.library/addGroup', {
  263. group_name: value,
  264. group_type: _this.options.type
  265. }, function (result) {
  266. layer.msg(result.msg);
  267. if (result.code === 1) {
  268. $groupList.append(template('tpl-group-item', result.data));
  269. var $groupSelectList = _this.$element.find('.group-select > .group-list');
  270. $groupSelectList.append(
  271. '<li>' +
  272. ' <a class="move-file-group" data-group-id="' + result.data.group_id + '"' +
  273. ' href="javascript:void(0);">' + result.data.group_name + '</a>' +
  274. '</li>'
  275. );
  276. }
  277. layer.close(load);
  278. });
  279. layer.close(index);
  280. });
  281. });
  282. },
  283. /**
  284. * 编辑分组事件
  285. */
  286. editGroupEvent: function () {
  287. var _this = this;
  288. _this.$element.find('.file-group').on('click', '.group-edit', function () {
  289. var $li = $(this).parent()
  290. , group_id = $li.data('group-id');
  291. layer.prompt({title: '修改分组名称', value: $li.attr('title')}, function (value, index) {
  292. var load = layer.load();
  293. $.post(STORE_URL + '/upload.library/editGroup', {
  294. group_id: group_id
  295. , group_name: value
  296. }, function (result) {
  297. layer.msg(result.msg);
  298. if (result.code === 1) {
  299. $li.attr('title', value).find('.group-name').text(value);
  300. var $groupSelectList = _this.$element.find('.group-select > .group-list');
  301. $groupSelectList.find('[data-group-id="' + group_id + '"]').text(value);
  302. }
  303. layer.close(load);
  304. });
  305. layer.close(index);
  306. });
  307. return false;
  308. });
  309. },
  310. /**
  311. * 删除分组事件
  312. */
  313. deleteGroupEvent: function () {
  314. var _this = this;
  315. _this.$element.find('.file-group').on('click', '.group-delete', function () {
  316. var $li = $(this).parent()
  317. , group_id = $li.data('group-id');
  318. layer.confirm('确定删除该分组吗?', {title: '友情提示'}, function (index) {
  319. var load = layer.load();
  320. $.post(STORE_URL + '/upload.library/deleteGroup', {
  321. group_id: group_id
  322. }, function (result) {
  323. layer.msg(result.msg);
  324. if (result.code === 1) {
  325. $li.remove();
  326. var $groupSelectList = _this.$element.find('.group-select > .group-list');
  327. $groupSelectList.find('[data-group-id="' + group_id + '"]').remove();
  328. }
  329. layer.close(load);
  330. });
  331. layer.close(index);
  332. });
  333. return false;
  334. });
  335. },
  336. /**
  337. * 注册文件选中事件
  338. */
  339. selectFilesEvent: function () {
  340. // 绑定文件选中事件
  341. this.$element.find('#file-list-body').on('click', '.file-list-item li', function () {
  342. $(this).toggleClass('active');
  343. });
  344. },
  345. /**
  346. * 分类切换事件
  347. */
  348. switchClassEvent: function () {
  349. var _this = this;
  350. // 注册分类切换事件
  351. _this.$element.find('.file-group').on('click', 'li', function () {
  352. var $this = $(this);
  353. // 切换选中状态
  354. $this.addClass('active').siblings('.active').removeClass('active');
  355. // 重新渲染文件列表
  356. _this.renderFileList();
  357. });
  358. },
  359. /**
  360. * 注册分组下拉选择组件
  361. */
  362. selectDropdown: function () {
  363. this.$element.find('.group-select').dropdown();
  364. },
  365. /**
  366. * 获取文件库列表数据
  367. * @param params
  368. * @param success
  369. */
  370. getJsonData: function (params, success) {
  371. var loadIndex = layer.load();
  372. typeof params === 'function' && (success = params);
  373. // 获取文件库列表
  374. $.getJSON(STORE_URL + '/upload.library/fileList', params, function (result) {
  375. layer.close(loadIndex);
  376. if (result.code === 1) {
  377. typeof success === 'function' && success(result.data);
  378. } else {
  379. layer.msg(result.msg, {anim: 6});
  380. }
  381. });
  382. },
  383. /**
  384. * 重新渲染文件列表
  385. * @param page
  386. */
  387. renderFileList: function (page) {
  388. var _this = this
  389. , groupId = this.getCurrentGroupId();
  390. // 重新渲染文件列表
  391. _this.getJsonData({group_id: groupId, page: page || 1}, function (data) {
  392. _this.$element.find('#file-list-body').html(template('tpl-file-list', data.file_list));
  393. });
  394. },
  395. /**
  396. * 获取选中的文件列表
  397. * @returns {Array}
  398. */
  399. getSelectedFiles: function () {
  400. var selectedList = [];
  401. this.$element.find('.file-list-item > li.active').each(function (index) {
  402. var $this = $(this);
  403. selectedList[index] = {
  404. file_id: $this.data('file-id')
  405. , file_path: $this.data('file-path')
  406. };
  407. });
  408. return selectedList;
  409. },
  410. /**
  411. * 获取选中的文件的ID集
  412. * @returns {Array}
  413. */
  414. getSelectedFileIds: function () {
  415. var fileList = this.getSelectedFiles();
  416. var data = [];
  417. fileList.forEach(function (item) {
  418. data.push(item.file_id);
  419. });
  420. return data;
  421. },
  422. /**
  423. * 获取当前分组id
  424. * @returns {*}
  425. */
  426. getCurrentGroupId: function () {
  427. return this.$element.find('.file-group > ul > li.active').data('group-id');
  428. },
  429. /**
  430. * 确认回调
  431. */
  432. done: function () {
  433. var selectedList = this.getSelectedFiles();
  434. selectedList.length > 0 && typeof this.options.done === 'function'
  435. && this.options.done(selectedList, this.$touch);
  436. }
  437. };
  438. // 在Jquery插件中使用FileLibrary对象
  439. $.fn.fileLibrary = function (options) {
  440. new FileLibrary(this, options);
  441. };
  442. // 在Jquery插件中使用FileLibrary对象
  443. $.fileLibrary = function (options) {
  444. new FileLibrary(false, options);
  445. };
  446. })(jQuery, window, document);