Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

feat(themes): register theme on the fly #9413

Merged
merged 1 commit into from
Aug 31, 2016
Merged

Conversation

EladBezalel
Copy link
Member

@EladBezalel EladBezalel commented Aug 26, 2016

  • Added defineTheme functionality to $mdTheming service
  • Added support for promise and a function that returns a promise on md-theme directive to support async build of themes

fixes #2965

@EladBezalel EladBezalel added the needs: review This PR is waiting on review from the team label Aug 26, 2016
@EladBezalel EladBezalel force-pushed the feat/themes-on-the-fly branch from e0ecde8 to 9310a84 Compare August 26, 2016 11:19
@EladBezalel EladBezalel added needs: work and removed needs: review This PR is waiting on review from the team labels Aug 26, 2016
@EladBezalel
Copy link
Member Author

@topherfangio we should find a better approach

it('does not warns when an unregistered theme is used', function() {
inject(function($log, $compile, $rootScope) {
spyOn($log, 'warn');
$compile('<div md-deferred-theme="unregistered"></div>')($rootScope);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You actually would want this to warn because the theme isn't registered. This approach loses that information.

@ErinCoughlan
Copy link
Contributor

@EladBezalel I have another idea. Instead of using a string in md-theme, can you use an object, at least in the deferred class? That way we know if it will be created since the object needs to come from somewhere.

@EladBezalel EladBezalel force-pushed the feat/themes-on-the-fly branch from 9310a84 to 3910466 Compare August 28, 2016 12:32
@EladBezalel EladBezalel added the in progress Mainly for in progress PRs, but may be used for issues that require multiple PRs label Aug 28, 2016
@EladBezalel EladBezalel force-pushed the feat/themes-on-the-fly branch 3 times, most recently from 2681002 to c72246e Compare August 31, 2016 12:10
@EladBezalel EladBezalel added needs: review This PR is waiting on review from the team and removed in progress Mainly for in progress PRs, but may be used for issues that require multiple PRs needs: work labels Aug 31, 2016
}
// Iterating backwards to support unregistering during iteration
// http://stackoverflow.com/a/9882349/890293
for (var i = registeredCallbacks.length - 1; i >= 0; i -= 1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't the following work?

registeredCallbacks.reverse().forEach(function(callback) {

Note: forEach keeps the right order.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe 😅 i'll check

@EladBezalel EladBezalel force-pushed the feat/themes-on-the-fly branch from c72246e to ac16f25 Compare August 31, 2016 12:31
// Allow us to be invoked via a linking function signature.
var applyTheme = function (scope, el) {
if (el === undefined) { el = scope; scope = undefined; }
if (scope === undefined) { scope = $rootScope; }
applyTheme.inherit(el, el);
};

applyTheme.THEMES = angular.extend({}, THEMES);
applyTheme.PALETTES = angular.extend({}, PALETTES);
Object.defineProperty(applyTheme, 'THEMES', {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this complexity... unless you want generate the THEMES object on demand (each time the getter is accessed).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, generating it once doesn't support themes that are registered on the fly

Copy link
Contributor

@hansl hansl Aug 31, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading this code, the following assertion happen, which seems like the wrong logic:

applyTheme.THEMES !== applyTheme.THEMES

And any app that changes THEMES (say by calling generateTheme) would not change references to THEMES that already exist.

@ThomasBurleson ThomasBurleson added the in progress Mainly for in progress PRs, but may be used for issues that require multiple PRs label Aug 31, 2016
@ThomasBurleson ThomasBurleson added this to the 1.1.1 milestone Aug 31, 2016
@ThomasBurleson ThomasBurleson added g3: pr This PR was posted by an internal or external product team. g3: reported The issue was reported by an internal or external product team. and removed g3: pr This PR was posted by an internal or external product team. in progress Mainly for in progress PRs, but may be used for issues that require multiple PRs needs: review This PR is waiting on review from the team labels Aug 31, 2016
@topherfangio
Copy link
Contributor

Looks pretty interesting. Any usage docs so that we can play with it and test it out? 😃

@EladBezalel
Copy link
Member Author

Actually I was testing on button demo, had a timeout to resolve a new defined theme after 2 seconds

@topherfangio
Copy link
Contributor

@EladBezalel Ok. Can you share that code at least so that we can do some testing on our local machines?

Also, this will need docs before we merge it, right?

@ThomasBurleson ThomasBurleson added the in progress Mainly for in progress PRs, but may be used for issues that require multiple PRs label Aug 31, 2016
@EladBezalel
Copy link
Member Author

There are no docs for the theming service at all.. I wanted to add it but it requires even more work on that..
I can fix the comments and work on another PR for documentation to mdTheming service

@EladBezalel
Copy link
Member Author

EladBezalel commented Aug 31, 2016

Run gulp watch site --dev and go to
http://codepen.io/EladBezalel/pen/YGKBYE
You can also remove md-theme-watch to see how it's not being changed

@EladBezalel EladBezalel force-pushed the feat/themes-on-the-fly branch from ac16f25 to f6e667e Compare August 31, 2016 16:45
if ((alwaysWatchTheme && !registerChangeCallback()) || (!alwaysWatchTheme && watchTheme)) {
el.on('$destroy', $rootScope.$watch(lookupThemeName, updateThemeClass) );
if (ctrl) {
var watchTheme = alwaysWatchTheme || ctrl.$shouldWatch || $mdUtil.parseAttributeBoolean(el.attr('md-theme-watch'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: line too long here :)

@ThomasBurleson ThomasBurleson added pr: merge ready This PR is ready for a caretaker to review and removed in progress Mainly for in progress PRs, but may be used for issues that require multiple PRs labels Aug 31, 2016

it('should accept $q promise as a theme', function() {
inject(function($compile, $rootScope, $q, $mdTheming) {
$rootScope.promise = $q.resolve('red');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I thought there was a method on $mdTheming that returns a promise (which eventually is the resolve). To make this test more robust and closer to real use, can you use that method instead of just resolve with a string.

@EladBezalel EladBezalel force-pushed the feat/themes-on-the-fly branch 3 times, most recently from 54328ae to 72ff924 Compare August 31, 2016 17:24
@@ -453,6 +453,7 @@ describe('$mdThemeProvider with custom styles', function() {
// Verify that $MD_THEME_CSS is still set to '/**/' in the test environment.
// Check angular-material-mocks.js for $MD_THEME_CSS latest value if this test starts to fail.
expect($MD_THEME_CSS).toBe('/**/');
expect($MD_THEME_CSS).toBe('/**/');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplicate line?

@ErinCoughlan
Copy link
Contributor

LGTM

registeredCallbacks.forEach(function (cb) {
cb();
// Iterating backwards to support unregistering during iteration
// http://stackoverflow.com/a/9882349/890293
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't that comment now invalid? since we are now using a simple reverse ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still explaining why are we iterating backwards

Copy link
Member

@devversion devversion Aug 31, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The URL does in my case only shows how to loop backwards. But does not explain why we do this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The array is being re-indexed when you do a .splice(), which means you'll skip over an index when one is removed, and your cached .length is obsolete.

Copy link
Member

@devversion devversion Aug 31, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a big deal. But where do we explicitly use a splice here? I can see no splice and .length` anymore :)

The comment is reasonable if we still use the snippet of that Stackoverflow, and it's still difficult enough to require some extra documentation.

Let's keep it as it is.

@EladBezalel EladBezalel force-pushed the feat/themes-on-the-fly branch from 72ff924 to 6837c1b Compare August 31, 2016 17:47
- Added `defineTheme` functionality to `$mdTheming` service
- Added support for promise and a function that returns a promise on `md-theme` directive to support async build of themes

fixes #2965
@EladBezalel EladBezalel force-pushed the feat/themes-on-the-fly branch from 6837c1b to 433a305 Compare August 31, 2016 18:05
@hansl hansl merged commit 0d2386c into master Aug 31, 2016
@EladBezalel EladBezalel deleted the feat/themes-on-the-fly branch August 31, 2016 22:22
hansl added a commit to hansl/material that referenced this pull request Aug 31, 2016
ThomasBurleson pushed a commit that referenced this pull request Aug 31, 2016
@EladBezalel EladBezalel restored the feat/themes-on-the-fly branch August 31, 2016 23:32
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
g3: reported The issue was reported by an internal or external product team. pr: merge ready This PR is ready for a caretaker to review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

$mdThemingProvider - registering a theme on the fly
6 participants