Commit 21ec36c8 authored by michaelpg's avatar michaelpg Committed by Commit bot

MD Settings: Refactor RoutableBehavior into MainPageBehavior

This merges RoutableBehavior into MainPageBehavior since there's no clear
separation between them anymore, and "Routable" is a weird name.

This is a mechanical change only. Other than merging 2 behaviors into 1 and making a couple functions @private, there's no logical changes, and the behavior should be identical.

BUG=589681
R=dschuyler@chromium.org
CQ_INCLUDE_TRYBOTS=master.tryserver.chromium.linux:closure_compilation

Review-Url: https://codereview.chromium.org/2211303004
Cr-Commit-Position: refs/heads/master@{#410433}
parent 3a94b6f1
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
Polymer({ Polymer({
is: 'settings-about-page', is: 'settings-about-page',
behaviors: [WebUIListenerBehavior, RoutableBehavior, I18nBehavior], behaviors: [WebUIListenerBehavior, MainPageBehavior, I18nBehavior],
properties: { properties: {
/** @private {?UpdateStatusChangedEvent} */ /** @private {?UpdateStatusChangedEvent} */
......
...@@ -10,12 +10,10 @@ ...@@ -10,12 +10,10 @@
Polymer({ Polymer({
is: 'settings-advanced-page', is: 'settings-advanced-page',
behaviors: [SettingsPageVisibility, RoutableBehavior], behaviors: [SettingsPageVisibility, MainPageBehavior],
properties: { properties: {
/** /** Preferences state. */
* Preferences state.
*/
prefs: { prefs: {
type: Object, type: Object,
notify: true, notify: true,
......
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
'../compiled_resources2.gyp:route', '../compiled_resources2.gyp:route',
'../settings_page/compiled_resources2.gyp:main_page_behavior', '../settings_page/compiled_resources2.gyp:main_page_behavior',
'../settings_page/compiled_resources2.gyp:settings_page_visibility', '../settings_page/compiled_resources2.gyp:settings_page_visibility',
'../settings_page/compiled_resources2.gyp:transition_behavior',
'../system_page/compiled_resources2.gyp:system_page', '../system_page/compiled_resources2.gyp:system_page',
], ],
'includes': ['../../../../../third_party/closure_compiler/compile_js2.gypi'], 'includes': ['../../../../../third_party/closure_compiler/compile_js2.gypi'],
......
...@@ -9,12 +9,10 @@ ...@@ -9,12 +9,10 @@
Polymer({ Polymer({
is: 'settings-basic-page', is: 'settings-basic-page',
behaviors: [SettingsPageVisibility, RoutableBehavior], behaviors: [SettingsPageVisibility, MainPageBehavior],
properties: { properties: {
/** /** Preferences state. */
* Preferences state.
*/
prefs: { prefs: {
type: Object, type: Object,
notify: true, notify: true,
......
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
'../compiled_resources2.gyp:route', '../compiled_resources2.gyp:route',
'../settings_page/compiled_resources2.gyp:main_page_behavior', '../settings_page/compiled_resources2.gyp:main_page_behavior',
'../settings_page/compiled_resources2.gyp:settings_page_visibility', '../settings_page/compiled_resources2.gyp:settings_page_visibility',
'../settings_page/compiled_resources2.gyp:transition_behavior',
], ],
'includes': ['../../../../../third_party/closure_compiler/compile_js2.gypi'], 'includes': ['../../../../../third_party/closure_compiler/compile_js2.gypi'],
}, },
......
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
'../compiled_resources2.gyp:route', '../compiled_resources2.gyp:route',
'settings_section', 'settings_section',
'transition_behavior', 'transition_behavior',
'<(EXTERNS_GYP):settings_private',
'<(EXTERNS_GYP):web_animations', '<(EXTERNS_GYP):web_animations',
'<(DEPTH)/ui/webui/resources/js/compiled_resources2.gyp:util', '<(DEPTH)/ui/webui/resources/js/compiled_resources2.gyp:util',
], ],
......
...@@ -20,10 +20,10 @@ function doWhenReady(readyTest, readyCallback) { ...@@ -20,10 +20,10 @@ function doWhenReady(readyTest, readyCallback) {
} }
/** /**
* Provides animations to expand and collapse individual sections in a page. * Responds to route changes by expanding, collapsing, or scrolling to sections
* Expanded sections take up the full height of the container. At most one * on the page. Expanded sections take up the full height of the container. At
* section should be expanded at any given time. * most one section should be expanded at any given time.
* @polymerBehavior Polymer.MainPageBehavior * @polymerBehavior MainPageBehavior
*/ */
var MainPageBehaviorImpl = { var MainPageBehaviorImpl = {
/** @type {?HTMLElement} The scrolling container. */ /** @type {?HTMLElement} The scrolling container. */
...@@ -38,23 +38,49 @@ var MainPageBehaviorImpl = { ...@@ -38,23 +38,49 @@ var MainPageBehaviorImpl = {
}, },
/** /**
* Hides or unhides the sections not being expanded. * @param {!settings.Route} newRoute
* @param {string} sectionName The section to keep visible. * @param {!settings.Route} oldRoute
* @param {boolean} hidden Whether the sections should be hidden.
* @private
*/ */
toggleOtherSectionsHidden_: function(sectionName, hidden) { currentRouteChanged: function(newRoute, oldRoute) {
var sections = Polymer.dom(this.root).querySelectorAll( var newRouteIsSubpage = newRoute && newRoute.subpage.length;
'settings-section'); var oldRouteIsSubpage = oldRoute && oldRoute.subpage.length;
for (var section of sections)
section.hidden = hidden && (section.section != sectionName); if (!oldRoute && newRouteIsSubpage) {
// Allow the page to load before expanding the section. TODO(michaelpg):
// Time this better when refactoring settings-animated-pages.
setTimeout(function() {
var section = this.getSection_(newRoute.section);
if (section)
this.expandSection_(section);
}.bind(this));
return;
}
if (newRouteIsSubpage) {
if (!oldRouteIsSubpage || newRoute.section != oldRoute.section) {
var section = this.getSection_(newRoute.section);
if (section)
this.expandSection_(section);
}
} else {
if (oldRouteIsSubpage) {
var section = this.getSection_(oldRoute.section);
if (section)
this.collapseSection_(section);
}
// Scrolls to the section if this main page contains the route's section.
if (newRoute && newRoute.section && this.getSection_(newRoute.section))
this.scrollToSection_();
}
}, },
/** /**
* Animates the card in |section|, expanding it to fill the page. * Animates the card in |section|, expanding it to fill the page.
* @param {!SettingsSectionElement} section * @param {!SettingsSectionElement} section
* @private
*/ */
expandSection: function(section) { expandSection_: function(section) {
// If another section's card is expanding, cancel that animation first. // If another section's card is expanding, cancel that animation first.
var expanding = this.$$('.expanding'); var expanding = this.$$('.expanding');
if (expanding) { if (expanding) {
...@@ -71,7 +97,7 @@ var MainPageBehaviorImpl = { ...@@ -71,7 +97,7 @@ var MainPageBehaviorImpl = {
// When it resolves, collapse that section's card before expanding // When it resolves, collapse that section's card before expanding
// this one. // this one.
setTimeout(function() { setTimeout(function() {
this.collapseSection( this.collapseSection_(
/** @type {!SettingsSectionElement} */(expanding)); /** @type {!SettingsSectionElement} */(expanding));
this.finishAnimation('section', function() { this.finishAnimation('section', function() {
this.startExpandSection_(section); this.startExpandSection_(section);
...@@ -128,11 +154,45 @@ var MainPageBehaviorImpl = { ...@@ -128,11 +154,45 @@ var MainPageBehaviorImpl = {
}.bind(this)); }.bind(this));
}, },
/**
* Expands the card in |section| to fill the page.
* @param {!SettingsSectionElement} section
* @return {!Promise}
* @private
*/
playExpandSection_: function(section) {
// We must be attached.
assert(this.scroller);
var promise;
var animationConfig = section.animateExpand(this.scroller);
if (animationConfig) {
promise = this.animateElement('section', animationConfig.card,
animationConfig.keyframes, animationConfig.options);
} else {
promise = Promise.resolve();
}
var finished;
promise.then(function() {
finished = true;
this.style.margin = 'auto';
}.bind(this), function() {
// The animation was canceled; catch the error and continue.
finished = false;
}).then(function() {
section.cleanUpAnimateExpand(finished);
});
return promise;
},
/** /**
* Animates the card in |section|, collapsing it back into its section. * Animates the card in |section|, collapsing it back into its section.
* @param {!SettingsSectionElement} section * @param {!SettingsSectionElement} section
* @private
*/ */
collapseSection: function(section) { collapseSection_: function(section) {
// If the section's card is still expanding, cancel the expand animation. // If the section's card is still expanding, cancel the expand animation.
if (section.classList.contains('expanding')) { if (section.classList.contains('expanding')) {
if (this.animations['section']) { if (this.animations['section']) {
...@@ -141,7 +201,7 @@ var MainPageBehaviorImpl = { ...@@ -141,7 +201,7 @@ var MainPageBehaviorImpl = {
// The animation must have finished but its promise hasn't finished // The animation must have finished but its promise hasn't finished
// resolving; try again asynchronously. // resolving; try again asynchronously.
this.async(function() { this.async(function() {
this.collapseSection(section); this.collapseSection_(section);
}); });
} }
return; return;
...@@ -166,39 +226,6 @@ var MainPageBehaviorImpl = { ...@@ -166,39 +226,6 @@ var MainPageBehaviorImpl = {
}.bind(this)); }.bind(this));
}, },
/**
* Expands the card in |section| to fill the page.
* @param {!SettingsSectionElement} section
* @return {!Promise}
* @private
*/
playExpandSection_: function(section) {
// We must be attached.
assert(this.scroller);
var promise;
var animationConfig = section.animateExpand(this.scroller);
if (animationConfig) {
promise = this.animateElement('section', animationConfig.card,
animationConfig.keyframes, animationConfig.options);
} else {
promise = Promise.resolve();
}
var finished;
promise.then(function() {
finished = true;
this.style.margin = 'auto';
}.bind(this), function() {
// The animation was canceled; catch the error and continue.
finished = false;
}).then(function() {
section.cleanUpAnimateExpand(finished);
});
return promise;
},
/** /**
* Collapses the card in |section| back to its normal position. * Collapses the card in |section| back to its normal position.
* @param {!SettingsSectionElement} section * @param {!SettingsSectionElement} section
...@@ -228,21 +255,7 @@ var MainPageBehaviorImpl = { ...@@ -228,21 +255,7 @@ var MainPageBehaviorImpl = {
}); });
return promise; return promise;
}, },
};
/** @polymerBehavior */
var MainPageBehavior = [
TransitionBehavior,
MainPageBehaviorImpl
];
/**
* TODO(michaelpg): integrate slide animations.
* @polymerBehavior RoutableBehavior
*/
var RoutableBehaviorImpl = {
/** @private */ /** @private */
scrollToSection_: function() { scrollToSection_: function() {
doWhenReady( doWhenReady(
...@@ -256,39 +269,17 @@ var RoutableBehaviorImpl = { ...@@ -256,39 +269,17 @@ var RoutableBehaviorImpl = {
}.bind(this)); }.bind(this));
}, },
/** @private */ /**
currentRouteChanged: function(newRoute, oldRoute) { * Hides or unhides the sections not being expanded.
var newRouteIsSubpage = newRoute && newRoute.subpage.length; * @param {string} sectionName The section to keep visible.
var oldRouteIsSubpage = oldRoute && oldRoute.subpage.length; * @param {boolean} hidden Whether the sections should be hidden.
* @private
if (!oldRoute && newRouteIsSubpage) { */
// Allow the page to load before expanding the section. TODO(michaelpg): toggleOtherSectionsHidden_: function(sectionName, hidden) {
// Time this better when refactoring settings-animated-pages. var sections = Polymer.dom(this.root).querySelectorAll(
setTimeout(function() { 'settings-section');
var section = this.getSection_(newRoute.section); for (var section of sections)
if (section) section.hidden = hidden && (section.section != sectionName);
this.expandSection(section);
}.bind(this));
return;
}
if (newRouteIsSubpage) {
if (!oldRouteIsSubpage || newRoute.section != oldRoute.section) {
var section = this.getSection_(newRoute.section);
if (section)
this.expandSection(section);
}
} else {
if (oldRouteIsSubpage) {
var section = this.getSection_(oldRoute.section);
if (section)
this.collapseSection(section);
}
// Scrolls to the section if this main page contains the route's section.
if (newRoute && newRoute.section && this.getSection_(newRoute.section))
this.scrollToSection_();
}
}, },
/** /**
...@@ -303,10 +294,9 @@ var RoutableBehaviorImpl = { ...@@ -303,10 +294,9 @@ var RoutableBehaviorImpl = {
}, },
}; };
/** @polymerBehavior */ /** @polymerBehavior */
var RoutableBehavior = [ var MainPageBehavior = [
MainPageBehavior,
settings.RouteObserverBehavior, settings.RouteObserverBehavior,
RoutableBehaviorImpl TransitionBehavior,
MainPageBehaviorImpl,
]; ];
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment