Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(renderer): refactor code #18

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules
*.app
*.db
build
dist
109 changes: 60 additions & 49 deletions app/renderer.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@

// JQuery
var $ = require('jquery');
var path = require('path');
var Emojis = require('./emoji.js');
var clipboard = require('electron').clipboard;

var $emojiTabs = $('#emoji-tab>li');
var $emojiLists = $('#emoji-view>div');
var $emojiSearchDelete = $('#emoji-search-delete');
var $emojiSearchInput = $('#emoji-search>input');


// Nedb
var Datastore = require('nedb');
var db = new Datastore({
Expand All @@ -16,16 +21,17 @@ var db = new Datastore({
var TEXT_RE = /:(.*):/;
var NAME_RE = /.*\/(.*)\.png$/;


// Render Emoji
var peopleHtml = '',
natureHtml = '',
objectsHtml = '',
placesHtml = '',
symbolsHtml = '';

function renderGroup(group){
function renderGroup(group) {
var groupHtml = '';
group.forEach(function(element){
group.forEach(function (element) {
groupHtml += '<img class="emoji-cell" data-clipboard-action="copy" ' +
'src="../resources/images/graphics/emojis/' + element.name +
'.png" alt=":' + element.text +
Expand All @@ -40,7 +46,7 @@ objectsHtml = renderGroup(Emojis.objects);
placesHtml = renderGroup(Emojis.places);
symbolsHtml = renderGroup(Emojis.symbols);

function initCommonTab(){
function initCommonTab() {
db.find({}).sort({count: -1}).limit(30).exec(function(err, docs){
var commonHtml = '';
commonHtml = renderGroup(docs);
Expand All @@ -55,113 +61,118 @@ $('#places').html(placesHtml);
$('#symbols').html(symbolsHtml);
initCommonTab();

$('#common-tab').on('click', function(){
var inputText = $('#emoji-search>input').val();
if(!inputText){
$('#common-tab').on('click', function () {
var inputText = $emojiSearchInput.val();
if (!inputText) {
initCommonTab();
}
});

function emojiMetas(el){
var name = $(el).attr('src').match(NAME_RE)[1];
var alternative = $(el).data('alternative-name');
var text = $(el).attr('alt').match(TEXT_RE)[1];

var obj = { name: name, alternative_name: alternative, text: text, count: 0 }; // jshint ignore:line
return obj;
}

// Copy
function statusTips(text){
function statusTips(text) {
var CopyStatus = $('#emoji-copy-status');
CopyStatus.html(text);
CopyStatus.stop().fadeIn(400).delay(2000).fadeOut(400);
}

$(document).on('click', '.emoji-cell', function(){
function emojiMetas(el) {
var name = $(el).attr('src').match(NAME_RE)[1];
var alternative = $(el).data('alternative-name');
var text = $(el).attr('alt').match(TEXT_RE)[1];

var obj = {
name: name,
alternative_name: alternative,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Identifier 'alternative_name' is not in camel case.

text: text,
count: 0
};
return obj;
}

$(document).on('click', '.emoji-cell', function () {
var that = this;
var emojiText = $(that).attr('alt');
var text = emojiText.match(TEXT_RE)[1];
clipboard.writeText(emojiText);
statusTips('Copied ' + emojiText);
db.find({ text: text}, function(err, docs){
if(docs.length === 0){
db.insert(emojiMetas(that), function(err){
db.find({ text: text}, function (err, docs) {
if (docs.length === 0) {
db.insert(emojiMetas(that), function (err) {
if (err !== null) {
console.log(err);
}
});
}else{
db.update({ text: text }, {$inc: {count: 1}}, function(err){
if(err !== null){
} else {
db.update({ text: text }, {$inc: {count: 1}}, function (err) {
if (err !== null) {
console.log('error');
}
});
}
});
});

// Tab
var emojiTabs = $('#emoji-tab>li');
var emojiLists = $('#emoji-view>div');

$('#emoji-tab li').on('click', function(){
emojiTabs.removeClass('tab-selected');
emojiLists.hide();
// Tab
$('#emoji-tab li').on('click', function () {
$emojiTabs.removeClass('tab-selected');
$emojiLists.hide();
$(this).addClass('tab-selected');
for(var i = 0; i < emojiLists.length; i++){
if($(this).data('title').toLowerCase() === emojiLists[i].id){
for (var i = 0; i < $emojiLists.length; i++) {
if ($(this).data('title').toLowerCase() === $emojiLists[i].id) {
$('#' + $(this).data('title').toLowerCase()).show();
}
}
});


// Search
function isElementMatching(element, needle){
function isElementMatching(element, needle) {
var alternative = element.attr('data-alternative-name');
var name = element.attr('alt');
return (name.toLowerCase().indexOf(needle) >= 0) ||
(alternative !== null && alternative.toLowerCase().indexOf(needle) >= 0);
}

function highlightAll(){
function highlightAll() {
$('.emoji-cell').show();
}

function highlightElements(needle){
if(needle.length === 0){
function highlightElements(needle) {
if (needle.length === 0) {
highlightAll();
$('#emoji-search-delete').hide();
$emojiSearchDelete.hide();
return;
}

needle = needle.toLowerCase();
$('#emoji-view img').each(function(index, el){
if(isElementMatching($(el), needle)){
$('#emoji-view img').each(function (index, el) {
if (isElementMatching($(el), needle)) {
$(el).show();
}else{
} else {
$(el).hide();
}
});
}

$('#emoji-search>input').keyup(function(e){
if(e.keyCode === 27){
$emojiSearchInput.keyup(function (e) {
if (e.keyCode === 27) {
$(this).val('').blur();
highlightAll();
$('#emoji-search-delete').hide();
$emojiSearchDelete.hide();
}
});

$('#emoji-search>input').on('change paste keyup', function(){
$('#emoji-search-delete').show();
highlightElements($('#emoji-search>input').val());
$emojiSearchInput.on('change paste keyup', function () {
$emojiSearchDelete.show();
highlightElements($emojiSearchInput.val());
});

$('#emoji-search>input').focus();
$emojiSearchInput.focus();

$('#emoji-search-delete').on('click', function(){
$('#emoji-search>input').val('');
$('#emoji-search-delete').hide();
$emojiSearchDelete.on('click', function () {
$emojiSearchInput.val('');
$emojiSearchDelete.hide();
highlightAll();
});
2 changes: 0 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,11 @@ var template = [

// todo: need remove
mb.on('ready', function ready(){
console.log(__dirname + '/resources/images/icon.png');
var menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
});

mb.on('after-create-window', function (){
mb.window.openDevTools();
mb.window.loadURL('file://' + __dirname + '/app/index.html');
});