Skip to content
Snippets Groups Projects
Commit ade9ad43 authored by Eike Cochu's avatar Eike Cochu
Browse files

applied jshint and js prettify

parent fd5bc11d
Branches
No related tags found
No related merge requests found
......@@ -2,8 +2,11 @@
* Vipra Application
* Main application file
******************************************************************************/
/* globals angular, console, NProgress */
(function() {
"use strict";
var app = angular.module('vipra.app', [
'ngResource',
'ngSanitize',
......@@ -23,7 +26,7 @@
$urlRouterProvider.otherwise('/');
// states
$stateProvider.state('index', {
url: '/',
templateUrl: 'html/index.html',
......@@ -122,11 +125,11 @@
}]);
app.config(['$httpProvider', function($httpProvider) {
var loadingTimeout;
var loadingTimeout;
app.config(['$httpProvider', function($httpProvider) {
$httpProvider.interceptors.push(function ($q, $injector, $rootScope) {
$httpProvider.interceptors.push(function($q, $injector, $rootScope) {
var requestIncrement = function(config) {
$rootScope.loading.requests = ++$rootScope.loading.requests || 1;
$rootScope.loading[config.method] = ++$rootScope.loading[config.method] || 1;
......@@ -175,7 +178,7 @@
$rootScope.loading = {};
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
$rootScope.$on('$stateChangeStart', function(event, toState) {
console.log('changing state: ' + toState.name);
NProgress.start();
......@@ -187,4 +190,4 @@
}]);
})();
\ No newline at end of file
})();
/* globals Vipra */
(function() {
"use strict";
window.Vipra = window.Vipra || {};
Vipra.config = {
......@@ -10,4 +13,4 @@
pageSize: 100
};
})();
\ No newline at end of file
})();
......@@ -2,8 +2,11 @@
* Vipra Application
* Controllers
******************************************************************************/
/* globals angular, Vipra, moment, vis, console, $, prompt */
(function() {
"use strict";
var app = angular.module('vipra.controllers', [
'ui.router',
'vipra.factories'
......@@ -19,35 +22,36 @@
app.controller('IndexController', ['$scope', '$location', 'ArticleFactory', 'TopicFactory', 'WordFactory', 'SearchFactory',
function($scope, $location, ArticleFactory, TopicFactory, WordFactory, SearchFactory) {
$scope.search = $location.search().query;
$scope.search = $location.search().query;
ArticleFactory.query({limit:Vipra.config.latestItems, sort:'-created'}, function(data) {
$scope.latestArticles = data;
});
ArticleFactory.query({ limit: Vipra.config.latestItems, sort: '-created' }, function(data) {
$scope.latestArticles = data;
});
TopicFactory.query({limit:Vipra.config.latestItems, sort:'-created'}, function(data) {
$scope.latestTopics = data;
});
TopicFactory.query({ limit: Vipra.config.latestItems, sort: '-created' }, function(data) {
$scope.latestTopics = data;
});
WordFactory.query({limit:Vipra.config.latestItems, sort:'-created'}, function(data) {
$scope.latestWords = data;
});
WordFactory.query({ limit: Vipra.config.latestItems, sort: '-created' }, function(data) {
$scope.latestWords = data;
});
$scope.$watch('search', function() {
if($scope.search) {
$location.search('query', $scope.search);
$scope.searching = true;
SearchFactory.query({limit:Vipra.config.searchResults, query:$scope.search}, function(data) {
$scope.searching = false;
$scope.searchResults = data;
});
} else {
$location.search('query', null);
$scope.searchResults = [];
}
});
$scope.$watch('search', function() {
if ($scope.search) {
$location.search('query', $scope.search);
$scope.searching = true;
SearchFactory.query({ limit: Vipra.config.searchResults, query: $scope.search }, function(data) {
$scope.searching = false;
$scope.searchResults = data;
});
} else {
$location.search('query', null);
$scope.searchResults = [];
}
});
}]);
}
]);
/**
* About controller
......@@ -55,14 +59,15 @@
app.controller('AboutController', ['$scope', 'InfoFactory',
function($scope, InfoFactory) {
InfoFactory.get(function(data) {
$scope.info = data;
$scope.buildDate = Vipra.formatDateTime(moment($scope.info.app.builddate, 'YYMMDD_HHmm').toDate());
$scope.startTime = Vipra.formatDateTime(moment($scope.info.vm.starttime, 'x').toDate());
$scope.upTime = moment.duration($scope.info.vm.uptime).humanize();
});
InfoFactory.get(function(data) {
$scope.info = data;
$scope.buildDate = Vipra.formatDateTime(moment($scope.info.app.builddate, 'YYMMDD_HHmm').toDate());
$scope.startTime = Vipra.formatDateTime(moment($scope.info.vm.starttime, 'x').toDate());
$scope.upTime = moment.duration($scope.info.vm.uptime).humanize();
});
}]);
}
]);
/**
* Network controller
......@@ -70,190 +75,191 @@
app.controller('NetworkController', ['$scope', '$state', '$stateParams', '$timeout', 'Store', 'ArticleFactory', 'TopicFactory', 'WordFactory',
function($scope, $state, $stateParams, $timeout, Store, ArticleFactory, TopicFactory, WordFactory) {
var id = 0,
var id = 0,
ids = {},
edges = {};
$scope.colors = {
articles: '#BBC9D2',
topics: '#DBB234',
words: '#547C65'
};
$scope.nodes = new vis.DataSet();
$scope.edges = new vis.DataSet();
$scope.data = {
nodes: $scope.nodes,
edges: $scope.edges
};
$scope.type = $stateParams.type;
$scope.options = {
nodes: {
font: { size: 14 },
shape: 'dot',
borderWidth: 0
},
edges: {
color: {
highlight: '#f00'
}
},
layout: { randomSeed: 1 },
physics: {
barnesHut: {
springConstant: 0.008,
gravitationalConstant: -3500
$scope.colors = {
articles: '#BBC9D2',
topics: '#DBB234',
words: '#547C65'
};
$scope.nodes = new vis.DataSet();
$scope.edges = new vis.DataSet();
$scope.data = {
nodes: $scope.nodes,
edges: $scope.edges
};
$scope.type = $stateParams.type;
$scope.options = {
nodes: {
font: { size: 14 },
shape: 'dot',
borderWidth: 0
},
edges: {
color: {
highlight: '#f00'
}
},
layout: { randomSeed: 1 },
physics: {
barnesHut: {
springConstant: 0.008,
gravitationalConstant: -3500
}
}
};
var factory;
if ($stateParams.type === 'articles')
factory = ArticleFactory;
else if ($stateParams.type === 'topics')
factory = TopicFactory;
else if ($stateParams.type === 'words')
factory = WordFactory;
else {
console.log('unknown network type');
return;
}
};
var factory;
if($stateParams.type === 'articles')
factory = ArticleFactory;
else if($stateParams.type === 'topics')
factory = TopicFactory;
else if($stateParams.type === 'words')
factory = WordFactory;
else {
console.log('unknown network type');
return;
}
// get root node
factory.get({id: $stateParams.id}, function(data) {
// add root node
if($stateParams.type === 'articles')
$scope.nodes.add([articleNode(data)]);
else if($stateParams.type === 'topics')
$scope.nodes.add([topicNode(data)]);
else if($stateParams.type === 'words')
$scope.nodes.add([wordNode(data)]);
ids[data.id] = id;
// create graph
var container = document.getElementById("visgraph");
$scope.graph = new vis.Network(container, $scope.data, $scope.options);
$scope.graph.on('selectNode', $scope.select);
$scope.graph.on('doubleClick', $scope.open);
});
var newNode = function(title, type, show, dbid, color, shape) {
ids[dbid] = ++id;
return {
id: id,
title: title,
label: title.multiline(5),
type: type,
show: show,
dbid: dbid,
shape: shape || 'dot',
color: {
background: color,
highlight: { background: color }
// get root node
factory.get({ id: $stateParams.id }, function(data) {
// add root node
if ($stateParams.type === 'articles')
$scope.nodes.add([articleNode(data)]);
else if ($stateParams.type === 'topics')
$scope.nodes.add([topicNode(data)]);
else if ($stateParams.type === 'words')
$scope.nodes.add([wordNode(data)]);
ids[data.id] = id;
// create graph
var container = document.getElementById("visgraph");
$scope.graph = new vis.Network(container, $scope.data, $scope.options);
$scope.graph.on('selectNode', $scope.select);
$scope.graph.on('doubleClick', $scope.open);
});
var newNode = function(title, type, show, dbid, color, shape) {
ids[dbid] = ++id;
return {
id: id,
title: title,
label: title.multiline(5),
type: type,
show: show,
dbid: dbid,
shape: shape || 'dot',
color: {
background: color,
highlight: { background: color }
}
};
};
var topicNode = function(topic) {
topic = topic.topic || topic;
return newNode(topic.name, 'topic', 'topics.show', topic.id, $scope.colors.topics, 'triangle');
};
var articleNode = function(article) {
return newNode(article.title, 'article', 'articles.show', article.id, $scope.colors.articles, 'square');
};
var wordNode = function(word) {
return newNode(word.id, 'word', 'words.show', word.id, $scope.colors.words);
};
var edgeExists = function(idA, idB) {
if (idB < idA) {
var tmp = idA;
idA = idB;
idB = tmp;
}
return edges.hasOwnProperty(idA + '-' + idB);
};
};
var topicNode = function(topic) {
topic = topic.topic || topic;
return newNode(topic.name, 'topic', 'topics.show', topic.id, $scope.colors.topics, 'triangle');
};
var articleNode = function(article) {
return newNode(article.title, 'article', 'articles.show', article.id, $scope.colors.articles, 'square');
};
var wordNode = function(word) {
return newNode(word.id, 'word', 'words.show', word.id, $scope.colors.words);
};
var edgeExists = function(idA, idB) {
if(idB < idA) {
var tmp = idA;
idA = idB;
idB = tmp;
}
return edges.hasOwnProperty(idA + '-' + idB);
};
var addEdge = function(idA, idB) {
if(idB < idA) {
var tmp = idA;
idA = idB;
idB = tmp;
}
edges[idA + '-' + idB] = 1;
};
// construct new nodes
var constructor = function(result, node, nodeFunction) {
if(result) {
var newNodes = [],
var addEdge = function(idA, idB) {
if (idB < idA) {
var tmp = idA;
idA = idB;
idB = tmp;
}
edges[idA + '-' + idB] = 1;
};
// construct new nodes
var constructor = function(result, node, nodeFunction) {
if (result) {
var newNodes = [],
newEdges = [];
for(var i = 0; i < result.length; i++) {
var current = result[i];
if(ids.hasOwnProperty(current.id)) {
if(edgeExists(ids[current.id], node.id))
continue;
newEdges.push({from:ids[current.id], to:node.id});
addEdge(ids[current.id], node.id);
} else {
newNodes.push(nodeFunction(current));
newEdges.push({from:id, to:node.id});
addEdge(id, node.id);
for (var i = 0; i < result.length; i++) {
var current = result[i];
if (ids.hasOwnProperty(current.id)) {
if (edgeExists(ids[current.id], node.id))
continue;
newEdges.push({ from: ids[current.id], to: node.id });
addEdge(ids[current.id], node.id);
} else {
newNodes.push(nodeFunction(current));
newEdges.push({ from: id, to: node.id });
addEdge(id, node.id);
}
}
if (newNodes.length)
$scope.nodes.add(newNodes);
if (newEdges.length)
$scope.edges.add(newEdges);
}
if(newNodes.length)
$scope.nodes.add(newNodes);
if(newEdges.length)
$scope.edges.add(newEdges);
}
};
// on node select
var selectTimeout;
$scope.select = function(props) {
$timeout.cancel(selectTimeout);
selectTimeout = $timeout(function() {
var node = $scope.nodes.get(props.nodes[0]);
if(node) {
if(node.type === 'article' && $scope.shown.topics) {
// node is article, load article to get topics
ArticleFactory.get({id:node.dbid}, function(data) {
if(data.topics) {
for(var i = 0; i < data.topics.length; i++)
data.topics[i] = data.topics[i].topic;
constructor(data.topics, node, topicNode);
}
});
} else if(node.type === 'topic') {
// node is topic, load topic to get words and articles
if($scope.shown.words)
TopicFactory.get({id:node.dbid}, function(data) {
constructor(data.words, node, wordNode);
};
// on node select
var selectTimeout;
$scope.select = function(props) {
$timeout.cancel(selectTimeout);
selectTimeout = $timeout(function() {
var node = $scope.nodes.get(props.nodes[0]);
if (node) {
if (node.type === 'article' && $scope.shown.topics) {
// node is article, load article to get topics
ArticleFactory.get({ id: node.dbid }, function(data) {
if (data.topics) {
for (var i = 0; i < data.topics.length; i++)
data.topics[i] = data.topics[i].topic;
constructor(data.topics, node, topicNode);
}
});
if($scope.shown.articles)
TopicFactory.articles({id:node.dbid}, function(data) {
constructor(data, node, articleNode);
} else if (node.type === 'topic') {
// node is topic, load topic to get words and articles
if ($scope.shown.words)
TopicFactory.get({ id: node.dbid }, function(data) {
constructor(data.words, node, wordNode);
});
if ($scope.shown.articles)
TopicFactory.articles({ id: node.dbid }, function(data) {
constructor(data, node, articleNode);
});
} else if (node.type === 'word' && $scope.shown.topics) {
// node is word, load word to get topics
WordFactory.topics({ id: node.dbid }, function(data) {
constructor(data, node, topicNode);
});
} else if(node.type === 'word' && $scope.shown.topics) {
// node is word, load word to get topics
WordFactory.topics({id:node.dbid}, function(data) {
constructor(data, node, topicNode);
});
}
$scope.nodes.update(node);
}
$scope.nodes.update(node);
}
}, 500);
};
}, 500);
};
// on node open
$scope.open = function(props) {
$timeout.cancel(selectTimeout);
var node = $scope.nodes.get(props.nodes[0]);
$state.transitionTo(node.show, {id:node.dbid});
};
// on node open
$scope.open = function(props) {
$timeout.cancel(selectTimeout);
var node = $scope.nodes.get(props.nodes[0]);
$state.transitionTo(node.show, { id: node.dbid });
};
}]);
}
]);
/****************************************************************************
* Article Controllers
......@@ -262,71 +268,73 @@
/**
* Article Index route
*/
app.controller('ArticlesIndexController', ['$scope', '$state', '$location', 'ArticleFactory', 'Store',
function($scope, $state, $location, ArticleFactory, Store) {
$scope.page = Math.max($location.search().page || 1, 1);
$scope.limit = Vipra.config.pageSize;
$scope.$watchGroup(['page','sort','order'], function() {
ArticleFactory.query({
skip: ($scope.page-1)*$scope.limit,
limit: $scope.limit,
sort: $scope.order+$scope.sort
}, function(data, headers) {
$scope.articles = data;
$scope.articlesTotal = headers("V-Total");
$scope.maxPage = Math.ceil($scope.articlesTotal / $scope.limit);
app.controller('ArticlesIndexController', ['$scope', '$state', '$location', 'ArticleFactory',
function($scope, $state, $location, ArticleFactory) {
$scope.page = Math.max($location.search().page || 1, 1);
$scope.limit = Vipra.config.pageSize;
$scope.$watchGroup(['page', 'sort', 'order'], function() {
ArticleFactory.query({
skip: ($scope.page - 1) * $scope.limit,
limit: $scope.limit,
sort: $scope.order + $scope.sort
}, function(data, headers) {
$scope.articles = data;
$scope.articlesTotal = headers("V-Total");
$scope.maxPage = Math.ceil($scope.articlesTotal / $scope.limit);
});
});
});
}]);
}
]);
/**
* Article Show route
*/
app.controller('ArticlesShowController', ['$scope', '$stateParams', 'ArticleFactory',
function($scope, $stateParams, ArticleFactory, testService) {
function($scope, $stateParams, ArticleFactory) {
$scope.topicSort = $scope.topicSort || 'share';
$scope.topicSortRev = typeof $scope.topicSortRev === 'undefined' ? true : $scope.topicSortRev;
$scope.topicSort = $scope.topicSort || 'share';
$scope.topicSortRev = typeof $scope.topicSortRev === 'undefined' ? true : $scope.topicSortRev;
ArticleFactory.get({id: $stateParams.id}, function(data) {
$scope.article = data;
$scope.article.text = Vipra.createInitial($scope.article.text);
$scope.articleDate = Vipra.formatDate($scope.article.date);
$scope.articleCreated = Vipra.formatDateTime($scope.article.created);
$scope.articleModified = Vipra.formatDateTime($scope.article.modified);
ArticleFactory.get({ id: $stateParams.id }, function(data) {
$scope.article = data;
$scope.article.text = Vipra.createInitial($scope.article.text);
$scope.articleDate = Vipra.formatDate($scope.article.date);
$scope.articleCreated = Vipra.formatDateTime($scope.article.created);
$scope.articleModified = Vipra.formatDateTime($scope.article.modified);
// calculate percentage share
if($scope.article.topics) {
var topicShareSeries = [],
// calculate percentage share
if ($scope.article.topics) {
var topicShareSeries = [],
topics = $scope.article.topics;
for(var i = 0; i < topics.length; i++) {
topicShareSeries.push({name: topics[i].topic.name.ellipsize(20), y: topics[i].share});
}
// highcharts data
var topicShare = {
chart: { type: 'pie' },
credits: { enabled: false },
plotOptions: {
pie: { allowPointSelect: true }
},
title: { text: '' },
tooltip: { pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>' },
series: [{
name: 'Topic Share',
colorByPoint: true,
data: topicShareSeries
}]
};
for (var i = 0; i < topics.length; i++) {
topicShareSeries.push({ name: topics[i].topic.name.ellipsize(20), y: topics[i].share });
}
$scope.topicShare = topicShare;
}
});
// highcharts data
var topicShare = {
chart: { type: 'pie' },
credits: { enabled: false },
plotOptions: {
pie: { allowPointSelect: true }
},
title: { text: '' },
tooltip: { pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>' },
series: [{
name: 'Topic Share',
colorByPoint: true,
data: topicShareSeries
}]
};
$scope.topicShare = topicShare;
}
});
}]);
}
]);
/****************************************************************************
* Topic Controllers
......@@ -338,70 +346,72 @@
app.controller('TopicsIndexController', ['$scope', '$location', 'Store', 'TopicFactory',
function($scope, $location, Store, TopicFactory) {
$scope.page = Math.max($location.search().page || 1, 1);
$scope.limit = Vipra.config.pageSize;
$scope.sort = Store('sorttopics') || 'name';
$scope.order = Store('ordertopics') || '+';
$scope.$watchGroup(['page','sort','order'], function() {
TopicFactory.query({
skip: ($scope.page-1)*$scope.limit,
limit: $scope.limit,
sort: $scope.order+$scope.sort
}, function(data, headers) {
$scope.topics = data;
$scope.topicsTotal = headers("V-Total");
$scope.maxPage = Math.ceil($scope.topicsTotal / $scope.limit);
$scope.page = Math.max($location.search().page || 1, 1);
$scope.limit = Vipra.config.pageSize;
$scope.sort = Store('sorttopics') || 'name';
$scope.order = Store('ordertopics') || '+';
$scope.$watchGroup(['page', 'sort', 'order'], function() {
TopicFactory.query({
skip: ($scope.page - 1) * $scope.limit,
limit: $scope.limit,
sort: $scope.order + $scope.sort
}, function(data, headers) {
$scope.topics = data;
$scope.topicsTotal = headers("V-Total");
$scope.maxPage = Math.ceil($scope.topicsTotal / $scope.limit);
});
});
});
}]);
}
]);
/**
* Topic Show route
*/
app.controller('TopicsShowController', ['$scope', '$stateParams', '$timeout', 'TopicFactory', 'WebSocketService',
function($scope, $stateParams, $timeout, TopicFactory, WebSocketService) {
$scope.wordSort = $scope.wordSort || 'likeliness';
$scope.wordSortRev = typeof $scope.wordSortRev === 'undefined' ? true : $scope.wordSortRev;
TopicFactory.get({id: $stateParams.id}, function(data) {
$scope.topic = data;
$scope.topicCreated = Vipra.formatDateTime($scope.topic.created);
$scope.topicModified = Vipra.formatDateTime($scope.topic.modified);
});
$scope.startRename = function() {
$scope.origName = $scope.topic.name;
$scope.isRename = true;
$timeout(function() {
$('#topicName').select();
}, 0);
};
$scope.endRename = function(save) {
delete $scope.renameErrors;
if(save) {
TopicFactory.update({id:$scope.topic.id}, $scope.topic, function(data) {
$scope.topic = data;
app.controller('TopicsShowController', ['$scope', '$stateParams', '$timeout', 'TopicFactory',
function($scope, $stateParams, $timeout, TopicFactory) {
$scope.wordSort = $scope.wordSort || 'likeliness';
$scope.wordSortRev = typeof $scope.wordSortRev === 'undefined' ? true : $scope.wordSortRev;
TopicFactory.get({ id: $stateParams.id }, function(data) {
$scope.topic = data;
$scope.topicCreated = Vipra.formatDateTime($scope.topic.created);
$scope.topicModified = Vipra.formatDateTime($scope.topic.modified);
});
$scope.startRename = function() {
$scope.origName = $scope.topic.name;
$scope.isRename = true;
$timeout(function() {
$('#topicName').select();
}, 0);
};
$scope.endRename = function(save) {
delete $scope.renameErrors;
if (save) {
TopicFactory.update({ id: $scope.topic.id }, $scope.topic, function(data) {
$scope.topic = data;
$scope.isRename = false;
}, function(errors) {
$scope.renameErrors = Vipra.getErrors(errors);
});
} else {
$scope.isRename = false;
}, function(errors) {
$scope.renameErrors = Vipra.getErrors(errors);
});
} else {
$scope.isRename = false;
$scope.topic.name = $scope.origName;
}
};
$scope.topic.name = $scope.origName;
}
};
$scope.keyup = function($event) {
if($event.which === 13 || $event.which === 27) {
$scope.endRename($event.which === 13);
$event.preventDefault();
}
};
}]);
$scope.keyup = function($event) {
if ($event.which === 13 || $event.which === 27) {
$scope.endRename($event.which === 13);
$event.preventDefault();
}
};
}
]);
/**
* Topic Show Articles route
......@@ -409,35 +419,26 @@
app.controller('TopicsArticlesController', ['$scope', '$stateParams', '$location', 'Store', 'TopicFactory',
function($scope, $stateParams, $location, Store, TopicFactory) {
$scope.page = Math.max($location.search().page || 1, 1);
$scope.limit = Vipra.config.pageSize;
$scope.sort = Store('sortarticles') || 'title';
$scope.order = Store('orderarticles') || '+';
$scope.$watchGroup(['page','sort','order'], function() {
TopicFactory.articles({
id: $stateParams.id,
skip: ($scope.page-1)*$scope.limit,
limit: $scope.limit,
sort: $scope.order+$scope.sort
}, function(data, headers) {
$scope.articles = data;
$scope.articlesTotal = headers("V-Total");
$scope.maxPage = Math.ceil($scope.articlesTotal / $scope.limit);
$scope.page = Math.max($location.search().page || 1, 1);
$scope.limit = Vipra.config.pageSize;
$scope.sort = Store('sortarticles') || 'title';
$scope.order = Store('orderarticles') || '+';
$scope.$watchGroup(['page', 'sort', 'order'], function() {
TopicFactory.articles({
id: $stateParams.id,
skip: ($scope.page - 1) * $scope.limit,
limit: $scope.limit,
sort: $scope.order + $scope.sort
}, function(data, headers) {
$scope.articles = data;
$scope.articlesTotal = headers("V-Total");
$scope.maxPage = Math.ceil($scope.articlesTotal / $scope.limit);
});
});
});
}]);
/**
* Topic Show Similar route
*/
app.controller('TopicsSimilarController', ['$scope', '$stateParams', 'TopicFactory',
function($scope, $stateParams, TopicFactory) {
$scope.typeLabel = $stateParams.type.substring(3);
}]);
}
]);
/****************************************************************************
* Word Controllers
......@@ -449,25 +450,25 @@
app.controller('WordsIndexController', ['$scope', '$state', '$location', 'Store', 'WordFactory',
function($scope, $state, $location, Store, WordFactory) {
$scope.page = Math.max($location.search().page || 1, 1);
$scope.limit = 300;
$scope.sort = Store('sortwords') || 'id';
$scope.order = Store('orderwords') || '+';
$scope.$watchGroup(['page','sort','order'], function() {
WordFactory.query({
skip: ($scope.page-1)*$scope.limit,
limit: $scope.limit,
sort: $scope.sort,
sort: $scope.order+$scope.sort
}, function(data, headers) {
$scope.words = data;
$scope.wordsTotal = headers("V-Total");
$scope.maxPage = Math.ceil($scope.wordsTotal / $scope.limit);
$scope.page = Math.max($location.search().page || 1, 1);
$scope.limit = 300;
$scope.sort = Store('sortwords') || 'id';
$scope.order = Store('orderwords') || '+';
$scope.$watchGroup(['page', 'sort', 'order'], function() {
WordFactory.query({
skip: ($scope.page - 1) * $scope.limit,
limit: $scope.limit,
sort: $scope.order + $scope.sort
}, function(data, headers) {
$scope.words = data;
$scope.wordsTotal = headers("V-Total");
$scope.maxPage = Math.ceil($scope.wordsTotal / $scope.limit);
});
});
});
}]);
}
]);
/**
* Word Show route
......@@ -475,16 +476,17 @@
app.controller('WordsShowController', ['$scope', '$stateParams', 'WordFactory',
function($scope, $stateParams, WordFactory) {
WordFactory.get({id: $stateParams.id}, function(data) {
$scope.word = data;
$scope.wordCreated = Vipra.formatDateTime($scope.word.created);
});
WordFactory.get({ id: $stateParams.id }, function(data) {
$scope.word = data;
$scope.wordCreated = Vipra.formatDateTime($scope.word.created);
});
WordFactory.topics({id: $stateParams.id}, function(data) {
$scope.topics = data;
});
WordFactory.topics({ id: $stateParams.id }, function(data) {
$scope.topics = data;
});
}]);
}
]);
/****************************************************************************
* Directive Controllers
......@@ -496,16 +498,16 @@
app.controller('PaginationController', ['$scope',
function($scope) {
if(!$scope.page)
if (!$scope.page)
$scope.page = 1;
$scope.calculatePages = function() {
var pages = [],
max = Math.ceil($scope.total/$scope.limit*1.0),
pad = 4,
start = Math.max($scope.page - pad, 1),
end = Math.min(Math.max($scope.page + pad, start + pad * 2), max);
for(var i = start; i <= end; i++) {
max = Math.ceil($scope.total / $scope.limit * 1.0),
pad = 4,
start = Math.max($scope.page - pad, 1),
end = Math.min(Math.max($scope.page + pad, start + pad * 2), max);
for (var i = start; i <= end; i++) {
pages.push(i);
}
$scope.pages = pages;
......@@ -513,7 +515,7 @@
};
$scope.$watchGroup(['total', 'page', 'limit'], function(newVal, oldVal) {
if(!angular.equals(newVal, oldVal)) {
if (!angular.equals(newVal, oldVal)) {
$scope.calculatePages();
}
});
......@@ -522,14 +524,15 @@
$scope.changePage = function(page) {
$scope.page = page;
window.scrollTo(0,0);
window.scrollTo(0, 0);
};
$scope.toPage = function() {
var page = prompt("Enter a page number (between 1 and " + $scope.maxPage + ")");
if(page > 0 && page <= $scope.maxPage)
if (page > 0 && page <= $scope.maxPage)
$scope.changePage(page);
};
}]);
}
]);
})();
\ No newline at end of file
})();
......@@ -2,8 +2,11 @@
* Vipra Application
* Directives
******************************************************************************/
/* globals angular, $, console, Vipra */
(function() {
"use strict";
var app = angular.module('vipra.directives', [
'ui.router'
]);
......@@ -17,7 +20,7 @@
replace: true,
transclude: true,
template: '<a class="topic-link" ui-sref="topics.show({id:topic.id})"><span ng-bind="topic.name"></span><ng-transclude/></a>'
}
};
});
app.directive('articleLink', function() {
......@@ -29,7 +32,7 @@
replace: true,
transclude: true,
template: '<a class="article-link" ui-sref="articles.show({id:article.id})"><span ng-bind="article.title"></span><ng-transclude/></a>'
}
};
});
app.directive('pagination', function() {
......@@ -54,7 +57,7 @@
},
link: function($scope, $element) {
$scope.$watch('highcharts', function(newVal) {
if(!newVal) return;
if (!newVal) return;
$element.highcharts($scope.highcharts);
});
......@@ -72,12 +75,12 @@
template: '<a class="hide-link" href="#" ng-bind="text" ng-click="change($event)"></a>',
link: function($scope) {
var target = $($scope.target);
if(!target.length) return;
if (!target.length) return;
var store = $scope.store !== 'false';
var name = 'hidelink-' + $scope.target;
if(store) {
if (store) {
var visible = Store(name);
if(visible !== null && typeof visible !== 'undefined')
if (visible !== null && typeof visible !== 'undefined')
target.toggle(visible);
}
......@@ -90,7 +93,7 @@
var visible = !target.is(':visible');
setText(visible);
target.slideToggle();
if(store)
if (store)
Store(name, visible);
};
......@@ -106,7 +109,7 @@
$($attrs.target).slideToggle();
});
}
}
};
});
app.directive('ngModelStore', ['Store', function(Store) {
......@@ -114,15 +117,15 @@
restrict: 'A',
require: 'ngModel',
link: function($scope, $elem, $attrs, $ctrl) {
if(!$attrs.ngModelStore) {
if (!$attrs.ngModelStore) {
console.log("no store key given");
return;
}
var value = Store($attrs.ngModelStore);
if(value != null) {
if (value !== null) {
$ctrl.$setViewValue(value);
$ctrl.$render();
} else if($attrs.ngModelDefault) {
} else if ($attrs.ngModelDefault) {
$ctrl.$setViewValue($scope.$eval($attrs.ngModelDefault));
$ctrl.$render();
}
......@@ -145,7 +148,7 @@
link: function($scope) {
$scope.dropdownId = Vipra.randomId();
$scope.align = 'dropdown-menu-left';
if($scope.align === 'right')
if ($scope.align === 'right')
$scope.align = 'dropdown-menu-right';
}
};
......@@ -161,7 +164,7 @@
replace: true,
templateUrl: 'html/directives/alert.html',
link: function($scope) {
if(!$scope.type) {
if (!$scope.type) {
console.log('no alert type given');
return;
}
......@@ -169,7 +172,7 @@
$scope.dismissible = $scope.dismissible !== 'false';
var classes = 'alert alert-' + $scope.type;
if($scope.dismissible)
if ($scope.dismissible)
classes += ' alert-dismissible';
$scope.classes = classes;
}
......@@ -182,10 +185,7 @@
ngModel: '='
},
restrict: 'E',
templateUrl: 'html/directives/checkbox.html',
link: function($scope) {
}
templateUrl: 'html/directives/checkbox.html'
};
});
......@@ -201,12 +201,12 @@
transclude: true,
template: '<span ng-click="click()"><ng-transclude/><span ng-show="sortType == sortBy" class="caret" ng-class="{\'caret-up\':sortReverse}"></span></span>',
link: function($scope, $elem, $attrs) {
if(!$attrs.sortBy) {
if (!$attrs.sortBy) {
console.log('no sort by key given');
return;
}
var value = Store($scope.storeKey);
if(value) {
if (value) {
value = value.split(',');
$scope.sortType = value[0];
$scope.sortReverse = value[1] === 'true';
......@@ -227,17 +227,16 @@
ngModel: '=',
values: '='
},
require: 'ngModel',
template: '<span ng-bind-html="label" ng-click="next()"></span>',
link: function($scope, $elem, $attrs, $ctrl) {
link: function($scope) {
var hasObjects = false;
if($scope.values && $scope.values.length)
if ($scope.values && $scope.values.length)
hasObjects = angular.isObject($scope.values[0]);
var index = function() {
if(hasObjects) {
for(var i = 0; i < $scope.values.length; i++) {
if($scope.values[i].value == $scope.ngModel)
if (hasObjects) {
for (var i = 0; i < $scope.values.length; i++) {
if ($scope.values[i].value == $scope.ngModel)
return i;
}
return 0;
......@@ -247,8 +246,8 @@
};
var select = function(i) {
if(i < 0 || i >= $scope.values.length) return;
if(hasObjects) {
if (i < 0 || i >= $scope.values.length) return;
if (hasObjects) {
$scope.ngModel = $scope.values[i].value;
$scope.label = $scope.values[i].label;
} else {
......@@ -257,7 +256,7 @@
};
$scope.next = function() {
select((index() + 1) % $scope.values.length)
select((index() + 1) % $scope.values.length);
};
$scope.$watch('ngModel', function() {
......@@ -267,4 +266,4 @@
};
});
})();
\ No newline at end of file
})();
......@@ -2,8 +2,11 @@
* Vipra Application
* Factories
******************************************************************************/
/* globals angular, Vipra */
(function() {
"use strict";
var app = angular.module('vipra.factories', []);
app.factory('ArticleFactory', ['$resource', function($resource) {
......@@ -36,11 +39,11 @@
return function(key, value) {
key += '-' + $state.current.name;
var lsSupport = false;
// Check for native support
if (localStorage)
lsSupport = true;
// If value is detected, set new or modify store
if (typeof value !== "undefined" && value !== null) {
// Convert object values to JSON
......@@ -52,23 +55,24 @@
else
createCookie(key, value, 30);
}
// No value supplied, return value
if (typeof value === "undefined") {
var data;
if (lsSupport)
data = localStorage.getItem(key);
else
data = readCookie(key);
try {
data = JSON.parse(data);
} catch(e) {
} catch (e) {
data = data;
}
return data;
}
// Null specified, remove store
if (value === null) {
if (lsSupport)
......@@ -76,7 +80,7 @@
else
createCookie(key, '', -1);
}
// Creates new cookie or removes cookie with negative expiration
function createCookie(key, value, exp) {
var date = new Date();
......@@ -84,7 +88,7 @@
var expires = "; expires=" + date.toGMTString();
document.cookie = key + "=" + value + expires + "; path=/";
}
// Returns contents of cookie
function readCookie(key) {
var nameEQ = key + "=";
......@@ -101,11 +105,11 @@
app.service('WebSocketService', ['$websocket', '$state', function($websocket, $state) {
var socket = $websocket(Vipra.config.websocketUrl),
callback = null;
callback = null;
socket.onMessage(function(message) {
var data = JSON.parse(message.data);
if(callback)
if (callback)
callback(data);
});
......@@ -117,7 +121,7 @@
callback = fn;
};
this.send(1, {state: $state.current.name});
this.send(1, { state: $state.current.name });
}]);
})();
\ No newline at end of file
})();
......@@ -2,8 +2,11 @@
* Vipra Application
* Filters
******************************************************************************/
/* globals angular, Vipra */
(function() {
"use strict";
var app = angular.module('vipra.filters', []);
app.filter('toPercent', function() {
......@@ -18,4 +21,4 @@
return Vipra.formatDateTime;
});
})();
\ No newline at end of file
})();
......@@ -2,8 +2,11 @@
* Vipra Application
* Helpers & Polyfills
******************************************************************************/
/* globals Vipra */
(function() {
"use strict";
window.Vipra = window.Vipra || {};
/**
......@@ -20,7 +23,7 @@
};
Vipra.toPercent = function(input) {
if(typeof input !== 'number')
if (typeof input !== 'number')
input = parseInt(input, 10);
return Math.round(input * 100);
};
......@@ -35,39 +38,39 @@
Vipra.getErrors = function(errors) {
var html = [];
if(errors && errors.length) {
for(var i = 0; i < errors.length; i++)
if (errors && errors.length) {
for (var i = 0; i < errors.length; i++)
html.push('<strong>' + errors[i].title + '</strong>: ' + errors[i].detail);
}
return html.join('<br>');
}
};
/**
* Polyfills
*/
if(typeof String.prototype.ellipsize === 'undefined')
if (typeof String.prototype.ellipsize === 'undefined')
String.prototype.ellipsize = function(max) {
max = max || 20;
if(this.length > max) {
if (this.length > max) {
return this.substring(0, max) + '...';
}
return this;
};
if(typeof String.prototype.multiline === 'undefined')
if (typeof String.prototype.multiline === 'undefined')
String.prototype.multiline = function(max) {
return this.split(new RegExp("((?:\\w+ ){" + max + "})", "g")).filter(Boolean).join("\n");
};
if(typeof String.prototype.startsWith === 'undefined')
if (typeof String.prototype.startsWith === 'undefined')
String.prototype.startsWith = function(start) {
return this.lastIndexOf(start, 0) === 0;
};
if(typeof window.console === 'undefined')
if (typeof window.console === 'undefined')
window.console = {
log: function () {}
log: function() {}
};
})();
\ No newline at end of file
})();
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment