Skip to content

Commit

Permalink
docs(examples): refactor system to run on docsite
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanjones243 committed Apr 14, 2023
1 parent 284e046 commit 03fa145
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 190 deletions.
15 changes: 9 additions & 6 deletions apiExamples/dynamicMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,15 @@ function generateMenuOptionHtml(menu, label, value) {
menu.appendChild(option);
}

// Main function that runs all JS to create example
export async function populateCombobox(elem) {
const dynamicData = new DynamicData();
// Main javascript that runs all JS to create example
const dynamicData = new DynamicData();
const dynamicMenuExample = document.querySelector('#dynamicMenuExample');

let data = await dynamicData.getData();
data = dynamicData.filterData(data, elem.value);
const input = dynamicMenuExample.shadowRoot.querySelector('auro-dropdown').querySelector('auro-input');

input.addEventListener('input', () => {
let data = dynamicData.getData();
data = dynamicData.filterData(data, dynamicMenuExample.value);

generateHtml(data);
}
});
9 changes: 5 additions & 4 deletions apiExamples/focus.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export function focus(elem) {
// export function focus(elem) {
const focusExample = document.querySelector('#focusExample');
const focusExampleBtnElem = document.querySelector('#focusExampleBtn');

focusExampleBtnElem.addEventListener('click', () => {
elem.focus();
})
}
focusExample.focus();
});
// }
14 changes: 8 additions & 6 deletions apiExamples/persistent.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export function persistentEventOption(elem) {
elem.addEventListener('addNewAddress', () => {
console.warn('addNewAddress event fired');
alert(`addNewAddress event fired`);
});
}
const persistentExample = document.querySelector('#persistent');

console.log(persistentExample);

persistentExample.addEventListener('addNewAddress', () => {
console.warn('addNewAddress event fired');
alert(`addNewAddress event fired`);
});
24 changes: 13 additions & 11 deletions apiExamples/swapValue.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
export function swapComboboxValues(selectors) {
const btn = document.querySelector(selectors[0]);
const comboboxOne = document.querySelector(selectors[1]);
const comboboxTwo = document.querySelector(selectors[2]);
const btn = document.querySelector('#swapExampleBtn');
const comboboxOne = document.querySelector('#swapExampleLeft');
const comboboxTwo = document.querySelector('#swapExampleRight');

btn.addEventListener('click', () => {
const valueOne = comboboxOne.value;
const valueTwo = comboboxTwo.value;
console.log("hello");
console.log(btn);

comboboxOne.value = valueTwo;
comboboxTwo.value = valueOne;
});
}
btn.addEventListener('click', () => {
console.log("btn clicked");
const valueOne = comboboxOne.value;
const valueTwo = comboboxTwo.value;

comboboxOne.value = valueTwo;
comboboxTwo.value = valueOne;
});
23 changes: 11 additions & 12 deletions apiExamples/value.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
export function initValueExamples(elem) {
document.querySelector('#valueValidExampleBtn').addEventListener('click', () => {
elem.value = 'Oranges';
})
const valueExample = document.querySelector('#valueExample');

document.querySelector('#valueInvalidExampleBtn').addEventListener('click', () => {
console.warn('dragon fruit', elem);
elem.value = 'Dragon Fruit';
})
document.querySelector('#valueValidExampleBtn').addEventListener('click', () => {
valueExample.value = 'Oranges';
});

document.querySelector('#valueUndefinedExampleBtn').addEventListener('click', () => {
elem.value = undefined;
})
}
document.querySelector('#valueInvalidExampleBtn').addEventListener('click', () => {
valueExample.value = 'Dragon Fruit';
});

document.querySelector('#valueUndefinedExampleBtn').addEventListener('click', () => {
valueExample.value = undefined;
});
11 changes: 2 additions & 9 deletions demo/api.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,20 @@
<script type="module">
import 'https://unpkg.com/marked@latest/marked.min.js';
import 'https://unpkg.com/prismjs@latest/prism.js';
import { Examples } from './api.js';
fetch('/demo/api.md')
.then((response) => response.text())
.then((text) => {
const rawHtml = marked.parse(text);
document.querySelector('main').innerHTML = rawHtml;
Prism.highlightAll();
})
.then(() => {
const examples = new Examples();

examples.initialize();
});
</script>

