deardesi.js/views/post/post.js

211 lines
6.8 KiB
JavaScript
Raw Normal View History

2015-01-13 10:18:13 +00:00
'use strict';
angular.module('myApp.post', ['ngRoute'])
.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/post', {
templateUrl: 'views/post/post.html',
controller: 'PostCtrl as Post'
});
}])
.controller('PostCtrl'
2015-01-16 04:56:14 +00:00
, ['$scope', '$location', '$timeout', 'Desirae'
2015-01-16 00:17:23 +00:00
, function ($scope, $location, $timeout, DesiraeService) {
2015-01-13 10:18:13 +00:00
var scope = this
2015-01-24 03:09:50 +00:00
, path = window.path
2015-01-13 10:18:13 +00:00
;
function init() {
2015-01-16 00:17:23 +00:00
DesiraeService.meta().then(function (desi) {
2015-01-21 20:38:07 +00:00
/*
if (!scope.site.base_url) {
window.alert("Please go to the site tab and add a url first");
return;
}
*/
2015-01-13 10:18:13 +00:00
scope.blogdir = desi.blogdir.path.replace(/^\/(Users|home)\/[^\/]+\//, '~/');
scope.site = desi.site;
2015-01-16 04:56:14 +00:00
scope.env = desi.site;
2015-01-13 10:18:13 +00:00
newPost();
2015-01-21 20:38:07 +00:00
if (/dropbox/.test(scope.site.base_url)) {
scope.env.explicitIndexes = true;
}
2015-01-13 10:18:13 +00:00
updateDate();
}).catch(function (e) {
window.alert("An Error Occured. Most errors that occur in the init phase are parse errors in the config files or permissions errors on files or directories, but check the error console for details.");
console.error(e);
throw e;
});
scope.extensions = ['md', 'html'];
}
function newPost() {
scope.selected = {
format: 'md'
2015-01-13 11:55:16 +00:00
, permalink: "/article/new.html"
2015-01-13 10:18:13 +00:00
, uuid: window.uuid.v4()
, abspath: scope.blogdir
2015-01-16 04:56:14 +00:00
, sourcepath: ''
2015-01-24 03:09:50 +00:00
, fileepath: ''
2015-01-13 10:18:13 +00:00
, post: {
yml: {
title: ""
2015-01-13 11:55:16 +00:00
, permalink: "/article/new.html"
2015-01-16 00:17:23 +00:00
, date: DesiraeService.toDesiDate(new Date())// "YYYY-MM-DD HH:MM pm" // TODO desirae
2015-01-13 10:18:13 +00:00
, updated: null
, description: ""
, categories: []
, tags: []
, theme: null
, layout: null
, swatch: null
}
}
};
scope.selected.date = scope.selected.post.yml.date;
scope.selected.post.frontmatter = window.jsyaml.dump(scope.selected.post.yml).trim();
}
scope.onChange = function () {
var post = scope.selected.post
, selected = scope.selected
;
post.yml.title = post.yml.title || '';
selected.title = post.yml.title;
2015-01-13 10:18:13 +00:00
post.yml.description = post.yml.description || '';
2015-01-16 04:56:14 +00:00
scope.slug = post.yml.title.toLowerCase()
.replace(/["']/g, '')
.replace(/\W/g, '-')
.replace(/^-+/g, '')
.replace(/-+$/g, '')
.replace(/--/g, '-')
;
2015-01-13 10:18:13 +00:00
if (selected.permalink === post.yml.permalink) {
2015-01-16 04:56:14 +00:00
selected.permalink = '/articles/' + scope.slug + '/';
// + '.html' //+ selected.format
2015-01-13 10:18:13 +00:00
post.yml.permalink = selected.permalink;
}
2015-01-13 11:55:16 +00:00
/*
2015-01-24 03:09:50 +00:00
if (path.extname(post.yml.permalink) !== '.' + selected.format) {
2015-01-13 10:18:13 +00:00
post.yml.permalink = post.yml.permalink.replace(/\.\w+$/, '.' + selected.format);
}
2015-01-13 11:55:16 +00:00
*/
2015-01-13 10:18:13 +00:00
post.frontmatter = window.jsyaml.dump(post.yml).trim();
// TODO use some sort of filepath pattern in config.yml
2015-01-24 03:09:50 +00:00
selected.path = path.join((scope.env.compiled_path || 'compiled'), post.yml.permalink);
2015-01-13 11:55:16 +00:00
if (!/\.html?$/.test(selected.path)) {
2015-01-24 03:09:50 +00:00
selected.path = path.join(selected.path, 'index.html');
2015-01-13 11:55:16 +00:00
}
2015-01-13 21:50:17 +00:00
2015-01-24 03:09:50 +00:00
selected.url = path.join(scope.site.base_url + path.join(scope.site.base_path, post.yml.permalink));
2015-01-21 20:38:07 +00:00
if (scope.env.explicitIndexes && /\/$/.test(selected.url)) {
selected.url += 'index.html';
}
selected.markdown = '[' + selected.title + '](' + selected.url + ')';
selected.ahref = '<a href="' + selected.url + '">' + selected.title + '</a>';
2015-01-24 03:09:50 +00:00
selected.abspath = path.join(scope.blogdir, selected.path);
selected.filepath = path.join((selected.collection || 'posts'), scope.slug + '.' + selected.format);
selected.sourcepath = path.join(scope.blogdir, (selected.collection || 'posts'), scope.slug + '.' + selected.format);
2015-01-13 10:18:13 +00:00
};
scope.onFrontmatterChange = function () {
var data
2015-01-13 11:55:16 +00:00
, post
2015-01-13 10:18:13 +00:00
;
try {
if (!scope.selected.post.frontmatter || !scope.selected.post.frontmatter.trim()) {
throw new Error('deleted frontmatter');
}
data = window.jsyaml.load(scope.selected.post.frontmatter);
2015-01-13 11:55:16 +00:00
//scope.selected.format = data.permalink.replace(/.*\.(\w+$)/, '$1');
2015-01-13 10:18:13 +00:00
if (!data.permalink) {
data = scope.selected.permalink;
}
scope.selected.post.yml = data;
2015-01-13 11:55:16 +00:00
post = scope.selected.post;
2015-01-13 21:50:17 +00:00
2015-01-24 03:09:50 +00:00
scope.selected.path = path.join((scope.env.compiled_path || 'compiled'), post.yml.permalink);
if (!/\.html?$/.test(path.basename(post.yml.permalink))) {
scope.selected.path = path.join(scope.selected.path.replace(/\.w+$/, ''), 'index.html');
2015-01-13 11:55:16 +00:00
}
2015-01-21 20:38:07 +00:00
2015-01-24 03:09:50 +00:00
scope.selected.url = path.join(scope.site.base_url + path.join(scope.site.base_path, post.yml.permalink));
2015-01-21 20:38:07 +00:00
if (scope.env.explicitIndexes && /\/$/.test(scope.selected.url)) {
scope.selected.url += 'index.html';
}
2015-01-24 03:09:50 +00:00
scope.selected.abspath = path.join(scope.blogdir, scope.selected.path);
scope.selected.sourcepath = path.join(scope.blogdir, (scope.selected.collection || 'posts'), scope.slug + '.' + scope.selected.format);
scope.selected.filepath = path.join((scope.selected.collection || 'posts'), scope.slug + '.' + scope.selected.format);
2015-01-13 10:18:13 +00:00
} catch(e) {
console.error(e);
console.error('ignoring update that created parse error');
scope.selected.post.frontmatter = window.jsyaml.dump(scope.selected.post.yml).trim();
}
};
function updateDate() {
$timeout.cancel(scope.dtlock);
scope.dtlock = $timeout(function () {
if (scope.selected && scope.selected.date === scope.selected.post.yml.date) {
2015-01-16 00:17:23 +00:00
scope.selected.date = scope.selected.post.yml.date = DesiraeService.toDesiDate(new Date());
2015-01-13 10:18:13 +00:00
}
scope.onChange();
updateDate();
}, 60 * 1000);
}
scope.upsert = function () {
if (-1 === scope.extensions.indexOf(scope.selected.format)) {
window.alert('.' + scope.selected.format + ' is not a supported extension.\n\nPlease choose from: .' + scope.extensions.join(' .'));
return;
}
scope.selected.post.yml.uuid = scope.selected.uuid;
['updated', 'theme', 'layout', 'swatch'].forEach(function (key) {
if (!scope.selected.post.yml[key]) {
delete scope.selected.post.yml[key];
}
});
scope.onChange();
var files = []
;
files.push({
2015-01-24 03:09:50 +00:00
path: scope.selected.filepath
2015-01-13 10:18:13 +00:00
, contents:
'---\n'
+ scope.selected.post.frontmatter.trim()
+ '\n'
+ '---\n'
+ '\n'
+ scope.selected.post.body.trim()
});
2015-01-16 00:17:23 +00:00
DesiraeService.putFiles(files).then(function (results) {
2015-01-13 10:18:13 +00:00
console.log('TODO check for error');
2015-01-13 11:55:16 +00:00
console.log(files);
2015-01-13 10:18:13 +00:00
console.log(results);
$location.path('/build');
}).catch(function (e) {
$timeout.cancel(scope.dtlock);
console.error(scope.site);
console.error(e);
window.alert("Error Nation! :/");
throw e;
});
};
init();
}]);