Skip to content

Instantly share code, notes, and snippets.

@naquad
Created March 22, 2014 23:33
Show Gist options
  • Save naquad/9716061 to your computer and use it in GitHub Desktop.
Save naquad/9716061 to your computer and use it in GitHub Desktop.
var app = angular.module('samlib', ['ui.modal']);
app.config(['$httpProvider', function($httpProvider){
$httpProvider.responseInterceptors.push('httpRestarter');
}]);
app.factory('httpRestarter', ['$q', '$injector', '$timeout', 'notifier', function($q, $injector, $timeout, notifier){
var $http;
return function(r){
return r.catch(function(r){
if(r.status == 400)
return $q.reject(r);
notifier.danger('Request error: ' + r.status);
var again = $q.defer();
$timeout(function(){
$http = $http || $injector.get('$http');
again.resolve($http(r.config));
}, 5000);
return again.promise;
});
};
}]);
app.service('notifier', function(){
var notifier = function(opts){
$.UIkit.notify(opts);
};
notifier.show = function(text, type, timeout, pos){
notifier({
'message': text,
'status': type,
'timeout': timeout || 0,
'pos': pos || 'bottom-right'
});
};
notifier.info = function(tt, to, p){
notifier.show(tt, 'info', to, p);
};
notifier.success = function(tt, to, p){
notifier.show(tt, 'success', to, p);
};
notifier.warning = function(tt, to, p){
notifier.show(tt, 'warning', to, p);
};
notifier.danger = function(tt, to, p){
notifier.show(tt, 'danger', to, p);
};
return notifier;
});
app.service('dialogs', ['$modal', 'urls', function($modal, urls){
return {
edit: function(url){
return $modal.open({
templateUrl: 'templates/edit.html',
controller: 'edit',
resolve: {
url: function(){
return url;
}
}
});
},
add: function(){
return $modal.open({
templateUrl: 'templates/add.html',
controller: 'edit',
resolve: {
url: function(){
return urls.make();
}
}
});
},
info: function(url){
return $modal.open({
templateUrl: 'templates/info.html',
controller: 'info',
resolve: {
url: function(){
return url;
}
}
});
},
remove: function(url){
return $modal.open({
templateUrl: 'templates/remove.html',
controller: 'remove',
resolve: {
url: function(){
return url;
}
}
});
}
};
}]);
app.service('urls', ['$http', '$q', function($http, $q){
function query(action, what){
switch(action){
case 'index':
return $http.get('api.php/urls');
case 'show':
return $http.get('api.php/urls/' + what.id);
case 'update':
return $http.put('api.php/urls/' + what.id, what);
case 'create':
return $http.post('api.php/urls', what);
case 'remove':
return $http.delete('api.php/urls/' + what.id);
case 'force-poll':
return $http.get('api.php/urls/' + what.id + '/update');
case 'test':
return $http.post('api.php/urls/test', what);
default:
throw "Don't know how to do " + action;
}
}
var urls = [];
function Url(data){
if($.isPlainObject(data))
$.extend(this, data);
}
Url.prototype.isError = function(){
return this.last_error && this.last_update;
};
Url.prototype.state = function(){
if(!this.last_update)
return 'pending';
if(this.last_error)
return 'error';
return 'ok';
};
Url.prototype.test = function(){
return query('test', this);
};
Url.prototype.update = function(){
var self = this;
return query('force-poll', this).success(function(data){
$.extend(self, data);
});
};
Url.prototype.reload = function(){
if(this.id){
var self = this;
return query('show', this).success(function(data){
$.extend(self, data);
});
}
throw "Trying to reload new URL";
};
Url.prototype.save = function(){
if(this.id)
return query('update', this);
var self = this;
return query('create', this).success(function(data){
$.extend(self, data);
urls.push(self);
});
};
Url.prototype.remove = function(){
if(!this.id)
throw "Trying to remove new URL";
var self = this;
return query('remove', this).success(function(){
var idx = urls.indexOf(self);
if(idx != -1)
urls.splice(idx, 1);
});
};
return {
make: function(data){
return new Url(data);
},
get: function(){
var defer = $q.defer();
query('index').success(function(data){
urls = $.map(data, function(x){
return new Url(x);
});
defer.resolve(urls);
});
return defer.promise;
}
};
}]);
app.controller('adder', ['$scope', 'dialogs', function($scope, dialogs){
$scope.add = dialogs.add;
}]);
app.controller('edit', ['$scope', '$modalInstance', 'url', function($scope, $modalInstance, url){
$scope.errors = {};
$scope.url = url;
$scope.verified = null;
$scope.verifying = false;
$scope.submitting = false;
$scope.cancel = function(){
$modalInstance.dismiss('cancel');
};
$scope.test = function(){
$scope.verifying = true;
url.test().success(function(data){
if(data.success){
$scope.verified = data;
}else{
$scope.verified = null;
$scope.errors.url = data.message;
}
$scope.verifying = false;
});
};
$scope.save = function(){
$scope.submitting = true;
$scope.verified = null;
url.save().success(function(){
$modalInstance.close(url);
}).error(function(data){
$scope.errors = data;
}).finally(function(){
$scope.submitting = false;
});
};
$scope.resetVerified = function(){
$scope.verified = null;
$scope.errors = {};
};
}]);
app.controller('info', ['$scope', '$modalInstance', 'url', function($scope, $modalInstance, url){
$scope.url = url;
$scope.updating = false;
$scope.close = function(){
$modalInstance.dismiss('close');
};
$scope.update = function(){
if($scope.updating)
return;
url.update().success(function(){
$scope.updating = false;
})
$scope.updating = true;
};
}]);
app.controller('remove', ['$scope', '$modalInstance', 'url', function($scope, $modalInstance, url){
$scope.url = url;
$scope.cancel = function(){
$modalInstance.dismiss('cancel');
};
$scope.remove = function(){
url.remove().success(function(){
$modalInstance.dismiss('ok');
});
};
}]);
app.controller('list', ['$scope', 'dialogs', 'urls', function($scope, dialogs, urls){
$scope.remove = dialogs.remove;
$scope.info = dialogs.info;
$scope.edit = dialogs.edit;
urls.get().then(function(urls){
$scope.urls = urls;
});
}]);
app.filter('prettyUrl', function(){
return function(text){
return text.replace(/^https?:\/\//, '');
};
});
app.filter('humanize', function(){
return function(text){
return (text || '-').replace(/^./, function(x){
return x.toUpperCase();
}).replace('_', ' ');
};
});
app.filter('fullDate', ['$filter', function($filter){
return function(val, if_empty){
return val ? $filter('date')(val * 1000, 'MM/dd/yyyy HH:mm:ss') : if_empty || '-';
};
}]);
app.filter('onlyDate', ['$filter', function($filter){
return function(val, if_empty){
return val ? $filter('date')(val * 1000, 'MM/dd/yyyy') : if_empty || '-';
};
}]);
app.filter('default', function(){
return function(val, empty){
return val ? val : empty || '-';
};
});
app.directive('tooltip', function(){
return {
restrict: 'A',
link: function(scope, element, attrs){
var tt = new $.UIkit.tooltip(element, '');
attrs.$observe('tooltip', function(val){
tt.tip = val;
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment