//jumpserver 自定义js 2015-01-29 //此函数用于checkbox的全选和反选 var checked=false; function check_all(form) { var checkboxes = document.getElementById(form); if (checked === false) { checked = true; } else { checked = false; } for (var i = 0; i < checkboxes.elements.length; i++) { if (checkboxes.elements[i].type == "checkbox") { checkboxes.elements[i].checked = checked; } } } function checkAll(id, name){ var checklist = document.getElementsByName(name); if(document.getElementById(id).checked) { for(var i=0;i" + data[0] + ": " + data[1] + "
" } var jumpserver = {}; jumpserver.checked = false; jumpserver.selected = {}; jumpserver.language = { processing: "加载中", search: "搜索", select: { rows: { _: "选中 %d 项", 0: "" } }, lengthMenu: "每页 _MENU_", info: "显示第 _START_ 至 _END_ 项结果; 总共 _TOTAL_ 项", infoFiltered: "", infoEmpty: "", zeroRecords: "没有匹配项", emptyTable: "没有记录", paginate: { first: "«", previous: "‹", next: "›", last: "»" } }; jumpserver.initDataTable = function (options) { // options = { // ele *: $('#dataTable_id'), // ajax_url *: '{% url 'users:user-list-api' %}', // columns *: [{data: ''}, ....], // dom: 'fltip', // i18n_url: '{% static "js/...../en-us.json" %}', // order: [[1, 'asc'], [2, 'asc'], ...], // buttons: ['excel', 'pdf', 'print'], // columnDefs: [{target: 0, createdCell: ()=>{}}, ...], // uc_html: 'header button', // op_html: 'div.btn-group?', // paging: true // } var ele = options.ele || $('.dataTable'); var columnDefs = [ { targets: 0, orderable: false, createdCell: function (td, cellData) { $(td).html(''.replace('99991937', cellData)); } }, {className: 'text-center', render: $.fn.dataTable.render.text(), targets: '_all'} ]; columnDefs = options.columnDefs ? options.columnDefs.concat(columnDefs) : columnDefs; var select = { style: 'multi', selector: 'td:first-child' }; var table = ele.DataTable({ pageLength: options.pageLength || 15, dom: options.dom || '<"#uc.pull-left">flt<"row m-t"<"col-md-8"<"#op.col-md-6"><"col-md-6 text-center"i>><"col-md-4"p>>', order: options.order || [], // select: options.select || 'multi', buttons: [], columnDefs: columnDefs, ajax: { url: options.ajax_url, dataSrc: "" }, columns: options.columns || [], select: options.select || select, language: jumpserver.language, lengthMenu: [[10, 15, 25, 50, -1], [10, 15, 25, 50, "All"]] }); table.on('select', function(e, dt, type, indexes) { var $node = table[ type ]( indexes ).nodes().to$(); $node.find('input.ipt_check').prop('checked', true); jumpserver.selected[$node.find('input.ipt_check').prop('id')] = true }).on('deselect', function(e, dt, type, indexes) { var $node = table[ type ]( indexes ).nodes().to$(); $node.find('input.ipt_check').prop('checked', false); jumpserver.selected[$node.find('input.ipt_check').prop('id')] = false }).on('draw', function(){ $('#op').html(options.op_html || ''); $('#uc').html(options.uc_html || ''); $('[data-toggle="popover"]').popover({ html: true, placement: 'bottom', // trigger: 'hover', container: 'body' }); }); $('.ipt_check_all').on('click', function() { if ($(this).prop("checked")) { $(this).closest('table').find('.ipt_check').prop('checked', true); jumpserver.checked = true; table.rows({search:'applied', page:'current'}).select(); } else { $(this).closest('table').find('.ipt_check').prop('checked', false); jumpserver.checked = false; table.rows({search:'applied', page:'current'}).deselect(); } }); return table; }; jumpserver.initStaticTable = function (selector) { $(selector).DataTable({ "searching": false, "bInfo": false, "paging": false, "order": [], "language": jumpserver.language }); }; jumpserver.initServerSideDataTable = function (options) { // options = { // ele *: $('#dataTable_id'), // ajax_url *: '{% url 'users:user-list-api' %}', // columns *: [{data: ''}, ....], // dom: 'fltip', // i18n_url: '{% static "js/...../en-us.json" %}', // order: [[1, 'asc'], [2, 'asc'], ...], // buttons: ['excel', 'pdf', 'print'], // columnDefs: [{target: 0, createdCell: ()=>{}}, ...], // uc_html: 'header button', // op_html: 'div.btn-group?', // paging: true // } var ele = options.ele || $('.dataTable'); var columnDefs = [ { targets: 0, orderable: false, createdCell: function (td, cellData) { $(td).html(''.replace('99991937', cellData)); } }, {className: 'text-center', targets: '_all'} ]; columnDefs = options.columnDefs ? options.columnDefs.concat(columnDefs) : columnDefs; var select = { style: 'multi', selector: 'td:first-child' }; var table = ele.DataTable({ pageLength: options.pageLength || 15, dom: options.dom || '<"#uc.pull-left">fltr<"row m-t"<"col-md-8"<"#op.col-md-6"><"col-md-6 text-center"i>><"col-md-4"p>>', order: options.order || [], buttons: [], columnDefs: columnDefs, serverSide: true, processing: true, ajax: { url: options.ajax_url , data: function (data) { delete data.columns; if (data.length !== null ){ data.limit = data.length; delete data.length; } if (data.start !== null) { data.offset = data.start; delete data.start; } if (data.search !== null) { var search_val = data.search.value; var search_list = search_val.split(" "); var search_attr = {}; var search_raw = []; search_list.map(function (val, index) { var kv = val.split(":"); if (kv.length === 2) { search_attr[kv[0]] = kv[1] } else { search_raw.push(kv) } }); data.search = search_raw.join(""); $.each(search_attr, function (k, v) { data[k] = v }) } if (data.order !== null && data.order.length === 1) { var col = data.order[0].column; var order = options.columns[col].data; if (data.order[0].dir = "desc") { order = "-" + order; } data.order = order; } }, dataFilter: function(data){ var json = jQuery.parseJSON( data ); json.recordsTotal = json.count; json.recordsFiltered = json.count; return JSON.stringify(json); // return JSON string }, dataSrc: "results" }, columns: options.columns || [], select: options.select || select, language: jumpserver.language, lengthMenu: [[10, 15, 25, 50], [10, 15, 25, 50]] }); table.selected = []; table.on('select', function(e, dt, type, indexes) { var $node = table[ type ]( indexes ).nodes().to$(); $node.find('input.ipt_check').prop('checked', true); jumpserver.selected[$node.find('input.ipt_check').prop('id')] = true; if (type === 'row') { var rows = table.rows(indexes).data(); $.each(rows, function (id, row) { if (row.id){ table.selected.push(row.id) } }) } }).on('deselect', function(e, dt, type, indexes) { var $node = table[ type ]( indexes ).nodes().to$(); $node.find('input.ipt_check').prop('checked', false); jumpserver.selected[$node.find('input.ipt_check').prop('id')] = false; if (type === 'row') { var rows = table.rows(indexes).data(); $.each(rows, function (id, row) { if (row.id){ var index = table.selected.indexOf(row.id); if (index > -1){ table.selected.splice(index, 1) } } }) } }).on('draw', function(){ $('#op').html(options.op_html || ''); $('#uc').html(options.uc_html || ''); var table_data = []; $.each(table.rows().data(), function (id, row) { if (row.id) { table_data.push(row.id) } }); $.each(table.selected, function (id, data) { var index = table_data.indexOf(data); if (index > -1){ table.rows(index).select() } }); }); var table_id = table.settings()[0].sTableId; $('#' + table_id + ' .ipt_check_all').on('click', function() { if ($(this).prop("checked")) { $(this).closest('table').find('.ipt_check').prop('checked', true); table.rows({search:'applied', page:'current'}).select(); } else { $(this).closest('table').find('.ipt_check').prop('checked', false); table.rows({search:'applied', page:'current'}).deselect(); } }); // jumpserver.table = table; return table; }; /** * 替换所有匹配exp的字符串为指定字符串 * @param exp 被替换部分的正则 * @param newStr 替换成的字符串 */ String.prototype.replaceAll = function (exp, newStr) { return this.replace(new RegExp(exp, "gm"), newStr); }; /** * 原型:字符串格式化 * @param args 格式化参数值 */ String.prototype.format = function(args) { var result = this; if (arguments.length < 1) { return result; } var data = arguments; if (arguments.length == 1 && typeof (args) == "object") { data = args; } for ( var key in data) { var value = data[key]; if (undefined != value) { result = result.replaceAll("\\{" + key + "\\}", value); } } return result; }; function setCookie(key, value) { var expires = new Date(); expires.setTime(expires.getTime() + (24 * 60 * 60 * 1000)); document.cookie = key + '=' + value + ';expires=' + expires.toUTCString() + ';path=/'; } function delCookie(key) { var expires = new Date(); expires.setTime(expires.getTime() - 1); var val = getCookie(key); if (val !== null) { document.cookie = key + '=' + val + ";expires" + expires.toUTCString() + ';path=/'; } } function createPopover(dataset, title, callback) { if (callback !== undefined){ var new_dataset = []; $.each(dataset, function (index, value) { new_dataset.push(callback(value)) }); dataset = new_dataset; } var data_content = dataset.join("
"); var html = "" + dataset.length + ""; return html; } $(function () { (function ($) { $.getUrlParam = function (name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return null; } })(jQuery); }); function getUrlParam(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return null; } function setUrlParam(url, name, value) { var urlArray = url.split("?"); if (urlArray.length===1){ url += "?" + name + "=" + value; } else { var oriParam = urlArray[1].split("&"); var oriParamMap = {}; $.each(oriParam, function (index, value) { var v = value.split("="); oriParamMap[v[0]] = v[1]; }); oriParamMap[name] = value; url = urlArray[0] + "?"; var newParam = []; $.each(oriParamMap, function (index, value) { newParam.push(index + "=" + value); }); url += newParam.join("&") } return url } // 校验密码-改变规则颜色 function checkPasswordRules(password, minLength) { if (wordMinLength(password, minLength)) { $('#rule_SECURITY_PASSWORD_MIN_LENGTH').css('color', 'green') } else { $('#rule_SECURITY_PASSWORD_MIN_LENGTH').css('color', '#908a8a') } if (wordUpperCase(password)) { $('#rule_SECURITY_PASSWORD_UPPER_CASE').css('color', 'green'); } else { $('#rule_SECURITY_PASSWORD_UPPER_CASE').css('color', '#908a8a') } if (wordLowerCase(password)) { $('#rule_SECURITY_PASSWORD_LOWER_CASE').css('color', 'green') } else { $('#rule_SECURITY_PASSWORD_LOWER_CASE').css('color', '#908a8a') } if (wordNumber(password)) { $('#rule_SECURITY_PASSWORD_NUMBER').css('color', 'green') } else { $('#rule_SECURITY_PASSWORD_NUMBER').css('color', '#908a8a') } if (wordSpecialChar(password)) { $('#rule_SECURITY_PASSWORD_SPECIAL_CHAR').css('color', 'green') } else { $('#rule_SECURITY_PASSWORD_SPECIAL_CHAR').css('color', '#908a8a') } } // 最小长度 function wordMinLength(word, minLength) { //var minLength = {{ min_length }}; var re = new RegExp("^(.{" + minLength + ",})$"); return word.match(re) } // 大写字母 function wordUpperCase(word) { return word.match(/([A-Z]+)/) } // 小写字母 function wordLowerCase(word) { return word.match(/([a-z]+)/) } // 数字字符 function wordNumber(word) { return word.match(/([\d]+)/) } // 特殊字符 function wordSpecialChar(word) { return word.match(/[`,~,!,@,#,\$,%,\^,&,\*,\(,\),\-,_,=,\+,\{,\},\[,\],\|,\\,;,',:,",\,,\.,<,>,\/,\?]+/) } // 显示弹窗密码规则 function popoverPasswordRules(password_check_rules, $el) { var message = ""; jQuery.each(password_check_rules, function (idx, rules) { message += "
  • " + rules.label + "
  • "; }); //$('#id_password_rules').html(message); $el.html(message) } // 初始化弹窗popover function initPopover($container, $progress, $idPassword, $el, password_check_rules, i18n_fallback){ options = {}; // User Interface options.ui = { container: $container, viewports: { progress: $progress //errors: $('.popover-content') }, showProgressbar: true, showVerdictsInsideProgressBar: true }; options.i18n = { fallback: i18n_fallback, t: function (key) { var result = ''; result = options.i18n.fallback[key]; return result === key ? '' : result; } }; $idPassword.pwstrength(options); popoverPasswordRules(password_check_rules, $el); }