mirror of
https://github.com/skishore/makemeahanzi.git
synced 2025-10-30 18:37:14 +08:00
Clean up Glyphs model methods
This commit is contained in:
@ -2,27 +2,33 @@
|
||||
|
||||
this.Glyphs = new Mongo.Collection('glyphs');
|
||||
|
||||
Glyphs.findGlyphsForRadical = function(radical) {
|
||||
if (radical) {
|
||||
return Glyphs.find({'index.radical': radical});
|
||||
}
|
||||
return Glyphs.find({'index.strokes': 0});
|
||||
Glyphs.get = (query) => Glyphs.findOne(query);
|
||||
|
||||
Glyphs.getNext = (glyph) => {
|
||||
const codepoint = glyph ? glyph.codepoint : undefined;
|
||||
const next = Glyphs.findOne(
|
||||
{codepoint: {$gt: codepoint}}, {sort: {codepoint: 1}});
|
||||
return next ? next : Glyphs.findOne({}, {sort: {codepoint: 1}});
|
||||
}
|
||||
|
||||
Glyphs.get_svg_path = function(glyph) {
|
||||
var terms = [];
|
||||
for (var i = 0; i < glyph.path.length; i++) {
|
||||
var segment = glyph.path[i];
|
||||
assert('LMQZ'.indexOf(segment.type) >= 0, segment.type);
|
||||
terms.push(segment.type);
|
||||
if (segment.x1 !== undefined) {
|
||||
terms.push(segment.x1);
|
||||
terms.push(segment.y1);
|
||||
}
|
||||
if (segment.x !== undefined) {
|
||||
terms.push(segment.x);
|
||||
terms.push(segment.y);
|
||||
}
|
||||
}
|
||||
return terms.join(' ');
|
||||
Glyphs.getPrevious = (glyph) => {
|
||||
const codepoint = glyph ? glyph.codepoint : undefined;
|
||||
const previous = Glyphs.findOne(
|
||||
{codepoint: {$lt: codepoint}}, {sort: {codepoint: -1}});
|
||||
return previous ? previous : Glyphs.findOne({}, {sort: {codepoint: -1}});
|
||||
}
|
||||
|
||||
Glyphs.save = (glyph) => {
|
||||
check(glyph.character, String);
|
||||
assert(glyph.character.length === 1);
|
||||
Glyphs.upsert({character: glyph.character}, glyph);
|
||||
}
|
||||
|
||||
// Register the methods above on the server so they are available to the client.
|
||||
if (Meteor.isServer) {
|
||||
const methods = {};
|
||||
const method_names = ['get', 'getNext', 'getPrevious', 'save'];
|
||||
method_names.map((name) => methods[`${name}Glyph`] = Glyphs[name]);
|
||||
methods.saveGlyphs = (glyphs) => glyphs.map(Glyphs.save);
|
||||
Meteor.methods(methods);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user