battery.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * This file is part of Threema Web.
  3. *
  4. * Threema Web is free software: you can redistribute it and/or modify it
  5. * under the terms of the GNU Affero General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or (at
  7. * your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Affero General Public License
  15. * along with Threema Web. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. import {BatteryStatusService} from '../services/battery';
  18. export default [
  19. '$rootScope',
  20. '$translate',
  21. 'BatteryStatusService',
  22. function($rootScope: ng.IRootScopeService, $translate: ng.translate.ITranslateService,
  23. batteryStatusService: BatteryStatusService) {
  24. return {
  25. restrict: 'E',
  26. scope: {},
  27. bindToController: {},
  28. controllerAs: 'ctrl',
  29. controller: [function() {
  30. this.available = () => batteryStatusService.dataAvailable;
  31. this.alert = () => batteryStatusService.isLow && !batteryStatusService.isCharging;
  32. this.percent = () => batteryStatusService.percent;
  33. this.description = (): string => {
  34. if (batteryStatusService.isCharging) {
  35. return $translate.instant('battery.CHARGING', {
  36. percent: this.percent(),
  37. });
  38. } else if (this.alert()) {
  39. return $translate.instant('battery.ALERT', {
  40. percent: this.percent(),
  41. });
  42. }
  43. return $translate.instant('battery.DISCHARGING', {
  44. percent: this.percent(),
  45. });
  46. };
  47. this.icon = (): string => {
  48. if (batteryStatusService.isCharging) {
  49. return 'battery_charging_full';
  50. } else if (this.alert()) {
  51. return 'battery_alert';
  52. }
  53. return 'battery_std';
  54. };
  55. }],
  56. template: `
  57. <div class="battery-status" ng-if="ctrl.available()"" ng-class="{'alert': ctrl.alert()}">
  58. <md-icon
  59. aria-label="Battery status: {{ ctrl.description() }}"
  60. title="{{ ctrl.description() }}"
  61. class="material-icons md-light md-24">{{ ctrl.icon() }}</md-icon>
  62. <span class="battery-percent" aria-hidden="true">{{ ctrl.percent() }}%</span>
  63. </div>
  64. `,
  65. };
  66. },
  67. ];