Files
vanillajs-seed/js/index.js
2019-04-14 22:14:12 +02:00

101 lines
2.2 KiB
JavaScript

(function() {
// globals
MyApp.Utils = {};
// utility functions
MyApp.Utils.getUrlParameter = function(name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
var results = regex.exec(location.search);
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
};
// app functions
MyApp.renderShell = function() {
document.title = i18next.t('vanillaJS');
var d = $.Deferred();
//preload this template, so we can be sure the dom in rendered when we resolve
ejs.preloadTemplate('/templates/main.ejs')
.then(function(templateUrl) {
$('.js-main-content').html(ejs.rr(templateUrl));
d.resolve();
});
return d;
};
MyApp.renderHomePage = function() {
$('.js-page-container').html(ejs.rr('/templates/home.ejs'));
};
MyApp.renderTextPage = function() {
$('.js-page-container').html(ejs.rr('/templates/sometext.ejs', {
texts: [
{
id: 100,
description: 'Some text with id 100'
},
{
id: 200,
description: 'Some other text with id 200. Click it!'
}
]
}));
};
MyApp.renderAboutPage = function() {
$('.js-page-container').html(ejs.rr('/templates/about.ejs'));
};
// events
$(document).on('click', '.js-link', function(ev) {
var el = $(ev.currentTarget);
var linkId = el.attr('data-id');
PNotify.success('The text you clicked had id ' + linkId + '. Maybe next time I will do something with this id.');
});
// app startup
// set language
i18next.init({
lng: MyApp.config.lang,
resources: {
en: {
translation: {
'vanillaJS': 'vanillaJS seed project',
'awesome': 'This is an awesome app!'
}
},
it: {
translation: {
'vanillaJS': 'progetto di esempio vanillaJS',
'awesome': 'Questa app è fantastica!'
}
},
de: {
translation: {
'vanillaJS': 'vanillaJS Beispiel Projekt',
'awesome': 'Coole app.'
}
}
}
});
// render main shell
MyApp.renderShell()
.then(function() {
//setup routing
page('/', MyApp.renderHomePage);
page('/about', MyApp.renderAboutPage);
page('/text', MyApp.renderTextPage);
page({
hashbang: true
});
});
})();