listen.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // +----------------------------------------------------------------------
  2. // | Think.Admin
  3. // +----------------------------------------------------------------------
  4. // | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  5. // +----------------------------------------------------------------------
  6. // | 官方网站: http://think.ctolog.com
  7. // +----------------------------------------------------------------------
  8. // | 开源协议 ( https://mit-license.org )
  9. // +----------------------------------------------------------------------
  10. // | github开源项目:https://github.com/zoujingli/Think.Admin
  11. // +----------------------------------------------------------------------
  12. define(['jquery', 'admin.plugs'], function () {
  13. /*! 定义当前body对象 */
  14. this.$body = $('body');
  15. /*! 注册 data-load 事件行为 */
  16. this.$body.on('click', '[data-load]', function () {
  17. var url = $(this).attr('data-load'), tips = $(this).attr('data-tips');
  18. function _goLoad() {
  19. $.form.load(url, {}, 'GET', null, true, tips);
  20. }
  21. if ($(this).attr('data-confirm')) {
  22. return $.msg.confirm($(this).attr('data-confirm'), _goLoad);
  23. }
  24. return _goLoad.call(this);
  25. });
  26. /*! 注册 data-serach 表单搜索行为 */
  27. this.$body.on('submit', 'form.form-search', function () {
  28. var url = $(this).attr('action');
  29. var split = url.indexOf('?') === -1 ? '?' : '&';
  30. if ((this.method || 'get').toLowerCase() === 'get') {
  31. window.location.href = '#' + $.menu.parseUri(url + split + $(this).serialize());
  32. } else {
  33. $.form.load(url, this, 'post');
  34. }
  35. });
  36. /*! 注册 data-modal 事件行为 */
  37. this.$body.on('click', '[data-modal]', function () {
  38. return $.form.modal($(this).attr('data-modal'), 'open_type=modal', $(this).attr('data-title') || '编辑');
  39. });
  40. /*! 注册 data-open 事件行为 */
  41. this.$body.on('click', '[data-open]', function () {
  42. $.form.href($(this).attr('data-open'), this);
  43. });
  44. /*! 注册 data-reload 事件行为 */
  45. this.$body.on('click', '[data-reload]', function () {
  46. $.form.reload();
  47. });
  48. /*! 注册 data-check 事件行为 */
  49. this.$body.on('click', '[data-check-target]', function () {
  50. var checked = !!this.checked;
  51. $($(this).attr('data-check-target')).map(function () {
  52. this.checked = checked;
  53. });
  54. });
  55. /*! 注册 data-update 事件行为 */
  56. this.$body.on('click', '[data-update]', function () {
  57. var id = $(this).attr('data-update') || (function () {
  58. var data = [];
  59. return $($(this).attr('data-list-target') || 'input.list-check-box').map(function () {
  60. (this.checked) && data.push(this.value);
  61. }), data.join(',');
  62. }).call(this);
  63. if (id.length < 1) {
  64. return $.msg.tips('请选择需要操作的数据!');
  65. }
  66. var action = $(this).attr('data-action') || $(this).parents('[data-location]').attr('data-location');
  67. var value = $(this).attr('data-value') || 0, field = $(this).attr('data-field') || 'status';
  68. $.msg.confirm('确定要操作这些数据吗?', function () {
  69. $.form.load(action, {field: field, value: value, id: id}, 'POST');
  70. });
  71. });
  72. /*! 注册 data-href 事件行为 */
  73. this.$body.on('click', '[data-href]', function () {
  74. var href = $(this).attr('data-href');
  75. if (href && href.indexOf('#') !== 0) {
  76. window.location.href = href;
  77. }
  78. });
  79. /*! 注册 data-page-href 事件行为 */
  80. this.$body.on('click', 'a[data-page-href]', function () {
  81. window.location.href = '#' + $.menu.parseUri(this.href, this);
  82. });
  83. /*! 注册 data-file 事件行为 */
  84. this.$body.on('click', '[data-file]', function () {
  85. var type = $(this).attr('data-type') || 'jpg,png';
  86. var field = $(this).attr('data-field') || 'file';
  87. var method = $(this).attr('data-file') === 'one' ? 'one' : 'mtl';
  88. var title = $(this).attr('data-title') || '文件上传';
  89. var uptype = $(this).attr('data-uptype') || '';
  90. var url = window.ROOT_URL + '/index.php/admin/plugs/upfile/mode/' + method + '.html?mode=' + method + '&uptype=' + uptype + '&type=' + type + '&field=' + field;
  91. $.form.iframe(url, title || '文件管理');
  92. });
  93. /*! 注册 data-iframe 事件行为 */
  94. this.$body.on('click', '[data-iframe]', function () {
  95. $.form.iframe($(this).attr('data-iframe'), $(this).attr('data-title') || '窗口');
  96. });
  97. /*! 注册 data-icon 事件行为 */
  98. this.$body.on('click', '[data-icon]', function () {
  99. var field = $(this).attr('data-icon') || $(this).attr('data-field') || 'icon';
  100. var url = window.ROOT_URL + '/index.php/admin/plugs/icon.html?field=' + field;
  101. $.form.iframe(url, '图标选择');
  102. });
  103. /*! 注册 data-tips-image 事件行为 */
  104. this.$body.on('click', '[data-tips-image]', function () {
  105. var src = this.getAttribute('data-tips-image') || this.src, img = new Image();
  106. var imgWidth = this.getAttribute('data-width') || '480px';
  107. img.onload = function () {
  108. layer.open({
  109. type: 1, area: imgWidth, title: false, closeBtn: 1, skin: 'layui-layer-nobg', shadeClose: true,
  110. content: $(img).appendTo('body').css({background: '#fff', width: imgWidth, height: 'auto'}),
  111. end: function () {
  112. $(img).remove();
  113. }
  114. });
  115. };
  116. img.src = src;
  117. });
  118. /*! 注册 data-tips-text 事件行为 */
  119. this.$body.on('mouseenter', '[data-tips-text]', function () {
  120. var text = $(this).attr('data-tips-text');
  121. var placement = $(this).attr('data-tips-placement') || 'auto';
  122. $(this).tooltip({title: text, placement: placement}).tooltip('show');
  123. });
  124. /*! 注册 data-phone-view 事件行为 */
  125. this.$body.on('click', '[data-phone-view]', function () {
  126. var $container = $('<div class="mobile-preview pull-left"><div class="mobile-header">公众号</div><div class="mobile-body"><iframe id="phone-preview" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></iframe></div></div>').appendTo('body');
  127. $container.find('iframe').attr('src', this.getAttribute('data-phone-view') || this.href);
  128. layer.style(layer.open({type: 1, scrollbar: !1, area: ['330px', '600px'], title: !1, closeBtn: 1, skin: 'layui-layer-nobg', shadeClose: !!1,
  129. content: $container,
  130. end: function () {
  131. $container.remove();
  132. }
  133. }), {boxShadow: 'none'});
  134. });
  135. /*! 后台菜单控制初始化 */
  136. $.menu.listen();
  137. /*! 表单监听初始化 */
  138. $.validate.listen(this);
  139. });