<!-- If additional elements are needed for the demo, add them here. -->
<script src="https://unpkg.com/@alaskaairux/auro-accordion@latest/dist/auro-accordion__bundled.js" type="module"></script>
<script src="https://unpkg.com/@alaskaairux/auro-icon@latest/dist/auro-icon__bundled.js" type="module"></script>
<script src="https://unpkg.com/@aurodesignsystem/auro-dropdown@latest/dist/auro-dropdown__bundled.js" type="module"></script>
<script src="https://unpkg.com/@aurodesignsystem/auro-input@latest/dist/auro-input__bundled.js" type="module"></script>
<script src="https://unpkg.com/@alaskaairux/auro-button@latest/dist/auro-button__bundled.js" type="module"></script>
<script type="module" src="../src/auro-combobox.js"></script>
<script type="module" src="../index.js"></script>
<script type="module" src="./api.js"></script>
</body>
</html>
58 changes: 3 additions & 55 deletions demo/api.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,3 @@
import { populateCombobox } from '../apiExamples/dynamicMenu';
import { initValueExamples } from '../apiExamples/value';
import { focus } from './../apiExamples/focus';

class Examples {
initialize() {
function initializeExample(element, callback, retryCount) {
const elem = document.querySelector(element);

if (!elem || !elem.ready) {
// If the component is not ready, retry until it is
if (!retryCount && retryCount != 0) {
retryCount = 0;
} else {
retryCount += 1;
}

if (retryCount < 10) {
setTimeout(initializeExample, 500, element, callback, retryCount);
} else {
console.error('Unable to initialize functional example code for:', element);
}
} else {
callback(elem);
}
}

/**
* Value examples
*/
initializeExample('auro-combobox#valueExample', function(elem) {
initValueExamples(elem);
});

/**
* Apply focus to trigger using element.focus()
*/
initializeExample('#focusExample', function(elem) {
focus(elem);
});

/**
* Populate combobox via Dynamic API
*/
initializeExample('#dynamicMenuExample', function(elem) {
const input = elem.shadowRoot.querySelector('auro-dropdown').querySelector('auro-input');

input.addEventListener('input', () => {
populateCombobox(elem);
});
});
}
}

export { Examples }
import '../apiExamples/dynamicMenu';
import '../apiExamples/value';
import '../apiExamples/focus';
51 changes: 27 additions & 24 deletions demo/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,18 @@ function generateMenuOptionHtml(menu, label, value) {
menu.appendChild(option);
}

// Main function that runs all JS to create example
export async function populateCombobox(elem) {
const dynamicData = new DynamicData();
// Main javascript that runs all JS to create example
const dynamicData = new DynamicData();
const dynamicMenuExample = document.querySelector('#dynamicMenuExample');

let data = await dynamicData.getData();
data = dynamicData.filterData(data, elem.value);
const input = dynamicMenuExample.shadowRoot.querySelector('auro-dropdown').querySelector('auro-input');

input.addEventListener('input', () => {
let data = dynamicData.getData();
data = dynamicData.filterData(data, dynamicMenuExample.value);

generateHtml(data);
}
});
```

</auro-accordion>
Expand Down Expand Up @@ -411,20 +414,19 @@ Note: using a value that does not match a menu option will reset the combobox va
```

