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

fix(all): remove all direct use of browser globals #10826

Closed
wants to merge 11 commits into from

Conversation

graingert
Copy link
Contributor

@graingert graingert commented Aug 1, 2017

use injectable $window and $document instead.

  • This removes the private provider $$mdMetaProvider because it used to access document before it was available. Meta headers can only be changed via the $$mdMeta service.
  • This updates and deprecates $mdThemingProvider.enableBrowserColor() to queue up color changes and dispatch them in the actual service when $$mdMeta is available.
  • This adds support for defining panel presets with an injectable function for the case where you want to
    define a preset that sets attachTo: angular.element($document[0].body) inside a config function.
  • This replaces all alert() calls with $mdDialog.show($mdDialog.alert()...); calls

This depends on eslint to find the misuse of global variables: #10824

What are $window and $document?

According to the AngularJS documentation: https://docs.angularjs.org/api/ng/service/$window

A reference to the browser's window object. While window is globally available in JavaScript, it causes testability problems, because it is a global variable. In AngularJS we always refer to it through the $window service, so it may be overridden, removed or mocked for testing.

$document is similar

Why is this needed?

  • Before $window, window and document, $document were being used interchangeably, this PR ensures consistency.
  • using an injectable $window and $document allows ...

@googlebot googlebot added the cla: yes PR author has signed Google's CLA: https://opensource.google.com/docs/cla/ label Aug 1, 2017
@graingert graingert force-pushed the no-browser-globals branch 4 times, most recently from da46b1f to 0857fdb Compare August 2, 2017 00:47
@graingert graingert changed the title WIP: remove all direct use of browser globals remove all direct use of browser globals Aug 2, 2017
@graingert graingert changed the title remove all direct use of browser globals chore(all): remove all direct use of browser globals Aug 2, 2017
@graingert graingert changed the title chore(all): remove all direct use of browser globals fix(all): remove all direct use of browser globals Aug 2, 2017
@graingert graingert force-pushed the no-browser-globals branch 3 times, most recently from c1939ec to 2d0bfea Compare August 2, 2017 01:23
@graingert graingert force-pushed the no-browser-globals branch 3 times, most recently from 1f158f9 to dbe7650 Compare August 4, 2017 12:45
@graingert
Copy link
Contributor Author

@jelbourn @tinayuangao I rebased this on master just now.

@graingert
Copy link
Contributor Author

@mgol in reply to your comment #10824 (comment) this builds on #10824 if you merge that, those unrelated commits will disappear

@graingert
Copy link
Contributor Author

There should be one PR for ESLint setup, then another for any $window-related changes etc.

That is indeed how I've split the PRs.

@graingert
Copy link
Contributor Author

@ThomasBurleson @EladBezalel any chance of a review here?

@Frank3K
Copy link
Contributor

Frank3K commented Aug 23, 2017

@graingert: it looks like there are lots of other commits in this PR. Can you fix this?

@graingert
Copy link
Contributor Author

@Frank3K those are just the extra commits from #10824 when that's merged the commits will disappear from here

@Frank3K
Copy link
Contributor

Frank3K commented Aug 24, 2017

@graingert: I know, but I think the PR's will not get pulled before they are cleaned up...

@graingert
Copy link
Contributor Author

@Frank3K these PRs are clean. They just depend on each other.

@ThomasBurleson
Copy link
Contributor

@graingert - This PR will not be accepted because the scope of the changes are too broad.
Please resubmit with small, granular PRs and we will gladly review those for inclusion in 1.1.6.

@graingert
Copy link
Contributor Author

@ThomasBurleson can you have a look at #10824 when that's merged it will automatically unbroarden this pr

Thomas Grainger and others added 7 commits August 20, 2018 09:15
relay on per application state.
Angular code should not use global state, it should be managed by
providers.

expose isDisabled() etc on $mdTheming
remove theme disabling logic from md-themes-disabled directive
  (it has no effect after generateAllThemes is run, because that's the
   only location that reads themeConfig.disableTheming)
check $document for md-themes-disabled in generateAllThemes.
@graingert
Copy link
Contributor Author

@Splaktar well I've rebased it

@graingert
Copy link
Contributor Author

@Splaktar ok so all the tests pass

@Splaktar Splaktar added needs: squash commits and removed needs: rebase This PR needs to be rebased on the latest commits from master and conflicts need to be resolved labels Sep 4, 2018
Copy link
Member

@Splaktar Splaktar left a comment

Choose a reason for hiding this comment

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

I made it about halfway through reviewing this. Many of the changes are harmless, but there are a number of cases where issues pop up.

In those cases, I observed exceptions in the demos that need to be resolved.

Please address the issues that I called out, manually re-test all demos that had either a demo change or component change, and then let me know and I'll review again.

@@ -18,7 +18,7 @@
</md-item-template>
<md-not-found>
No states matching "{{ctrl.searchText}}" were found.
<a ng-click="ctrl.newState(ctrl.searchText)">Create a new one!</a>
<md-button ng-click="ctrl.newState(ctrl.searchText, $event)">Create a new one!</md-button>
Copy link
Member

Choose a reason for hiding this comment

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

The new behavior here is quite buggy.
screen shot 2018-09-04 at 12 34 07 pm

Let's completely remove the concept of a link or button in the md-not-found for this demo.

