| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- define(['yzn'], function(Yzn) {
- var Backend = {
- api: {
- //暂时不支持顶部菜单模式
- sidebar: function (params) {
- colorArr = ['red', 'orange', 'green', 'cyan', 'black', 'gray', 'blue'];
- $colorNums = colorArr.length;
- badgeList = {};
- $.each(params, function (k, v) {
- $url = Yzn.api.fixurl(k);
- if ($.isArray(v)) {
- $nums = typeof v[0] !== 'undefined' ? v[0] : 0;
- $color = typeof v[1] !== 'undefined' ? v[1] : colorArr[(!isNaN($nums) ? $nums : $nums.length) % $colorNums];
- $class = typeof v[2] !== 'undefined' ? v[2] : 'layui-badge';
- } else {
- $nums = v;
- $color = colorArr[(!isNaN($nums) ? $nums : $nums.length) % $colorNums];
- $class = 'layui-badge';
- }
- //必须nums大于0才显示
- badgeList[$url] = $nums > 0 ? '<small class="' + $class + ' layui-bg-' + $color + '">' + $nums + '</small>' : '';
- });
- $.each(badgeList, function (k, v) {
- var anchor = top.window.$("#sideMenu a.site-demo-active[menu-url='" + k + "']");
- if (anchor) {
- top.window.$(".pull-right-container", anchor).replaceWith(v);
- }
- });
- },
- replaceids: function(elem, url) {
- //如果有需要替换ids的
- if (url.indexOf("{ids}") > -1) {
- var ids = 0;
- var tableId = $(elem).data("table");
- if (tableId && $("#" + tableId).length > 0) {
- var Table = require("table");
- ids = Table.api.selectedids(tableId).join(",");
- }
- url = url.replace(/\{ids\}/g, ids);
- }
- return url;
- },
- addtabs: function(url, title, id) {
- url = Yzn.api.fixurl(url);
- if (parent.pearAdmin) {
- parent.pearAdmin.addTab(id, title, url);
- } else if (window.top.pearAdmin) {
- window.top.pearAdmin.addTab(id, title, url);
- }
- },
- refreshmenu: function () {
- top.window.$(".pear-menu").trigger("refresh");
- },
- gettablecolumnbutton: function (options) {
- if (typeof options.table !== 'undefined' && typeof options.tableKey !== 'undefined' && typeof options.buttonIndex !== 'undefined') {
- var tableOptions = layui.table.getOptions(options.table);
- if (tableOptions) {
- var columnObj = null;
- $.each(tableOptions.cols, function (i, columns) {
- $.each(columns, function (j, column) {
- if (typeof column.key !== 'undefined' && column.key === options.tableKey) {
- columnObj = column;
- return false;
- }
- });
- if (columnObj) {
- return false;
- }
- });
- if (columnObj) {
- return columnObj['operat'][options.buttonIndex];
- }
- }
- }
- return null;
- },
- },
- init: function() {
- //配置Toastr的参数
- Toastr.options.positionClass = Config.controllername === 'index' ? "toast-top-right-index" : "toast-top-right";
- //点击包含.btn-dialog的元素时弹出dialog
- $(document).on('click', '.btn-dialog,.dialogit', function(e) {
- var that = this;
- var options = $.extend({}, $(that).data() || {});
- var url = Backend.api.replaceids(that, $(that).data("url") || $(that).attr('href'));
- var title = $(that).attr("title") || $(that).data("title") || $(that).data('original-title');
- var button = Backend.api.gettablecolumnbutton(options);
- if (button && typeof button.callback === 'function') {
- options.callback = button.callback;
- }
- if (typeof options.confirm !== 'undefined') {
- Layer.confirm(options.confirm, function(index) {
- Backend.api.open(url, title, options);
- Layer.close(index);
- });
- } else {
- window[$(that).data("window") || 'self'].Backend.api.open(url, title, options);
- }
- return false;
- });
- //点击包含.btn-addtabs的元素时新增选项卡
- $(document).on('click', '.btn-addtabs,.addtabsit', function(e) {
- var that = this;
- var options = $.extend({}, $(that).data() || {});
- var url = Backend.api.replaceids(that, $(that).data("url") || $(that).attr('href'));
- var title = $(that).attr("title") || $(that).data("title") || $(that).data('original-title');
- var id = $(that).data("menu-id") || Math.floor(new Date().valueOf() * Math.random());
- if (!$(this).attr('data-menu-id')) {
- $(this).attr('data-menu-id', id);
- }
- if (typeof options.confirm !== 'undefined') {
- Layer.confirm(options.confirm, function(index) {
- Backend.api.addtabs(url, title, id);
- Layer.close(index);
- });
- } else {
- Backend.api.addtabs(url, title, id);
- }
- return false;
- });
- //点击包含.btn-ajax的元素时发送Ajax请求
- $(document).on('click', '.btn-ajax,.ajaxit', function(e) {
- var that = this;
- var options = $.extend({}, $(that).data() || {});
- if (typeof options.url === 'undefined' && $(that).attr("href")) {
- options.url = $(that).attr("href");
- }
- options.url = Backend.api.replaceids(this, options.url);
- var success = typeof options.success === 'function' ? options.success : null;
- var error = typeof options.error === 'function' ? options.error : null;
- delete options.success;
- delete options.error;
- var button = Backend.api.gettablecolumnbutton(options);
- if (button) {
- if (typeof button.success === 'function') {
- success = button.success;
- }
- if (typeof button.error === 'function') {
- error = button.error;
- }
- }
- //如果未设备成功的回调,设定了自动刷新的情况下自动进行刷新
- if (!success && typeof options.table !== 'undefined' && typeof options.refresh !== 'undefined' && options.refresh) {
- success = function() {
- layui.table.reload(options.table);
- }
- }
- if (typeof options.confirm !== 'undefined') {
- Layer.confirm(options.confirm, function(index) {
- Backend.api.ajax(options, success, error);
- Layer.close(index);
- });
- } else {
- Backend.api.ajax(options, success, error);
- }
- return false;
- });
- $(document).on('click', '.btn-click,.clickit', function (e) {
- var that = this;
- var options = $.extend({}, $(that).data() || {});
- var row = {};
- if (typeof options.table !== 'undefined') {
- var index = parseInt(options.rowIndex);
- var data = layui.table.getData(options.table)
- row = typeof data[index] !== 'undefined' ? data[index] : {};
- }
- var button = Backend.api.gettablecolumnbutton(options);
- var click = typeof button.click === 'function' ? button.click : $.noop;
- if (typeof options.confirm !== 'undefined') {
- Layer.confirm(options.confirm, function (index) {
- click.apply(that, [options, row, button]);
- Layer.close(index);
- });
- } else {
- click.apply(that, [options, row, button]);
- }
- return false;
- });
- // 放大图片
- $('body').on('click', '[data-image]', function() {
- var title = $(this).attr('data-image'),
- src = $(this).attr('src'),
- alt = $(this).attr('alt');
- var photos = {
- "title": title,
- "id": Math.random(),
- "data": [{
- "alt": alt,
- "pid": Math.random(),
- "src": src,
- "thumb": src
- }]
- };
- Layer.photos({
- photos: photos,
- anim: 5
- });
- return false;
- });
- var tips_index = 0;
- $(document).on('mouseenter', '[lay-tips]', function() {
- var content = $(this).attr('lay-tips');
- var direction = $(this).attr('lay-tips-direction') || 'top';
- var bgColor = $(this).attr('lay-tips-bg');
- var tipsMap = {
- 'top': 1,
- 'right': 2,
- 'bottom': 3,
- 'left': 4
- };
- var tipsDirection = tipsMap[direction] || 1;
- var options = {
- time: 0
- };
- // 如果有背景色,使用数组格式
- if (bgColor) {
- options.tips = [tipsDirection, bgColor];
- } else {
- options.tips = tipsDirection;
- }
-
- tips_index = layer.tips(content, this, options);
- }).on('mouseleave', '[lay-tips]', function() {
- layer.close(tips_index);
- });
- //修复含有fixed-footer类的body边距
- if ($(".fixed-footer").length > 0) {
- $(document.body).css("padding-bottom", $(".fixed-footer").outerHeight());
- }
- //修复不在iframe时layer-footer隐藏的问题
- if ($(".layer-footer").length > 0 && self === top) {
- $(".layer-footer").show();
- }
- }
- }
- Backend.api = $.extend(Yzn.api, Backend.api);
- //将Backend渲染至全局,以便于在子框架中调用
- window.Backend = Backend;
- Backend.init();
- return Backend;
- })
|