```js
export function initValueExamples(elem) {
document.querySelector('#valueValidExampleBtn').addEventListener('click', () => {
elem.value = 'Oranges';
})

document.querySelector('#valueInvalidExampleBtn').addEventListener('click', () => {
console.warn('dragon fruit', elem);
elem.value = 'Dragon Fruit';
})

document.querySelector('#valueUndefinedExampleBtn').addEventListener('click', () => {
elem.value = undefined;
})
}
const valueExample = document.querySelector('#valueExample');

document.querySelector('#valueValidExampleBtn').addEventListener('click', () => {
valueExample.value = 'Oranges';
});

document.querySelector('#valueInvalidExampleBtn').addEventListener('click', () => {
valueExample.value = 'Dragon Fruit';
});

document.querySelector('#valueUndefinedExampleBtn').addEventListener('click', () => {
valueExample.value = undefined;
});
```

</auro-accordion>
Expand Down Expand Up @@ -490,13 +492,14 @@ The focus method will apply focus state to the combobox input field.
<span slot="trigger">See code</span>

```js
export function focus(elem) {
// export function focus(elem) {
const focusExample = document.querySelector('#focusExample');
const focusExampleBtnElem = document.querySelector('#focusExampleBtn');

focusExampleBtnElem.addEventListener('click', () => {
elem.focus();
})
}
focusExample.focus();
});
// }
```

```html
Expand Down
9 changes: 2 additions & 7 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,20 @@
<script type="module">
import 'https://unpkg.com/marked@latest/marked.min.js';
import 'https://unpkg.com/prismjs@latest/prism.js';
import { Examples } from './index.js';
fetch('/index/index.md')
.then((response) => response.text())
.then((text) => {
const rawHtml = marked.parse(text);
document.querySelector('main').innerHTML = rawHtml;
Prism.highlightAll();
})
.then(() => {
const examples = new Examples();

examples.initialize();
});
</script>

<!-- If additional elements are needed for the demo, add them here. -->
<script src="https://unpkg.com/@alaskaairux/auro-accordion@latest/dist/auro-accordion__bundled.js" type="module"></script>
<script src="https://unpkg.com/@alaskaairux/auro-icon@latest/dist/auro-icon__bundled.js" type="module"></script>
<script src="https://unpkg.com/@alaskaairux/auro-button@latest/dist/auro-button__bundled.js" type="module"></script>
<script type="module" src="../src/auro-combobox.js"></script>
<script type="module" src="../index.js"></script>
<script type="module" src="./index.js"></script>
</body>
</html>
57 changes: 2 additions & 55 deletions demo/index.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,2 @@
import { persistentEventOption } from '../apiExamples/persistent';
import { swapComboboxValues } from '../apiExamples/swapValue';

class Examples {
initialize() {
function initializeExample(elements, callback, elementsPendingReady, retryCount) {
if (!elementsPendingReady) {
elementsPendingReady = elementsPendingReady || [];

if (typeof elements === 'string') {
elementsPendingReady.push(elements);
} else {
elementsPendingReady = elements;
}

initializeExample(elements, callback, elementsPendingReady);
} else {
let readyCount = 0;

elementsPendingReady.forEach(element => {
if (document.querySelector(element) && document.querySelector(element)['ready']) {
readyCount++;
}
});

retryCount = retryCount || 0;

if (elementsPendingReady.length != readyCount && retryCount < 10) {
retryCount = retryCount + 1;
setTimeout(initializeExample, 500, elements, callback, elementsPendingReady, retryCount);
} else {
callback(elements);
}
}
}

/**
* Persistent option with custom event
*/
initializeExample('#persistent', function(selector) {
persistentEventOption(document.querySelector(selector));
});

/**
* swap values example
*/
(function(){
initializeExample(['#swapExampleBtn', '#swapExampleLeft', '#swapExampleRight'], function(selectors) {
swapComboboxValues(selectors);
});
}());
}
}

export { Examples }
import '../apiExamples/persistent';
import '../apiExamples/swapValue';
2 changes: 1 addition & 1 deletion src/auro-combobox.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import styleCss from "./style-css.js";
*/

// build the component class
class AuroCombobox extends LitElement {
export class AuroCombobox extends LitElement {
constructor() {
super();

Expand Down

0 comments on commit 03fa145

Please sign in to comment.