From bc4e1ca94f6fcf4ee334effc7b1f8ecefdfc0f84 Mon Sep 17 00:00:00 2001 From: Margaret Dulat Date: Fri, 14 Dec 2018 15:52:28 -0800 Subject: [PATCH 01/11] Polymer 3 Conversion --- .eslintignore | 2 + .eslintrc.json | 2 +- .gitignore | 5 +- bower.json | 51 ----- build/default/index.html | 12 +- d2l-date-picker-behavior.html | 201 ----------------- d2l-date-picker-behavior.js | 202 ++++++++++++++++++ ...s.html => d2l-date-picker-style-modules.js | 25 +-- d2l-date-picker.html | 124 ----------- d2l-date-picker.js | 110 ++++++++++ demo/index.html | 61 ++++-- index.html | 16 -- localize-behavior.html | 85 -------- localize-behavior.js | 80 +++++++ package.json | 58 +++-- polymer.json | 3 +- test/.eslintrc.json | 2 +- test/d2l-date-picker_test.html | 182 ++++++++-------- test/index.html | 4 +- 19 files changed, 595 insertions(+), 630 deletions(-) create mode 100644 .eslintignore delete mode 100644 bower.json delete mode 100644 d2l-date-picker-behavior.html create mode 100644 d2l-date-picker-behavior.js rename d2l-date-picker-style-modules.html => d2l-date-picker-style-modules.js (81%) delete mode 100644 d2l-date-picker.html create mode 100644 d2l-date-picker.js delete mode 100644 index.html delete mode 100644 localize-behavior.html create mode 100644 localize-behavior.js diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..58d9402 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,2 @@ +test/acceptance/* +reports diff --git a/.eslintrc.json b/.eslintrc.json index 296730a..b96e7a6 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,5 +1,5 @@ { - "extends": "brightspace/polymer-config", + "extends": "brightspace/polymer-3-config", "globals": { "d2lIntl": true, "fastdom": false diff --git a/.gitignore b/.gitignore index 1308773..2402785 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ -bower_components -bower_components-1.x -bower-1.x.json node_modules package-lock.json + +node_modules diff --git a/bower.json b/bower.json deleted file mode 100644 index e57cc9c..0000000 --- a/bower.json +++ /dev/null @@ -1,51 +0,0 @@ -{ - "name": "d2l-date-picker", - "description": "A Date Picker for D2L Brightspace", - "main": "d2l-date-picker.html", - "license": "Apache-2.0", - "ignore": [ - "demo", - "test", - ".editorconfig", - ".eslintrc.json", - ".gitignore", - ".travis.yml", - "package.json", - "polymer.json", - "wct.conf.json" - ], - "dependencies": { - "d2l-localize-behavior": "^1.1.0", - "d2l-colors": "^3.1.2", - "d2l-fastdom-import": "Brightspace/fastdom-import#^1.0.0", - "d2l-offscreen": "^3.0.1", - "d2l-polymer-behaviors": "^1.0.0", - "d2l-time-picker": "BrightspaceUI/time-picker#^1.0.0", - "iron-a11y-keys": "^2.0.0", - "iron-input": "^2.0.0", - "polymer": "1.9 - 2", - "vaadin-date-picker": "Brightspace/vaadin-date-picker#1 - 2" - }, - "devDependencies": { - "iron-component-page": "PolymerElements/iron-component-page#^2.0.0", - "iron-demo-helpers": "PolymerElements/iron-demo-helpers#^2.0.0", - "iron-test-helpers": "^2.0.0", - "web-component-tester": "Polymer/web-component-tester#^6.0.0" - }, - "variants": { - "1.x": { - "dependencies": { - "polymer": "^1.9.1", - "vaadin-date-picker": "Brightspace/vaadin-date-picker#^1.2.8" - }, - "resolutions": { - "iron-a11y-keys-behavior": "1 - 2", - "iron-selector": "1 - 2", - "webcomponentsjs": "^0.7.24" - } - } - }, - "resolutions": { - "vaadin-date-picker": "^2.0.0" - } -} diff --git a/build/default/index.html b/build/default/index.html index 92d1323..c981719 100644 --- a/build/default/index.html +++ b/build/default/index.html @@ -1,15 +1,15 @@ - - + + - d2l-date-picker + d2l-date-picker - + - + - + \ No newline at end of file diff --git a/d2l-date-picker-behavior.html b/d2l-date-picker-behavior.html deleted file mode 100644 index 4c67d88..0000000 --- a/d2l-date-picker-behavior.html +++ /dev/null @@ -1,201 +0,0 @@ - - - - diff --git a/d2l-date-picker-behavior.js b/d2l-date-picker-behavior.js new file mode 100644 index 0000000..3d1ffc4 --- /dev/null +++ b/d2l-date-picker-behavior.js @@ -0,0 +1,202 @@ +import '@polymer/polymer/polymer-legacy.js'; +import 'fastdom/fastdom.js'; +// WORKAROUND: polymer-modulizer grabs non-existing Element export from polymer-element +// TODO: Remove Element reference +import { PolymerElement as Element } from '@polymer/polymer/polymer-element.js'; +import { useShadow } from '@polymer/polymer/lib/utils/settings.js'; +window.D2L = window.D2L || {}; +window.D2L.PolymerBehaviors = window.D2L.PolymerBehaviors || {}; +window.D2L.PolymerBehaviors.DatePicker = window.D2L.PolymerBehaviors.DatePicker || {}; + +/** @polymerBehavior D2L.PolymerBehaviors.DatePicker.DatePickerBehavior */ +D2L.PolymerBehaviors.DatePicker.DatePickerBehaviorImpl = { + properties: { + _target: { + type: Object + }, + _descriptionId: { + type: String + }, + placeholder: { + type: String + }, + min: { + type: String + }, + max: { + type: String + }, + label: { + type: String + }, + hideLabel: { + type: Boolean, + value: false, + reflectToAttribute: true + }, + description:{ + type: String + }, + locale: { + type: String, + value: 'en' + }, + firstDayOfWeek: { + type: Number, + value: 0 + }, + value: { + type: String, + reflectToAttribute: true, + notify: true + }, + customOverlayStyle: { + type: Boolean, + reflectToAttribute: true, + value: false + } + }, + + observers: [ + '_updateDatePickeri18n( locale, localize, firstDayOfWeek )' + ], + + ready: function() { + this._descriptionId = D2L.Id.getUniqueId(); + this._handleValueChanged = this._handleValueChanged.bind(this); + + if (this._isPolymer2()) { + this._setUpChangeEventListenerPolymer2(); + } + }, + + attached: function() { + var buttons = document.getElementsByClassName('vaadin-date-picker-overlay paper-button-0'); + + fastdom.mutate(function() { + for (var i = 0; i < buttons.length; i++) { + buttons[i].style.fontFamily = 'inherit'; + } + }); + }, + + detached: function() { + if (this._isPolymer2()) { + this.removeEventListener('value-changed', this._handleValueChanged); + } + this._changeListener = null; + }, + + _setUpChangeEventListenerPolymer2: function() { + // on-value-changed got removed for the Polymer2 version of vaadin-date-picker, + // so for Polymer2 we'll set our own event + this.addEventListener('value-changed', this._handleValueChanged); + }, + + _isPolymer2: function() { + return !!Element; + }, + + _handleValueChangedPolymer1: function(e) { + // using the `value-changed` listener sometimes sends 2 events in the + // Polymer 1 vaadin-date-picker when in shadyDom, so this function listens to + // on-value-changed from the vaadin-date-picker (removed in Polymer 2) + if (this._isPolymer2()) { + return; + } + this._handleValueChanged(e); + }, + + _handleValueChanged: function(e) { + this.dispatchEvent(new CustomEvent('d2l-date-picker-value-changed', { + detail: e.detail, + bubbles: true + })); + }, + + _handleTap: function() { + var datepicker = this.$$('vaadin-date-picker-light'); + if (!datepicker.opened) { + datepicker._focusOverlayOnOpen = true; + } + }, + + onEnter: function(e) { + var datepicker = this.$$('vaadin-date-picker-light'); + if (!datepicker.opened) { + // A better solution is to add a boolean to the 3rd party open function + datepicker._focusOverlayOnOpen = true; + datepicker.open(); + e.detail.keyboardEvent.preventDefault(); + e.detail.keyboardEvent.stopPropagation(); + } + + }, + + onUpDown: function(e) { + var datepicker = this.$$('vaadin-date-picker-light'); + if (!datepicker.opened) { + e.detail.keyboardEvent.preventDefault(); + e.detail.keyboardEvent.stopPropagation(); + } + }, + + _handleFocus: function() { + // in shady DOM the input's "focus" event does not bubble, + // so no need to fire it + if (!useShadow) { + this.dispatchEvent(new CustomEvent( + 'focus', + { bubbles: true, composed: false } + )); + } + }, + + _updateDatePickeri18n: function() { + var locale = d2lIntl.LocaleProvider(this.locale); + + var datePicker = this.$$('vaadin-date-picker-light'); + var localeOverrides = { + monthNames: locale.date.calendar.months.long, + weekdays: locale.date.calendar.days.long, + weekdaysShort: locale.date.calendar.days.short, + firstDayOfWeek: this.firstDayOfWeek ? this.firstDayOfWeek : locale.date.calendar.firstDayOfWeek, + today: this.localize('today'), + cancel: this.localize('cancel'), + formatDate: function(date) { + return this.formatDate(date); + }.bind(this), + parseDate: function(dateString) { + try { + return this.parseDate(dateString); + } catch (exception) { + return null; + } + }.bind(this) + }; + var datePickerLocale = this._merge(datePicker.i18n, localeOverrides); + datePicker.i18n = {}; // reassign to have Polymer refresh + datePicker.i18n = datePickerLocale; + }, + + _merge: function(obj1, obj2) { + if (obj2 === undefined || obj2 === null || typeof(obj2) !== 'object') { + return obj1; + } + Object.keys(obj2).forEach(function(i) { + if (obj1.hasOwnProperty(i)) { + if (typeof(obj2[i]) === 'object' && typeof(obj1[i]) === 'object') { + this._merge(obj1[i], obj2[i]); + } else { + obj1[i] = obj2[i]; + } + } + }.bind(this)); + return obj1; + } +}; + +/** @polymerBehavior D2L.PolymerBehaviors.DatePicker.DatePickerBehavior */ +D2L.PolymerBehaviors.DatePicker.DatePickerBehavior = [ + D2L.PolymerBehaviors.DatePicker.DatePickerBehaviorImpl +]; diff --git a/d2l-date-picker-style-modules.html b/d2l-date-picker-style-modules.js similarity index 81% rename from d2l-date-picker-style-modules.html rename to d2l-date-picker-style-modules.js index b8df081..80441cc 100644 --- a/d2l-date-picker-style-modules.html +++ b/d2l-date-picker-style-modules.js @@ -1,8 +1,9 @@ - - - +import '@polymer/polymer/polymer-legacy.js'; +import 'd2l-colors/d2l-colors.js'; +import 'vaadin-date-picker/vaadin-date-picker-styles.js'; +const $_documentContainer = document.createElement('template'); - +$_documentContainer.innerHTML = ` - - - + - - - + - - - + - +`; + +document.head.appendChild($_documentContainer.content); diff --git a/d2l-date-picker.html b/d2l-date-picker.html deleted file mode 100644 index eaf213d..0000000 --- a/d2l-date-picker.html +++ /dev/null @@ -1,124 +0,0 @@ - - - - - - - - - - - - - - - - - - - diff --git a/d2l-date-picker.js b/d2l-date-picker.js new file mode 100644 index 0000000..513801a --- /dev/null +++ b/d2l-date-picker.js @@ -0,0 +1,110 @@ +/** +`d2l-date-picker` +A Date Picker for D2L Brightspace + +@demo demo/index.html +*/ +/* + FIXME(polymer-modulizer): the above comments were extracted + from HTML and may be out of place here. Review them and + then delete this comment! +*/ +import '@polymer/polymer/polymer-legacy.js'; + +import 'vaadin-date-picker/vaadin-date-picker-light.js'; +import '@polymer/iron-input/iron-input.js'; +import '@polymer/iron-a11y-keys/iron-a11y-keys.js'; +import 'd2l-colors/d2l-colors.js'; +import 'd2l-offscreen/d2l-offscreen-shared-styles.js'; +import 'd2l-polymer-behaviors/d2l-id.js'; +import 'd2l-time-picker/d2l-input-styles.js'; +import 'fastdom/fastdom.js'; +import './localize-behavior.js'; +import './d2l-date-picker-behavior.js'; +import './d2l-date-picker-style-modules.js'; +import { Polymer } from '@polymer/polymer/lib/legacy/polymer-fn.js'; +import { html } from '@polymer/polymer/lib/utils/html-tag.js'; +Polymer({ + _template: html` + + + + + + + + + + +
{{description}}
+
+
+
+`, + + is: 'd2l-date-picker', + + behaviors: [ + D2L.PolymerBehaviors.DatePicker.DatePickerBehavior, + D2L.PolymerBehaviors.DatePicker.LocalizeBehavior + ], + + properties: { + invalid: { + type: Boolean, + value: false, + reflectToAttribute: true + } + }, + + ready: function() { + var input = this.$$('input'); + if (input) { + this._target = this.$$('input'); + } + }, + + focus: function() { + var input = this.$$('input'); + if (input) { + fastdom.mutate(function() { + input.focus(); + }); + } + }, + + _computedAriaInvalid: function(invalid) { + return invalid ? 'true' : 'false'; + } +}); diff --git a/demo/index.html b/demo/index.html index 7daeda2..b7a87d7 100644 --- a/demo/index.html +++ b/demo/index.html @@ -1,23 +1,40 @@ - - + + - d2l-date-picker demo + d2l-date-picker demo - + - - - + + + - - - + + - + - -
+ </script> + `; + +document.body.appendChild($_documentContainer.content); + type="module"> +const $_documentContainer = document.createElement('template'); + +$_documentContainer.innerHTML = `

Basic d2l-date-picker demo

-
+
`; + +document.body.appendChild($_documentContainer.content); + diff --git a/index.html b/index.html deleted file mode 100644 index 2e332f5..0000000 --- a/index.html +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - d2l-date-picker - - - - - - - - - diff --git a/localize-behavior.html b/localize-behavior.html deleted file mode 100644 index 8ea924e..0000000 --- a/localize-behavior.html +++ /dev/null @@ -1,85 +0,0 @@ - - - diff --git a/localize-behavior.js b/localize-behavior.js new file mode 100644 index 0000000..a46c78f --- /dev/null +++ b/localize-behavior.js @@ -0,0 +1,80 @@ +import '@polymer/polymer/polymer-legacy.js'; +import 'd2l-localize-behavior/d2l-localize-behavior.js'; + +window.D2L = window.D2L || {}; +window.D2L.PolymerBehaviors = window.D2L.PolymerBehaviors || {}; +window.D2L.PolymerBehaviors.DatePicker = window.D2L.PolymerBehaviors.DatePicker || {}; + +/** @polymerBehavior D2L.PolymerBehaviors.DatePicker.LocalizeBehavior */ +D2L.PolymerBehaviors.DatePicker.LocalizeBehaviorImpl = { + properties: { + resources: { + value: function() { + return { + 'ar': { + today: 'اليوم', + cancel: 'إلغاء' + }, + 'da': { + today: 'i dag', + cancel: 'annuller' + }, + 'en': { + today: 'today', + cancel: 'cancel' + }, + 'es': { + today: 'hoy', + cancel: 'cancelar' + }, + 'fi': { + today: 'tänään', + cancel: 'peruuta' + }, + 'fr': { + today: 'aujourd\'hui', + cancel: 'Annuler' + }, + 'ja': { + today: '今日', + cancel: 'キャンセル' + }, + 'ko': { + today: '오늘', + cancel: '취소' + }, + 'nl': { + today: 'vandaag', + cancel: 'Annuleer' + }, + 'pt': { + today: 'hoje', + cancel: 'cancelar' + }, + 'sv': { + today: 'i dag', + cancel: 'annullera' + }, + 'tr': { + today: 'bugün', + cancel: 'iptal etmek' + }, + 'zh': { + today: '今天', + cancel: '取消' + }, + 'zh-tw': { + today: '今天', + cancel: '取消' + } + }; + } + } + } +}; + +/** @polymerBehavior D2L.PolymerBehaviors.DatePicker.LocalizeBehavior */ +D2L.PolymerBehaviors.DatePicker.LocalizeBehavior = [ + D2L.PolymerBehaviors.LocalizeBehavior, + D2L.PolymerBehaviors.DatePicker.LocalizeBehaviorImpl +]; diff --git a/package.json b/package.json index ea34680..d213621 100644 --- a/package.json +++ b/package.json @@ -1,36 +1,58 @@ { - "name": "d2l-date-picker", "description": "Polymer-based web component for D2L date picker", - "private": true, - "main": "d2l-date-picker.html", - "scripts": { - "postinstall": "polymer install --variants", - "lint": "npm run lint:wc && npm run lint:html", - "lint:html": "eslint *.html demo/*.html test/*.html", - "lint:wc": "polymer lint -i d2l-date-picker.html", - "test": "npm run lint && npm run test:polymer:local", - "test:polymer:local": "polymer test --skip-plugin sauce", - "test:polymer:sauce": "polymer test --skip-plugin local" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/BrightspaceUI/date-picker.git" - }, "keywords": [ "D2L", "date", "picker" ], + "repository": { + "type": "git", + "url": "git+https://github.com/BrightspaceUI/date-picker.git" + }, + "homepage": "https://github.com/BrightspaceUI/date-picker#readme", + "name": "d2l-date-picker", + "main": "d2l-date-picker.js", + "scripts": { + "lint": "npm run lint:wc && npm run lint:js", + "lint:js": "eslint . --ext .js,.html test/**/*.js test/**/*.html demo/**/*.js demo/**/*.html", + "lint:wc": "polymer lint", + "test": "npm run lint && npm run test:polymer:local", + "test:polymer:local": "polymer test --skip-plugin sauce", + "test:polymer:sauce": "polymer test --skip-plugin local" + }, "author": "D2L Corporation", "license": "Apache-2.0", "bugs": { "url": "https://github.com/BrightspaceUI/date-picker/issues" }, - "homepage": "https://github.com/BrightspaceUI/date-picker#readme", "devDependencies": { + "@babel/polyfill": "^7.0.0", + "@polymer/iron-component-page": "^3.0.0-pre.18", + "@polymer/iron-demo-helpers": "^3.0.0-pre.18", + "@polymer/iron-test-helpers": "^3.0.0-pre.18", + "@webcomponents/webcomponentsjs": "^2.2.1", + "babel-eslint": "^10.0.1", "eslint": "^4.15.0", "eslint-config-brightspace": "^0.4.0", "eslint-plugin-html": "^4.0.1", - "polymer-cli": "^1.7.7" + "polymer-cli": "^1.9.2", + "wct-browser-legacy": "^1.0.1" + }, + "version": "", + "resolutions": { + "inherits": "2.0.3", + "samsam": "1.1.3", + "supports-color": "3.1.2", + "type-detect": "1.0.0" + }, + "dependencies": { + "d2l-localize-behavior": "BrightspaceUI/localize-behavior#semver:^2", + "d2l-colors": "BrightspaceUI/colors#semver:^4", + "fastdom": "^1.0.8", + "d2l-offscreen": "BrightspaceUI/offscreen#semver:^4", + "d2l-polymer-behaviors": "Brightspace/d2l-polymer-behaviors-ui#semver:^2", + "@polymer/iron-a11y-keys": "^3.0.0-pre.18", + "@polymer/iron-input": "^3.0.0-pre.18", + "@polymer/polymer": "^3.0.0" } } diff --git a/polymer.json b/polymer.json index 3d0892e..6111e49 100644 --- a/polymer.json +++ b/polymer.json @@ -1,7 +1,8 @@ { + "npm": true, "lint": { "rules": [ - "polymer-2-hybrid" + "polymer-3" ] } } diff --git a/test/.eslintrc.json b/test/.eslintrc.json index ae5320e..3dacc9b 100644 --- a/test/.eslintrc.json +++ b/test/.eslintrc.json @@ -1,3 +1,3 @@ { - "extends": "brightspace/wct-config" + "extends": "brightspace/wct-polymer-3-config" } diff --git a/test/d2l-date-picker_test.html b/test/d2l-date-picker_test.html index faeda84..9953ebf 100644 --- a/test/d2l-date-picker_test.html +++ b/test/d2l-date-picker_test.html @@ -6,11 +6,12 @@ d2l-date-picker test - - + + + - - + + @@ -20,100 +21,103 @@ - + MockInteractions.focus(elem.$$('input')); + }); + }); +}); + diff --git a/test/index.html b/test/index.html index 106f5f9..6001878 100644 --- a/test/index.html +++ b/test/index.html @@ -2,8 +2,8 @@ - - + + + - - + + + type="module"> -const $_documentContainer = document.createElement('template'); - -$_documentContainer.innerHTML = `
-

Basic d2l-date-picker demo

- - - - -

d2l-date-picker with slotted content

- - - -
`; - -document.body.appendChild($_documentContainer.content); - + const $_documentContainer = document.createElement('template'); + + $_documentContainer.innerHTML = `
+

Basic d2l-date-picker demo

+ + + + +

d2l-date-picker with slotted content

+ + + +
`; + + document.body.appendChild($_documentContainer.content); + diff --git a/package.json b/package.json index d213621..6041d36 100644 --- a/package.json +++ b/package.json @@ -26,15 +26,16 @@ "url": "https://github.com/BrightspaceUI/date-picker/issues" }, "devDependencies": { - "@babel/polyfill": "^7.0.0", "@polymer/iron-component-page": "^3.0.0-pre.18", "@polymer/iron-demo-helpers": "^3.0.0-pre.18", "@polymer/iron-test-helpers": "^3.0.0-pre.18", "@webcomponents/webcomponentsjs": "^2.2.1", + "async": "^1.5.2", "babel-eslint": "^10.0.1", "eslint": "^4.15.0", "eslint-config-brightspace": "^0.4.0", "eslint-plugin-html": "^4.0.1", + "lodash": "^3.10.1", "polymer-cli": "^1.9.2", "wct-browser-legacy": "^1.0.1" }, @@ -46,13 +47,16 @@ "type-detect": "1.0.0" }, "dependencies": { - "d2l-localize-behavior": "BrightspaceUI/localize-behavior#semver:^2", + "@polymer/iron-a11y-keys": "^3.0.0-pre.18", + "@polymer/iron-dropdown": "^3.0.1", + "@polymer/iron-input": "^3.0.0-pre.18", + "@polymer/polymer": "^3.0.0", + "@vaadin/vaadin-date-picker": "^3.3.2", "d2l-colors": "BrightspaceUI/colors#semver:^4", - "fastdom": "^1.0.8", + "d2l-localize-behavior": "BrightspaceUI/localize-behavior#semver:^2", "d2l-offscreen": "BrightspaceUI/offscreen#semver:^4", "d2l-polymer-behaviors": "Brightspace/d2l-polymer-behaviors-ui#semver:^2", - "@polymer/iron-a11y-keys": "^3.0.0-pre.18", - "@polymer/iron-input": "^3.0.0-pre.18", - "@polymer/polymer": "^3.0.0" + "d2l-time-picker": "BrightspaceUI/time-picker#semver:^2", + "fastdom": "^1.0.8" } } diff --git a/test/d2l-date-picker_test.html b/test/d2l-date-picker_test.html index 9953ebf..f655bac 100644 --- a/test/d2l-date-picker_test.html +++ b/test/d2l-date-picker_test.html @@ -6,7 +6,6 @@ d2l-date-picker test - @@ -16,7 +15,7 @@ -