用ES6语法写的btnSelect下拉按钮

btnSelect下拉按钮

按钮支持多个按钮关联,带联想功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// 下拉选择按钮 btnSelect
/* 暴露btnSelect类,假定全局存在jQuery */
export default class btnSelect {
constructor () {
this.btnSelectClass = ".btn__select"
}
init () {
var btnSelectList = {}
$(this.btnSelectClass).each(function(){
var name = $(this).children('input[type="hidden"]').attr("name");
var btnSelect = new btnSelectClass($(this))
btnSelectList[name] = btnSelect
btnSelect.init()
})
window.btnSelectList = btnSelectList
var btnSelectRelations = new btnSelectRelation(["province","city"])
btnSelectRelations.init()
}
}

/**
* 注册的组件放在window.btnSelectList下 取input[type='hidden']的name值为键名
<span class="btn__select">
<input type="text" placeholder="专业">
<input type="hidden" name="qwe">
<button type="button"></button>
<ul>
<li name="caa">123</li>
<li name="tafa">456</li>
<li name="xafa">789</li>
</ul>
</span>
*
* 简写 在实例化的时候会补全,不过这样是没有初始数据的
<span class="btn__select">
<input type="text" placeholder="专业">
<input type="hidden" name="qwe">
</span>
*
* 导入数据
window.btnSelectList.qwe.set({
caa: "中国美术学院",
tafa: "天津美术学院",
xafa: "西安美术学院"
})
*
* 获取
window.btnSelectList.qwe.get()
*
*/
class btnSelectClass {
constructor (element, data = null){
this.btn__select = element
this.data = data
this.preText = ""
this.name = undefined
this.btnSelectRelation = null
this.set = data => {
this.data = data
this.reBuildDom(data)
}
this.get = () => this.btn__select.children("input[type='hidden']").val()
}

init () {
var _this = this
this.data == null && this.buildBtnSelect()
// 选择 取值
this.btn__select.on("click","li",function(){
_this.setValue($(this).attr("name"), $(this).text())
typeof _this.btnSelectRelation === "function" && _this.btnSelectRelation(_this.name,$(this).attr("name"))
})
// 监听输入动作,触发搜索功能
this.btn__select.children("input[type='text']").keyup(e => {
var val = $(e.target).val()
val !== this.preText && this.search(val)
})
this.setName()
}

setValue (name, value) {
this.btn__select.children("input[type='text']").val(value)
this.btn__select.children("input[type='hidden']").val(name)
}

setName () {
this.name = this.btn__select.children('input[type="hidden"]').attr("name")
}

buildBtnSelect () {
var ul = this.btn__select.children("ul")
var button = this.btn__select.children("button")
button.length == 0 && this.btn__select.children("input[type='hidden']").after('<button type="button"></button>')
if(ul.length == 0){
this.btn__select.append("<ul></ul>")
this.data = {}
}else{
var data = {}
var items = this.btn__select.find("li")
items.each(function(){
data[$(this).attr("name")] = $(this).text()
})
this.data = data
}
}

reBuildDom (data) {
var ul = this.btn__select.children("ul")
ul.empty()
var items = ""
$.each(data, function (k,v) {
items += `<li name="${k}">${v}</li>`
})
ul.append(items)
}

search (val) {
this.preText = val
var data = {}
if(val != ""){
$.each(this.data,(k, v) => {
(v.indexOf(val) > -1) && (data[k] = v)
})
this.reBuildDom(data)
}else{
this.reBuildDom(this.data)
}
}
}

/*["province","city"]*/
/**
* btnSelect关联器
* 在btnSelect初始化之后 才能 将关联器调用初始化
*
* 在实例化关联器是传入与关联按钮的name数组,按关联顺序排序
* 如下:province的下一级是city
*
* 关于初始化的板栗:
var btnSelectRelations = new btnSelectRelation(["province","city"])
btnSelectRelations.init()
*
* 设置数据:
window.btnSelectList.province.setSource({
zj: "浙江省",
hn: "湖南省",
sc: "四川省"
})

window.btnSelectList.city.setSource({
zj: {
hz:"杭州市",
sx:"绍兴市",
jh:"金华市"
},
hn: {
cs:"长沙市",
yy:"岳阳市",
cd:"常德市"
},
sc: {
cd:"成都市",
my:"绵阳市",
dy:"德阳市"
}
})
*
*/
class btnSelectRelation {
constructor (relationType) {
this.relationType = relationType
this.relationData = []
this.set = (childName, data) => {
var index = this.getChildIndex(childName)
this.relationData[index] = data
index == 0 && this.setChildData(index, data)
}
}
init () {
$.each(this.relationType,(k, v) => {
window.btnSelectList[v].setSource = data => {
var name = v
this.set(name,data)
}
window.btnSelectList[v].btnSelectRelation = (btnName,key) => {
var index = this.getChildIndex(btnName) + 1
index > 0 && index < this.relationData.length && this.filter(index, key)
}
})
}

getChildIndex (childName) {
return this.relationType.indexOf(childName)
}

filter (index, key) {
this.setChildData(index, this.relationData[index][key])
}

setChildData (index, data) {
window.btnSelectList[this.relationType[index]].set(data)
}
}

