var isProduction = ~location.host.indexOf('zhixinhuixue.com');
//判断是新建答题卡,还是根据已经有的试卷信息生成答题卡
//GetQueryString('new') === 'true';
var isNewBuild = ~location.href.indexOf('online/third')
if(localStorage.getItem('grade')==undefined){
localStorage.setItem('grade',1);
}
String.prototype.substitute = function(data) {
if (data && typeof data == 'object') {
return this.replace(/\{([^{}]+)\}/g, function(match, key) {
var value = data[key]
return value !== undefined ? '' + value : ''
})
} else {
return this.toString()
}
}
String.prototype.clearFixible = function(){
return this.replace(/
<\/i><\/div>/g,'')
}
function GetQueryString(name) {
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
var r = window.location.search.substr(1).match(reg) //search,查询?后面的参数,并匹配正则
if (r != null) return unescape(r[2])
return null
}
//将base64转换为文件对象
function dataURLtoFile(dataurl, filename) {
var arr = dataurl.split(',')
var mime = arr[0].match(/:(.*?);/)[1]
var bstr = atob(arr[1])
var n = bstr.length
var u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
//转换成file对象
return new File([u8arr], filename, { type: mime })
//转换成成blob对象
//return new Blob([u8arr], { type: mime })
}
function UnitConversion() {
/**
* 获取DPI
* @returns {Array}
*/
this.conversion_getDPI = function() {
var arrDPI = new Array()
if (window.screen.deviceXDPI) {
arrDPI[0] = window.screen.deviceXDPI
arrDPI[1] = window.screen.deviceYDPI
} else {
var tmpNode = document.createElement('DIV')
tmpNode.style.cssText =
'width:1in;height:1in;position:absolute;left:0px;top:0px;z-index:99;visibility:hidden'
document.body.appendChild(tmpNode)
arrDPI[0] = parseInt(tmpNode.offsetWidth)
arrDPI[1] = parseInt(tmpNode.offsetHeight)
tmpNode.parentNode.removeChild(tmpNode)
}
return arrDPI
}
/**
* px转换为mm
* @param value
* @returns {number}
*/
this.pxConversionMm = function(value) {
var inch = value / this.conversion_getDPI()[0]
var c_value = inch * 25.4
return c_value
}
/**
* mm转换为px
* @param value
* @returns {number}
*/
this.mmConversionPx = function(value) {
/**
* in 英尺单位 1in = 25.4 mm
*/
var inch = value / 25.4
var c_value = inch * this.conversion_getDPI()[0]
return c_value
}
}
//复选
function CheckBoxItem($checkBox, allFn, singleFn) {
this.allFn = allFn || function() {}
this.singleFn = singleFn || function() {}
this.$checBox = $checkBox
//除了 全选 禁用按钮以外的其他按钮的集合
this.totalCount = this.$checBox.find(
'.h_checkItem:not(".checkAll"):not(".disabled")'
).length
this.checkedItemsCount = 0
this.bindEvent()
}
CheckBoxItem.prototype.bindEvent = function() {
var self = this
this.$checBox.on('click', '.h_checkItem', function() {
var isDisabled = $(this).hasClass('disabled')
var isChecked = $(this).hasClass('checked')
var isCheckAllEl = $(this).hasClass('checkAll')
if (isDisabled) return
$(this)[isChecked ? 'removeClass' : 'addClass']('checked')
if (isCheckAllEl) {
var checkItems = $(this).siblings('.h_checkItem')
checkItems[isChecked ? 'removeClass' : 'addClass']('checked')
self.checkedItemsCount = isChecked ? 0 : checkItems.length
self.allFn($(this), !isChecked)
} else {
var checkAll = $(this).siblings('.checkAll')
!isChecked ? self.checkedItemsCount++ : self.checkedItemsCount--
var isCheckAllStatus = self.checkedItemsCount >= self.totalCount
checkAll[isCheckAllStatus ? 'addClass' : 'removeClass']('checked')
self.singleFn($(this), !isChecked)
}
})
}
// 单选
function RadioBoxItem($radioBox, fn) {
this.$radioBox = $radioBox
this.cb = fn
this.bindEvent()
}
RadioBoxItem.prototype.bindEvent = function() {
var self = this
this.$radioBox.on('click', '.h_radioItem', function() {
var isDisabled = $(this).hasClass('disabled')
var isChecked = $(this).hasClass('checked')
if (isDisabled) return
$(this)
.addClass('checked')
.siblings('.h_radioItem')
.removeClass('checked')
self.cb && self.cb($(this))
})
}
// 切换
function Switch($switch, fn, defaultstatus) {
this.$switch = $switch
this.status = defaultstatus || false
this.cb = fn || function() {}
this.bindEvent()
}
Switch.prototype.bindEvent = function() {
var self = this
this.$switch.click(function() {
$(this).toggleClass('open')
self.status = !self.status
self.cb && self.cb(self.status)
})
}
//layer使用判断
window.hgc_layer = layui.layer
window.logs = isProduction?window.console.log:function(){}
function simpleCopy(obj) {
return JSON.parse(JSON.stringify(obj))
}
//公共弹框
var hgc_modal = {
tpl:
'\
\
{title}X
\
{content}
\
\
{ensureText}\
{cancelText}\
\
\
',
init: function(data) {
this.title = data.title || '消息提示'
this.conetnt = data.content || '提示消息'
this.ensureText = data.ensureText || '确定'
this.cancelText = data.cancelText || '取消'
this.sureCb = data.sureCb || function() {}
this.cancelCb = data.cancelCb || function() {}
this.afterCb = data.afterCb || function() {}
this.render()
this.initDom()
this.bindEvent()
},
initDom: function() {
this.$modalBox = $('.hgc_modalBox')
},
render: function(content) {
var self = this
$('body').append(
self.tpl.substitute({
content: self.conetnt,
title: self.title ,
ensureText:self.ensureText,
cancelText:self.cancelText
})
)
self.afterCb && self.afterCb()
},
bindEvent: function() {
var self = this
self.$modalBox.find('.modalBtns .sure').click(function() {
self.sureCb()
self.$modalBox.remove()
})
self.$modalBox.find('.modalBtns .cancel').click(function() {
self.$modalBox.remove()
self.cancelCb()
})
self.$modalBox.find('h2 .close').click(function() {
self.$modalBox.remove()
})
}
}
//阿拉伯数字和中文数字转换
function SectionToChinese(section) {
var chnNumChar = ['', '一', '二', '三', '四', '五', '六', '七', '八', '九']
var chnUnitChar = ['', '十', '百', '千']
var strIns = '',chnStr = ''
var unitPos = 0
var zero = true
while (section > 0) {
var v = section % 10
if (v === 0) {
if (!zero) {
zero = true
chnStr = chnNumChar[v] + chnStr
}
} else {
zero = false
strIns = chnNumChar[v]
strIns += chnUnitChar[unitPos]
chnStr = strIns + chnStr
}
unitPos++
section = Math.floor(section / 10)
}
return chnStr
}
window.logs = isProduction?window.console.log:function(){}
function simpleCopy(obj) {
return JSON.parse(JSON.stringify(obj))
}
/**
* log 日志函数
* 可以配置过滤参数
*/
window.log = function(obj,filterKey){
if(Object.prototype.toString.call(obj) === '[object Object]'){
var filterFn = filterKey?function(key,val){
if(key === filterKey){
return null
}
return val
}:null
console.log(JSON.stringify(obj,filterFn,4))
}else{
console.log(obj)
}
}
//全局生成唯一key
var uid = 0;
function guid() {
var keys = Print.questionMap?Print.questionMap:Print.getBuildQuestions()
var uidStart = Object.keys(keys).length;
return 'modelId'+ ++uidStart
};
function _symbolId() {
return Math.random().toString(36).substr(3,10);
}
//字符串转base64
function encode(str){
// 对字符串进行编码
var encode = encodeURI(str);
// 对编码的字符串转化base64
var base64 = btoa(encode);
return base64;
};
// base64转字符串
function decode(base64){
// 对base64转编码
var decode = atob(base64);
// 编码转字符串
var str = decodeURI(decode);
return str;
}
;function isBase64 (str){
if(!str || !str.trim()) return false;
try {
return btoa(atob(str)) == str
} catch (error) {
return false
}
};