thedesk/app/js/platform/first.js

214 lines
4.8 KiB
JavaScript
Raw Normal View History

2019-11-09 00:52:54 +11:00
document.title = 'TheDesk'
$.strip_tags = function(str, allowed) {
2019-05-19 17:39:30 +10:00
if (!str) {
2019-11-09 00:52:54 +11:00
return ''
2019-05-19 17:39:30 +10:00
}
2019-11-09 00:52:54 +11:00
allowed = (((allowed || '') + '').toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join('')
2019-05-19 17:39:30 +10:00
var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>?/gi,
2019-11-09 00:52:54 +11:00
commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi
return str.replace(commentsAndPhpTags, '').replace(tags, function($0, $1) {
return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : ''
})
}
2019-05-19 17:39:30 +10:00
function escapeHTML(str) {
if (!str) {
2019-11-09 00:52:54 +11:00
return ''
2018-12-09 05:46:01 +11:00
}
2019-11-09 00:52:54 +11:00
return str
.replace(/&/g, '&amp;')
2019-05-19 17:39:30 +10:00
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
2019-11-09 00:52:54 +11:00
.replace(/'/g, '&#039;')
2019-05-19 17:39:30 +10:00
}
//PHPのnl2brと同様
2018-12-09 05:46:01 +11:00
function nl2br(str) {
2019-05-19 17:39:30 +10:00
if (!str) {
2019-11-09 00:52:54 +11:00
return ''
2018-12-09 05:46:01 +11:00
}
2019-11-09 00:52:54 +11:00
str = str.replace(/\r\n/g, '<br />')
str = str.replace(/(\n|\r)/g, '<br />')
return str
2019-01-22 03:25:40 +11:00
}
2019-04-14 02:42:18 +10:00
function br2nl(str) {
2019-05-19 17:39:30 +10:00
if (!str) {
2019-11-09 00:52:54 +11:00
return ''
2019-04-14 02:42:18 +10:00
}
2019-11-09 00:52:54 +11:00
str = str.replace(/<br \/>/g, '\r\n')
return str
2019-04-14 02:42:18 +10:00
}
2019-05-19 17:39:30 +10:00
function formattime(date) {
2019-11-09 00:52:54 +11:00
var str = date.getFullYear() + '-'
2019-05-19 17:39:30 +10:00
if (date.getMonth() + 1 < 10) {
2019-11-09 00:52:54 +11:00
str = str + '0' + (date.getMonth() + 1) + '-'
2019-05-19 17:39:30 +10:00
} else {
2019-11-09 00:52:54 +11:00
str = str + (date.getMonth() + 1) + '-'
2019-05-19 17:39:30 +10:00
}
if (date.getDate() < 10) {
2019-11-09 00:52:54 +11:00
str = str + '0' + date.getDate()
2019-05-19 17:39:30 +10:00
} else {
str = str + date.getDate()
}
2019-11-09 00:52:54 +11:00
str = str + 'T'
2019-05-19 17:39:30 +10:00
if (date.getHours() < 10) {
2019-11-09 00:52:54 +11:00
str = str + '0' + date.getHours() + ':'
2019-05-19 17:39:30 +10:00
} else {
2019-11-09 00:52:54 +11:00
str = str + date.getHours() + ':'
2019-05-19 17:39:30 +10:00
}
if (date.getMinutes() < 10) {
2019-11-09 00:52:54 +11:00
str = str + '0' + date.getMinutes()
2019-05-19 17:39:30 +10:00
} else {
str = str + date.getMinutes()
2019-01-22 03:25:40 +11:00
}
2019-11-09 00:52:54 +11:00
return escapeHTML(str)
2019-01-22 03:25:40 +11:00
}
2019-05-19 17:39:30 +10:00
function formattimeutc(date) {
2019-11-09 00:52:54 +11:00
var str = date.getUTCFullYear() + '-'
2019-05-19 17:39:30 +10:00
if (date.getUTCMonth() + 1 < 10) {
2019-11-09 00:52:54 +11:00
str = str + '0' + (date.getUTCMonth() + 1) + '-'
2019-05-19 17:39:30 +10:00
} else {
2019-11-09 00:52:54 +11:00
str = str + (date.getUTCMonth() + 1) + '-'
2019-05-19 17:39:30 +10:00
}
if (date.getUTCDate() < 10) {
2019-11-09 00:52:54 +11:00
str = str + '0' + date.getUTCDate()
2019-05-19 17:39:30 +10:00
} else {
str = str + date.getUTCDate()
}
2019-11-09 00:52:54 +11:00
str = str + 'T'
2019-05-19 17:39:30 +10:00
if (date.getUTCHours() < 10) {
2019-11-09 00:52:54 +11:00
str = str + '0' + date.getUTCHours() + ':'
2019-05-19 17:39:30 +10:00
} else {
2019-11-09 00:52:54 +11:00
str = str + date.getUTCHours() + ':'
2019-05-19 17:39:30 +10:00
}
if (date.getUTCMinutes() < 10) {
2019-11-09 00:52:54 +11:00
str = str + '0' + date.getUTCMinutes()
2019-05-19 17:39:30 +10:00
} else {
str = str + date.getUTCMinutes()
2019-01-22 03:25:40 +11:00
}
2019-11-09 00:52:54 +11:00
return escapeHTML(str)
2019-03-08 05:19:26 +11:00
}
2019-11-09 00:52:54 +11:00
postMessage(['sendSinmpleIpc', 'custom-css-request'], '*')
2019-05-19 17:39:30 +10:00
function makeCID() {
2019-11-09 00:52:54 +11:00
return (
randomStr(8) +
'-' +
randomStr(4) +
'-' +
randomStr(4) +
'-' +
randomStr(4) +
'-' +
randomStr(12)
)
2019-03-08 05:19:26 +11:00
}
2019-05-19 17:39:30 +10:00
function randomStr(l) {
2019-03-08 05:19:26 +11:00
// 生成する文字列に含める文字セット
2019-11-09 00:52:54 +11:00
var c = 'abcdefghijklmnopqrstuvwxyz0123456789'
var cl = c.length
var r = ''
2019-05-19 17:39:30 +10:00
for (var i = 0; i < l; i++) {
2019-11-09 00:52:54 +11:00
r += c[Math.floor(Math.random() * cl)]
2019-03-08 05:19:26 +11:00
}
2019-11-09 00:52:54 +11:00
return r
2019-03-08 05:19:26 +11:00
}
2019-05-19 17:39:30 +10:00
function rgbToHex(color) {
// HEXに変換したものを代入する変数
2019-11-09 00:52:54 +11:00
var hex = ''
2019-05-19 17:39:30 +10:00
// 第1引数がHEXのとき変換処理は必要ないのでそのままreturn
// IE8の場合はjQueryのcss()関数でHEXを返すので除外
if (color.match(/^#[a-f\d]{3}$|^#[a-f\d]{6}$/i)) {
2019-11-09 00:52:54 +11:00
return color
2019-05-19 17:39:30 +10:00
}
// 正規表現
2019-11-09 00:52:54 +11:00
var regex = color.match(/^rgb\(([0-9.]+),\s*([0-9.]+),\s*([0-9.]+)\)$/)
2019-05-19 17:39:30 +10:00
// 正規表現でマッチしたとき
if (regex) {
2019-11-09 00:52:54 +11:00
var rgb = [
// RGBからHEXへ変換
parseInt(regex[1]).toString(16),
parseInt(regex[2]).toString(16),
parseInt(regex[3]).toString(16)
]
2019-05-19 17:39:30 +10:00
for (var i = 0; i < rgb.length; ++i) {
// rgb(1,1,1)のようなときHEXに変換すると1桁になる
// 1桁のときは前に0を足す
if (rgb[i].length == 1) {
2019-11-09 00:52:54 +11:00
rgb[i] = '0' + rgb[i]
2019-05-19 17:39:30 +10:00
}
2019-11-09 00:52:54 +11:00
hex += rgb[i]
2019-05-19 17:39:30 +10:00
}
2019-11-09 00:52:54 +11:00
return hex
2019-05-19 17:39:30 +10:00
}
2019-11-09 00:52:54 +11:00
console.error(color + ':第1引数はRGB形式で入力')
2019-05-19 16:17:05 +10:00
}
2019-05-19 17:39:30 +10:00
/*マルチバイト用切り出し*/
2019-11-09 00:52:54 +11:00
$.isSurrogatePear = function(upper, lower) {
return 0xd800 <= upper && upper <= 0xdbff && 0xdc00 <= lower && lower <= 0xdfff
}
$.mb_strlen = function(str) {
2019-11-11 03:20:51 +11:00
var splitter = new GraphemeSplitter()
var arr = splitter.splitGraphemes(str)
return arr.length
2019-11-09 00:52:54 +11:00
}
$.mb_substr = function(str, begin, end) {
2019-11-11 03:20:51 +11:00
//配列にする
var splitter = new GraphemeSplitter()
var arr = splitter.splitGraphemes(str)
var newarr = []
for(var i = 0; i < arr.length; i++){
if(i >= begin && i <= end){
newarr.push(arr[i])
2019-05-19 17:39:30 +10:00
}
}
2019-11-11 03:20:51 +11:00
return newarr.join('')
2019-11-09 00:52:54 +11:00
}
2019-07-29 01:42:17 +10:00
//ソートするやつ
function object_array_sort(data, key, order, fn) {
2019-11-09 00:52:54 +11:00
var num_a = -1
var num_b = 1
2019-07-29 01:42:17 +10:00
if (order === 'asc') {
2019-11-09 00:52:54 +11:00
num_a = 1
num_b = -1
}
data = data.sort(function(a, b) {
var x = a[key]
var y = b[key]
if (x > y) return num_a
if (x < y) return num_b
return 0
})
var arrObj = {}
2019-07-29 01:42:17 +10:00
for (var i = 0; i < data.length; i++) {
2019-11-09 00:52:54 +11:00
arrObj[data[i]['family']] = data[i]
2019-07-29 01:42:17 +10:00
}
2019-11-09 00:52:54 +11:00
data = []
2019-07-29 01:42:17 +10:00
for (var key in arrObj) {
2019-11-09 00:52:54 +11:00
data.push(arrObj[key])
2019-07-29 01:42:17 +10:00
}
2019-11-09 00:52:54 +11:00
fn(data)
2019-07-29 01:42:17 +10:00
}
2019-11-09 00:52:54 +11:00
function setLog(txt1, txt2, txt3) {
2019-10-31 02:30:26 +11:00
//url,statuscode,responsetext
var text = new Date().toUTCString()
2019-11-09 00:52:54 +11:00
text = text + ',' + txt1 + ',' + txt2 + ',' + escapeCsv(txt3)
2019-10-31 02:30:26 +11:00
console.error(text)
2019-11-09 00:52:54 +11:00
postMessage(['log', text], '*')
2019-10-31 02:30:26 +11:00
}
function escapeCsv(str) {
2019-11-09 00:52:54 +11:00
if (!str) {
return str
}
var result
result = str.replace(/\"/g, '""')
if (result.indexOf(',') >= 0) {
result = '"' + result + '"'
}
return result
}