Skip to content
This repository has been archived by the owner on Feb 2, 2019. It is now read-only.

Commit

Permalink
feat(build): add setBaseUrl to override where to load templates and s…
Browse files Browse the repository at this point in the history
…tyles from

 - use custom url resolver to map template and style sheet requests to the base url, if set.
 - supports the quick-start use-case where you don't want to build anything.
  • Loading branch information
justindujardin committed Dec 20, 2015
1 parent 2e223e9 commit 3ea1270
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ module.exports = function (grunt) {
{expand: true, src: 'config.js', dest: '<%- sitePath %>/<%- pkg.version %>/'},
{expand: true, src: 'ng2-material/**/*', dest: '<%- sitePath %>/<%- pkg.version %>/'},
{expand: true, src: 'dist/*.*', dest: '<%- sitePath %>/<%- pkg.version %>/'},
{expand: true, cwd: 'public/font/', flatten: true, src: ['*.*'], dest: '<%- sitePath %>/<%- pkg.version %>/dist/'},
{expand: true, src: 'public/**/*', dest: '<%- sitePath %>/<%- pkg.version %>/'},
{expand: true, src: 'examples/**/*', dest: '<%- sitePath %>/<%- pkg.version %>/'}
]
Expand Down Expand Up @@ -101,6 +102,7 @@ module.exports = function (grunt) {
src: [
"examples/*.scss",
"examples/**/*.scss",
"public/font/*.scss",
"<%- sourceRoot %>/all.scss",
"<%- sourceRoot %>/components/**/*.scss"
],
Expand Down
46 changes: 43 additions & 3 deletions ng2-material/all.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {CONST_EXPR, Type} from 'angular2/src/facade/lang';
import {provide} from 'angular2/core';

import {MdAnchor, MdButton} from './components/button/button';
export * from './components/button/button';
Expand Down Expand Up @@ -44,6 +45,7 @@ import {MdToolbar} from './components/toolbar/toolbar';
export * from './components/toolbar/toolbar';

import {MdTabs, MdTab} from './components/tabs/tabs';
import {UrlResolver} from "angular2/compiler";
export * from './components/toolbar/toolbar';

/**
Expand All @@ -66,9 +68,47 @@ export const MATERIAL_DIRECTIVES: Type[] = CONST_EXPR([
MdTab, MdTabs
]);


/**
* Reference to specified base load URL for templates and styles.
* @private
*/
var BASE_URL: string = null;

/**
* Specify the baseUrl to load templates and styles from.
* @param url
*/
export function setBaseUrl(url: string) {
BASE_URL = url;
}

/**
* This is a workaround to tell us where to load templates and styles from until
* we have a better template bundling strategy.
*/
export class MaterialTemplateResolver extends UrlResolver {
static RESOURCE_MATCHER: RegExp = /^ng2-material\/.*?\.(html|css)$/;

resolve(baseUrl: string, url: string): string {
if (!BASE_URL) {
return super.resolve(baseUrl, url);
}
if (baseUrl.startsWith(BASE_URL)) {
baseUrl = baseUrl.substr(0, BASE_URL.length);
}
let result = super.resolve(baseUrl, url);
if (MaterialTemplateResolver.RESOURCE_MATCHER.test(result)) {
return `${BASE_URL}${result}`;
}
return result;
}
}

/**
* Collection of Material Design component providers.
*/
export const MATERIAL_PROVIDERS: Type[] = CONST_EXPR([
MdRadioDispatcher
]);
export const MATERIAL_PROVIDERS: any[] = [
MdRadioDispatcher,
provide(UrlResolver, {useValue: new MaterialTemplateResolver()})
];

0 comments on commit 3ea1270

Please sign in to comment.