Skip to content

Instantly share code, notes, and snippets.

@mastastealth
Last active July 9, 2021 13:27
Show Gist options
  • Save mastastealth/318853114e2b38ab53a031c7d950f970 to your computer and use it in GitHub Desktop.
Save mastastealth/318853114e2b38ab53a031c7d950f970 to your computer and use it in GitHub Desktop.
Select All Shenanigans
import Component from "@glimmer/component";
import { action } from "@ember/object";
import { tracked } from "@glimmer/tracking";
export default class Test extends Component {
@tracked selectedFiles = [];
files = ["a", "b", "c"];
@action
handleFileToggle(file) {
console.log(`Toggling file ${file}`);
if (this.selectedFiles.includes(file)) {
this.selectedFiles = this.selectedFiles.filter((f) => f !== file);
} else {
this.selectedFiles = [...this.selectedFiles, file];
}
}
get areAllFilesSelected() {
console.log("Running areAllFilesSelected getter");
return this.selectedFiles.length === this.files.length;
}
get areSomeButNotAllFilesSelected() {
console.log("Running areSomeButNotAllFilesSelected fn");
return (
this.selectedFiles.length > 0 &&
this.selectedFiles.length !== this.files.length
);
}
@action
handleSelectAllChange(e) {
console.log("handleSelectAllChange", e);
e.preventDefault();
if (!e.target.checked) {
this.selectedFiles = [];
} else {
this.selectedFiles = [...this.files];
}
}
}
import Controller from '@ember/controller';
export default class ApplicationController extends Controller {
appName = 'Ember Twiddle';
}
import { helper } from "@ember/component/helper";
export default helper(function ([needle, haystack]) {
return needle.includes(haystack);
});
<h1>Welcome to {{this.appName}}</h1>
<br>
<br>
<Test />
<br>
<br>
<label style="display: block;">
Toggle ALL
<Input
@type="checkbox"
@checked={{this.areAllFilesSelected}}
@indeterminate={{this.areSomeButNotAllFilesSelected}}
{{on "change" this.handleSelectAllChange}}
/>
</label>
<hr />
{{#each this.files as |file|}}
<label style="display: block;">
Toggle {{file}}
<Input
@type="checkbox"
@checked={{includes this.selectedFiles file}}
{{on "click" (fn this.handleFileToggle file)}}
/>
</label>
{{/each}}
{
"version": "0.17.1",
"EmberENV": {
"FEATURES": {},
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": false,
"_APPLICATION_TEMPLATE_WRAPPER": true,
"_JQUERY_INTEGRATION": true
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js",
"ember": "3.18.1",
"ember-template-compiler": "3.18.1",
"ember-testing": "3.18.1"
},
"addons": {
"@glimmer/component": "1.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment