SettingsController.coffee 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. define [
  2. "base"
  3. ], (App) ->
  4. App.controller "SettingsController", ["$scope", "settings", "ide", "_", ($scope, settings, ide, _) ->
  5. $scope.overallThemesList = window.overallThemes
  6. $scope.ui =
  7. loadingStyleSheet: false
  8. _updateCSSFile = (theme) ->
  9. $scope.ui.loadingStyleSheet = true
  10. docHeadEl = document.querySelector "head"
  11. oldStyleSheetEl = document.getElementById "main-stylesheet"
  12. newStyleSheetEl = document.createElement "link"
  13. newStyleSheetEl.addEventListener "load", (e) =>
  14. $scope.$applyAsync () =>
  15. $scope.ui.loadingStyleSheet = false
  16. docHeadEl.removeChild oldStyleSheetEl
  17. newStyleSheetEl.setAttribute "rel", "stylesheet"
  18. newStyleSheetEl.setAttribute "id", "main-stylesheet"
  19. newStyleSheetEl.setAttribute "href", theme.path
  20. docHeadEl.appendChild newStyleSheetEl
  21. if $scope.settings.mode not in ["default", "vim", "emacs"]
  22. $scope.settings.mode = "default"
  23. if $scope.settings.pdfViewer not in ["pdfjs", "native"]
  24. $scope.settings.pdfViewer = "pdfjs"
  25. if $scope.settings.fontFamily? and $scope.settings.fontFamily not in ["monaco", "lucida"]
  26. delete $scope.settings.fontFamily
  27. if $scope.settings.lineHeight? and $scope.settings.lineHeight not in ["compact", "normal", "wide"]
  28. delete $scope.settings.lineHeight
  29. $scope.fontSizeAsStr = (newVal) ->
  30. if newVal?
  31. $scope.settings.fontSize = newVal
  32. return $scope.settings.fontSize.toString()
  33. $scope.$watch "settings.editorTheme", (editorTheme, oldEditorTheme) =>
  34. if editorTheme != oldEditorTheme
  35. settings.saveSettings({editorTheme})
  36. $scope.$watch "settings.overallTheme", (overallTheme, oldOverallTheme) =>
  37. if overallTheme != oldOverallTheme
  38. chosenTheme = _.find $scope.overallThemesList, (theme) -> theme.val == overallTheme
  39. if chosenTheme?
  40. _updateCSSFile chosenTheme
  41. settings.saveSettings({overallTheme})
  42. $scope.$watch "settings.fontSize", (fontSize, oldFontSize) =>
  43. if fontSize != oldFontSize
  44. settings.saveSettings({fontSize: parseInt(fontSize, 10)})
  45. $scope.$watch "settings.mode", (mode, oldMode) =>
  46. if mode != oldMode
  47. settings.saveSettings({mode: mode})
  48. $scope.$watch "settings.autoComplete", (autoComplete, oldAutoComplete) =>
  49. if autoComplete != oldAutoComplete
  50. settings.saveSettings({autoComplete: autoComplete})
  51. $scope.$watch "settings.autoPairDelimiters", (autoPairDelimiters, oldAutoPairDelimiters) =>
  52. if autoPairDelimiters != oldAutoPairDelimiters
  53. settings.saveSettings({autoPairDelimiters: autoPairDelimiters})
  54. $scope.$watch "settings.pdfViewer", (pdfViewer, oldPdfViewer) =>
  55. if pdfViewer != oldPdfViewer
  56. settings.saveSettings({pdfViewer: pdfViewer})
  57. $scope.$watch "settings.syntaxValidation", (syntaxValidation, oldSyntaxValidation) =>
  58. if syntaxValidation != oldSyntaxValidation
  59. settings.saveSettings({syntaxValidation: syntaxValidation})
  60. $scope.$watch "settings.fontFamily", (fontFamily, oldFontFamily) =>
  61. if fontFamily != oldFontFamily
  62. settings.saveSettings({fontFamily: fontFamily})
  63. $scope.$watch "settings.lineHeight", (lineHeight, oldLineHeight) =>
  64. if lineHeight != oldLineHeight
  65. settings.saveSettings({lineHeight: lineHeight})
  66. $scope.$watch "project.spellCheckLanguage", (language, oldLanguage) =>
  67. return if @ignoreUpdates
  68. if oldLanguage? and language != oldLanguage
  69. settings.saveProjectSettings({spellCheckLanguage: language})
  70. # Also set it as the default for the user
  71. settings.saveSettings({spellCheckLanguage: language})
  72. $scope.$watch "project.compiler", (compiler, oldCompiler) =>
  73. return if @ignoreUpdates
  74. if oldCompiler? and compiler != oldCompiler
  75. settings.saveProjectSettings({compiler: compiler})
  76. $scope.$watch "project.imageName", (imageName, oldImageName) =>
  77. return if @ignoreUpdates
  78. if oldImageName? and imageName != oldImageName
  79. settings.saveProjectSettings({imageName: imageName})
  80. $scope.$watch "project.rootDoc_id", (rootDoc_id, oldRootDoc_id) =>
  81. return if @ignoreUpdates
  82. # don't save on initialisation, Angular passes oldRootDoc_id as
  83. # undefined in this case.
  84. return if typeof oldRootDoc_id is "undefined"
  85. # otherwise only save changes, null values are allowed
  86. if (rootDoc_id != oldRootDoc_id)
  87. settings.saveProjectSettings({rootDocId: rootDoc_id})
  88. ide.socket.on "compilerUpdated", (compiler) =>
  89. @ignoreUpdates = true
  90. $scope.$apply () =>
  91. $scope.project.compiler = compiler
  92. delete @ignoreUpdates
  93. ide.socket.on "imageNameUpdated", (imageName) =>
  94. @ignoreUpdates = true
  95. $scope.$apply () =>
  96. $scope.project.imageName = imageName
  97. delete @ignoreUpdates
  98. ide.socket.on "spellCheckLanguageUpdated", (languageCode) =>
  99. @ignoreUpdates = true
  100. $scope.$apply () =>
  101. $scope.project.spellCheckLanguage = languageCode
  102. delete @ignoreUpdates
  103. ]