补上css,这里是用stylus语法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* 清除按钮样式 */
btn-clean()
border none
outline none
box-shadow none
background none

/* 下拉按钮样式 */
dropDown()
padding 0
border-left 6px solid transparent
border-right 6px solid transparent
border-top 10px solid mt-orange

.btn__select
btn-clean()
border 1px solid #ccc
border-radius 2px
font-size 14px
color gray-a7
padding 0.5em 0.7em
position relative
input,
button
btn-clean()
&:focus~ul
display block
button
dropDown()
ul
position absolute
top 100%
left 0
width 100%
border-radius 0 0 2px 2px
box-shadow 0 2px 2px rgba(0,0,0,.3)
display none
&:hover
display block
li
padding 0.5em 0.3em
border 1px solid #ccc
&:not(:last-child)
border-bottom none
&:last-child
border-radius 0 0 2px 2px

使用原生js以base64读取本地图片文件

起因

在做项目的时候遇到了一个问题,需要用户上传一张图片在画布里操作,一般的逻辑是先传图片到服务器,然后从服务器读取这张图片,这过程中可能读取的时间会过长,而且服务器也没必要存操作前的图片,于是就需要本地读取图片(这里用base64的格式)。
这里为了备忘,记录下。

代码

1
2
3
4
5
6
7
File.prototype.convertToBase64 = function(callback){
var FR= new FileReader();
FR.onload = function(e) {
callback(e.target.result)
};
FR.readAsDataURL(this);
}

使用方法

1
2
3
document.querySelector('[type="file"]').files[0].convertToBase64(function(base64){
console.log(base64);
});

zz的我和hexo多说配置

在_config.yml中添加多说的配置:

(⚠️注意:_config.yml在根目录和主题根目录都会存在,主题目录下的文件信息将会覆盖根目录的文件信息)

1
2
duoshuo: 你站点的short_name
duoshuo_shortname: 你站点的short_name

修改themes\landscape\layout\_partial\article.ejs模板

1
2
3
4
5
6
7
<% if (!index && post.comments && config.disqus_shortname){ %>
<section id="comments">
<div id="disqus_thread">
<noscript>Please enable JavaScript to view the <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
</div>
</section>
<% } %>

改为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<% if (!index && post.comments && config.duoshuo_shortname){ %>
<section id="comments">
<!-- 多说评论框 start -->
<div class="ds-thread" data-thread-key="<%= post.layout %>-<%= post.slug %>" data-title="<%= post.title %>" data-url="<%= page.permalink %>"></div>
<!-- 多说评论框 end -->
<!-- 多说公共JS代码 start (一个网页只需插入一次) -->
<script type="text/javascript">
var duoshuoQuery = {short_name:'<%= config.duoshuo_shortname %>'};
(function() {
var ds = document.createElement('script');
ds.type = 'text/javascript';
ds.async = true;
ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
ds.charset = 'UTF-8';
(document.getElementsByTagName('head')[0]
|| document.getElementsByTagName('body')[0]).appendChild(ds);
})();
</script>
<!-- 多说公共JS代码 end -->
</section>
<% } %>

同时使用多个deployer

单个deployer的时候的 _config.yml

1
2
3
4
deploy:
type: git
repo: git@github.com:leenty/leenty.github.io.git
branch: master

而换成多个deployer的时候需要在前面加-

1
2
3
4
5
6
7
deploy:
- type: git
repo: git@github.com:demo/demo.github.io.git
branch: master
- type: git
repo: git@git.coding.net:demo/demo.git
branch: master

推荐一个不错的sublime UI界面美化方案

晒一晒我的prefernces.sublime-setting

界面如图, 主题名字叫做 Material Theme
prefernces-sublime-setting

主题地址

equinusocio/material-theme

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
{
"always_show_minimap_viewport": true,
"color_scheme": "Packages/Material Theme/schemes/Material-Theme.tmTheme",
"file_exclude_patterns":
[
".DS_Store"
],
"folder_exclude_patterns":
[
".git",
"node_modules",
"public/static"
],
"font_size": 12,
"ignored_packages":
[
"Vintage"
],
"line_padding_bottom": 3,
"line_padding_top": 3,
"material_theme_accent_sky": true,
"material_theme_compact_sidebar": true,
"material_theme_contrast_mode": true,
"material_theme_disable_tree_indicator": true,
"material_theme_panel_separator": true,
"material_theme_small_statusbar": true,
"material_theme_small_tab": true,
"material_theme_tabs_autowidth": true,
"material_theme_tabs_separator": true,
"material_theme_tree_headings": true,
"overlay_scroll_bars": "enabled",
"tab_size": 2,
"theme": "Material-Theme.sublime-theme",
"translate_tabs_to_spaces": true,
"word_warp": "auto"
}