Controller.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Controller.js
  3. *
  4. * @author realor
  5. */
  6. import { Formula } from '../formula/Formula.js'
  7. class Controller {
  8. static classes = {}
  9. constructor(object, name) {
  10. this.application = null
  11. this.object = object
  12. this.name = name || 'controller'
  13. this._started = false
  14. this.autoStart = true
  15. }
  16. init(application) {
  17. this.application = application
  18. if (this.autoStart) this.start()
  19. }
  20. start() {
  21. if (!this._started) {
  22. this.onStart()
  23. this._started = true
  24. }
  25. }
  26. stop() {
  27. if (this._started) {
  28. this.onStop()
  29. this._started = false
  30. }
  31. }
  32. isStarted() {
  33. return this._started
  34. }
  35. onStart() {}
  36. onStop() {}
  37. hasChanged(event) {
  38. return this.updateFormulas() || (event.source !== this && event.objects.includes(this.object))
  39. }
  40. updateFormulas() {
  41. return Formula.update(this.object, 'controllers.' + this.name, true)
  42. }
  43. /* static methods */
  44. static addClass(controllerClass) {
  45. this.classes[controllerClass.name] = controllerClass
  46. }
  47. /* returns an array with the names of the controllers of the given class */
  48. static getTypesOf(controllerClass) {
  49. let types = []
  50. for (let className in this.classes) {
  51. let cls = this.classes[className]
  52. if (cls.prototype instanceof controllerClass || cls === controllerClass) {
  53. types.push(className)
  54. }
  55. }
  56. return types
  57. }
  58. static getDescription() {
  59. return 'controller.' + this.name
  60. }
  61. }
  62. export { Controller }