Skip to content

Instantly share code, notes, and snippets.

@miguel-leon
Created June 19, 2017 00:41
Show Gist options
  • Save miguel-leon/a7dd0608c77cf059106d9cfae76799cc to your computer and use it in GitHub Desktop.
Save miguel-leon/a7dd0608c77cf059106d9cfae76799cc to your computer and use it in GitHub Desktop.
Routes configurator for angular 1.5
angular.module('configurators', ['ngRoute', 'ngMaterial'])
.config(['$provide', '$routeProvider', 'Resolver', function ($provide, $routeProvider, Resolver) {
var modules = {};
$provide.constant('RouteConfigurationProvider', function (module, options) {
if (arguments.length === 1) {
if (angular.isObject(module)) {
options = module;
module = null;
}
else {
return modules[module];
}
}
options = angular.extend({
routeDefaults: null,
viewsBase: null,
templateExtension: 'html'
}, options);
var templateResolver;
if (options.viewsBase) {
templateResolver = Resolver(options.viewsBase, {
relative: true,
extension: options.templateExtension
});
}
var provider = Object.create($routeProvider);
provider.when = function (path, route) {
if (options.routeDefaults) {
route = angular.extend({}, options.routeDefaults, route);
}
if (options.viewsBase && route.templateName) {
route.templateUrl = templateResolver(route.templateName);
}
$routeProvider.when(path, route);
return provider;
};
if (module) {
modules[module] = provider;
}
return provider;
});
}])
.config(['$provide', 'Resolver', function ($provide, Resolver) {
$provide.constant('ComponentConfigurationProvider', function (module, options) {
options = angular.extend({
componentsBase: null,
templateExtension: 'html',
componentOwnFolder: true
}, options);
if (options.componentsBase) {
var templateResolver = Resolver(options.componentsBase, {
relative: true,
extension: options.templateExtension
});
angular.module(module)._invokeQueue.filter(function (item) {
return item[1] === 'component';
})
.forEach(function (item) {
var defObj = item[2][1];
if (!(defObj.template || defObj.templateUrl)) {
var templateName = defObj.templateName;
if (!templateName) {
var name = camelToKebabCase(item[2][0]);
templateName = options.componentOwnFolder ? name + '/' + name : name;
}
defObj.templateUrl = templateResolver(templateName);
}
});
}
});
function camelToKebabCase(str) {
return str.replace(/[A-Z]/g, function ($0) {
return "-" + $0.toLowerCase();
});
}
}])
.provider('DialogConfiguration', ['$mdDialogProvider', 'Resolver', '$controllerProvider', function ($mdDialogProvider, Resolver, $controllerProvider) {
var buildScope, extendScope;
function DialogConfigurationProvider(module, options) {
options = angular.extend({
dialogsBase: null,
templateExtension: 'html',
dialogOwnFolder: true
}, options);
if (options.dialogsBase) {
var templateResolver = Resolver(options.dialogsBase, {
relative: true,
extension: options.templateExtension
});
}
$mdDialogProvider.addMethod(module, function (localOptions) {
localOptions = localOptions || {};
if (localOptions.templateName) {
if (options.dialogsBase) {
localOptions.templateUrl = templateResolver(localOptions.templateName + (options.dialogOwnFolder ? '/' + localOptions.templateName : ''));
}
if (angular.isUndefined(localOptions.controller)) {
var controller = kebabToCamelCase(localOptions.templateName, true) + 'Controller';
if ($controllerProvider.has(controller)) {
localOptions.controller = controller;
}
}
}
if (!localOptions.scope) {
localOptions.scope = buildScope(localOptions.locals);
}
extendScope(localOptions.scope, localOptions.locals);
localOptions.controllerAs = 'dialog';
localOptions.bindToController = true;
return localOptions;
});
}
DialogConfigurationProvider.$get = ['$rootScope', '$mdDialog', function ($rootScope, $mdDialog) {
buildScope = function () {
return $rootScope.$new(true);
};
extendScope = function (scope, locals) {
return angular.extend(scope, locals, {
$hide: $mdDialog.hide,
$cancel: $mdDialog.cancel
});
};
}];
return DialogConfigurationProvider;
function kebabToCamelCase(str, firstLetter) {
if (firstLetter) {
str = str.charAt(0).toUpperCase() + str.slice(1);
}
return str.replace(/\-([a-z])/g, function ($0, $1) {
return $1.toUpperCase();
});
}
}])
// Force DialogConfiguration instantiation
.run(['DialogConfiguration', function () {}])
.constant('Resolver', (function () {
var defaults = {
separator: '/',
extension: false,
relative: false
};
return function Resolver(base, options) {
options = angular.extend({}, defaults, options);
base = sanitize(base, options.separator) || options.separator;
if (options.relative) {
base = base.slice(1);
}
return function resolver(path) {
return base + sanitize(path, options.separator) + (options.extension ? '.' + options.extension : '');
};
};
function sanitize(path, separator) {
return angular.isString(path) ? path.split(separator).reduce(function (res, part) {
return part ? res + separator + part : res;
}, '') : '';
}
})())
.config(['RouteConfigurationProvider', 'ComponentConfigurationProvider', 'DialogConfigurationProvider',
function (RouteConfigurationProvider, ComponentConfigurationProvider, DialogConfigurationProvider) {
ComponentConfigurationProvider('myapp', {
componentsBase: 'myapp/components'
});
DialogConfigurationProvider('myapp', {
dialogsBase: 'myapp/dialogs'
});
RouteConfigurationProvider('myapp', {
viewsBase: 'myapp/views',
routeDefaults: {
bindToController: true,
controllerAs: 'view'
}
});
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment