Skip to content

Commit 26f250f

Browse files
author
Joel Day
committed
load common, also fix module exports
1 parent 1b9b619 commit 26f250f

File tree

1 file changed

+63
-23
lines changed

1 file changed

+63
-23
lines changed

charMap.js

Lines changed: 63 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,7 @@ var charMap = {
1616
this.opt.commonality = 2;
1717
}
1818

19-
// Load in commonly used symbols in a common category
20-
var charMapCommon = localStorage.getItem('charMapCommon');
21-
if ( charMapCommon ) {
22-
this.commonCategories = JSON.parse(charMapCommon);
23-
24-
var sortable = [];
25-
for (var char in this.commonCategories) {
26-
if ( this.commonCategories[char] >= this.opt.commonality ) {
27-
sortable.push({char: char, count: this.commonCategories[char]})
28-
sortable.sort(
29-
function(a, b) {
30-
return a.count - b.count
31-
}
32-
).reverse();
33-
}
34-
}
35-
36-
this.categories.Common = sortable;
37-
}
38-
19+
this.loadCommon();
3920

4021
// Create the dialog element
4122
var container = document.createElement('div');
@@ -89,7 +70,66 @@ var charMap = {
8970
}
9071

9172
},
73+
loadCommon: function(){
74+
// Load in commonly used symbols in a common category
75+
var charMapCommon = localStorage.getItem('charMapCommon');
76+
if ( charMapCommon ) {
77+
this.commonCategories = JSON.parse(charMapCommon);
9278

79+
var sortable = [];
80+
for (var char in this.commonCategories) {
81+
if ( this.commonCategories[char] >= this.opt.commonality ) {
82+
sortable.push({char: char, count: this.commonCategories[char]})
83+
sortable.sort(
84+
function(a, b) {
85+
return a.count - b.count
86+
}
87+
).reverse();
88+
}
89+
}
90+
91+
this.categories.Common = sortable;
92+
}
93+
},
94+
95+
updateCommon: function(){
96+
var outputCommon = [];
97+
var common = document.querySelector('.charMapCategory[data-category=Common]');
98+
this.loadCommon();
99+
if ( common ) {
100+
common.innerHTML = '';
101+
for ( var c = 0; c < this.categories.Common.length; ++c ) {
102+
cat = this.categories.Common[c];
103+
104+
105+
if ( outputCommon.indexOf(cat.char) > -1 ) {
106+
continue;
107+
}
108+
outputCommon.push(cat.char);
109+
110+
111+
li = document.createElement('li');
112+
li.innerHTML = cat.char;
113+
if ( cat.entity ) {
114+
li.innerHTML = cat.entity;
115+
}
116+
117+
li.setAttribute('data-char', li.innerHTML);
118+
119+
if ( cat.name ) {
120+
li.setAttribute('data-name', cat.name);
121+
}
122+
123+
if ( cat.entity ) {
124+
li.setAttribute('data-entity', cat.entity);
125+
}
126+
127+
li.addEventListener('click', this.onClick.bind(this));
128+
129+
common.appendChild(li);
130+
}
131+
}
132+
},
93133
onClick: function(event){
94134
var char = event.target.getAttribute('data-char');
95135
var charMapCommon = JSON.parse((localStorage.getItem('charMapCommon') || '{}'));
@@ -103,7 +143,7 @@ var charMap = {
103143
}
104144

105145
localStorage.setItem('charMapCommon', JSON.stringify(charMapCommon));
106-
146+
this.updateCommon();
107147
if ( typeof this.opt.onClick == 'function' ) {
108148
this.opt.onClick(char);
109149
}
@@ -2531,6 +2571,6 @@ var charMap = {
25312571
}
25322572
};
25332573

2534-
if ( typeof modules != 'undefined' ) {
2535-
modules.exports = charMap;
2574+
if ( typeof module != 'undefined' ) {
2575+
module.exports = charMap;
25362576
}

0 commit comments

Comments
 (0)