$mdDialog.show(
$mdDialog
.alert()
.title('state creation failed.')
Copy link
Member

Choose a reason for hiding this comment

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

Let's completely remove the concept of a link or button in the md-not-found, and thus this newState() function for this demo.

}

return function($document, $q, $window) {
if (prom) {
Copy link
Member

Choose a reason for hiding this comment

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

What does prom stand for? Perhaps this can be renamed to be more clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

promise

var self = this;
var pendingSearch, cancelSearch = angular.noop;
var lastSearch;

self.allContacts = loadContacts();
loader($document, $q, $window).then(function (CryptoJS) {
Copy link
Member

Choose a reason for hiding this comment

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

This throws:

TypeError: Cannot read property 'then' of undefined
    at DemoCtrl (docs-demo-scripts.js:589)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Urgh, probably a missing return

@@ -400,7 +403,7 @@ function ConfigurationItem(url, viewBoxSize) {
*/

/* @ngInject */
function MdIconService(config, $templateRequest, $q, $log, $mdUtil, $sce) {
function mdIconService(config, $templateRequest, $q, $log, $mdUtil, $sce, $window) {
Copy link
Member

Choose a reason for hiding this comment

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

Why did the casing of the name need to change here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Probably a rebase error

this.selected.favoriteDessert = dessert;
this._mdPanelRef && this._mdPanelRef.close().then(function() {
angular.element(document.querySelector('.demo-menu-open-button')).focus();
angular.element($document[0].querySelector('.demo-menu-open-button')).focus();
Copy link
Member

Choose a reason for hiding this comment

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

This throws

TypeError: Cannot read property '0' of undefined
    at docs-demo-scripts.js:2220

@@ -6,7 +6,7 @@ angular.module('panelAnimationsDemo', ['ngMaterial'])
.controller('DialogCtrl', DialogCtrl);


function AnimationCtrl($mdPanel) {
function AnimationCtrl($mdPanel, $document) {
this._mdPanel = $mdPanel;
this.openFrom = 'button';
Copy link
Member

Choose a reason for hiding this comment

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

Opening the dialog throws

TypeError: Cannot read property '0' of undefined
    at AnimationCtrl.showDialog (docs-demo-scripts.js:2440)

@@ -1204,12 +1204,19 @@ MdPanelService.prototype.open = function(preset, config) {
* @returns {!Object} The preset configuration object.
*/
MdPanelService.prototype._getPresetByName = function(preset) {
if (!this._presets[preset]) {
var $injector = this._$injector;
var v = this._presets[preset];
Copy link
Member

Choose a reason for hiding this comment

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

What does v stand for? Can you please give this a more meaningful name?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's an arbitrary value because I didn't know what it was for.

Maybe the preset -> presetName and v -> preset?

Copy link
Member

Choose a reason for hiding this comment

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

That sounds good. It is much more clear.

.alert()
.title('Action')
.textContent('submit')
.event($event)
Copy link
Member

Choose a reason for hiding this comment

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

This throws

TypeError: $mdDialog.alert(...).title(...).textContent(...).event is not a function
    at m.$scope.submit (docs-demo-scripts.js:2746)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm probably not understanding how $mdDialog works here, would be good for an angular material dev to take a look, please?

Copy link
Member

@Splaktar Splaktar Sep 4, 2018

Choose a reason for hiding this comment

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

It's .targetEvent($event)

* Change the selected date in the calendar (ngModel value has already been changed).
* @param {Date} date
*/
CalendarMonthCtrl.prototype.changeSelectedDate = function(date) {
Copy link
Member

Choose a reason for hiding this comment

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

Where did this come from?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I probably resurrected this by accident in a rebase

@Splaktar Splaktar added the in progress Mainly for in progress PRs, but may be used for issues that require multiple PRs label Sep 4, 2018
@graingert
Copy link
Contributor Author

The $document[0] not working is really worrying. This should be injected by angularjs as a jqlite element of document

@graingert
Copy link
Contributor Author

@Splaktar can you take over this PR and make those changes? I'm not using AngularJS anymore so don't have much time to work on this

@Splaktar
Copy link
Member

Splaktar commented Sep 4, 2018

OK, I'll look into creating a new PR, which I'll need you to post in to grant approval to use your commits. I probably won't have time to do this real soon.

Thank you for getting a start here though.

@Splaktar Splaktar modified the milestones: 1.1.11, - Backlog Sep 4, 2018
@graingert
Copy link
Contributor Author

@Splaktar I've enabled edits to this PR from maintainers, so you should be able to push to this branch

@Splaktar Splaktar modified the milestones: - Backlog, 1.2.0 Jul 6, 2020
@Splaktar Splaktar modified the milestones: 1.2.0, - Backlog Jul 28, 2020
@Splaktar Splaktar added the resolution: too risky There is too much risk or disruption with this proposal for our limited resources. label Dec 6, 2020
@Splaktar Splaktar removed this from the - Backlog milestone Dec 6, 2020
@Splaktar
Copy link
Member

Splaktar commented Dec 6, 2020

Thank you for all of the work on this effort. Unfortunately, this has been determined to be too risky to try to get merged at this point.

@Splaktar Splaktar closed this Dec 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes PR author has signed Google's CLA: https://opensource.google.com/docs/cla/ 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 P5: nice to have These issues will not be fixed without community contributions. resolution: too risky There is too much risk or disruption with this proposal for our limited resources. type: chore
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants