SettingsManager.coffee 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. define [
  2. "utils/Modal"
  3. "settings/DropboxSettingsManager"
  4. ], (Modal, DropboxSettingsManager) ->
  5. class SettingsManager
  6. templates:
  7. settingsPanel: $("#settingsPanelTemplate").html()
  8. constructor: (@ide, options) ->
  9. options || = {}
  10. setup = _.once =>
  11. @bindToProjectName()
  12. @bindToPublicAccessLevel()
  13. @bindToCompiler()
  14. @bindToRootDocument()
  15. @bindToSpellingPreference()
  16. new DropboxSettingsManager @ide
  17. @setFontSize()
  18. if @ide?
  19. @ide.on "afterJoinProject", (project) =>
  20. @project = project
  21. setup()
  22. settingsPanel = $(@templates.settingsPanel)
  23. @ide.tabManager.addTab
  24. id: "settings"
  25. name: "Settings"
  26. content: settingsPanel
  27. lock: true
  28. $('#DownloadZip').click (event)=>
  29. event.preventDefault()
  30. @ide.mainAreaManager.setIframeSrc "/project/#{@ide.project_id}/download/zip"
  31. $("#deleteProject").click (event)=>
  32. event.preventDefault()
  33. self = @
  34. deleteProject = =>
  35. $.ajax
  36. url: "/Project/#{@ide.project_id}",
  37. type: 'DELETE'
  38. data:
  39. _csrf: window.csrfToken
  40. success: ->
  41. window.location = '/'
  42. modalOptions =
  43. templateId:'deleteEntityModal'
  44. isStatic: false
  45. title:'Delete Project'
  46. message: "Are you sure you want to delete this project?"
  47. buttons: [{
  48. text : "Cancel",
  49. class : "btn",
  50. }, {
  51. text : "Delete Forever",
  52. class : "btn-danger confirm",
  53. callback : deleteProject
  54. }]
  55. Modal.createModal modalOptions
  56. $('.cloneProject').click (event)=>
  57. event.stopPropagation()
  58. event.preventDefault()
  59. goToRegPage = ->
  60. window.location = "/register"
  61. modalOptions =
  62. isStatic: false
  63. title:'Registration Required'
  64. message: "You need to register to clone a project"
  65. buttons: [{
  66. text : "Cancel",
  67. class : "btn",
  68. }, {
  69. text : "Register Now",
  70. class : "btn-success confirm",
  71. callback : goToRegPage
  72. }]
  73. user = @project.get("ide").user
  74. if user.id == "openUser"
  75. return Modal.createModal modalOptions
  76. $modal = $('#cloneProjectModal')
  77. $confirm = $('#confirmCloneProject')
  78. $modal.modal({backdrop:true, show:true, keyboard:true})
  79. $modal.find('input').val('').focus()
  80. self = @
  81. $confirm.click (e)->
  82. $confirm.attr("disabled", true)
  83. $confirm.text("Cloning...")
  84. projectName = $modal.find('input').val()
  85. $.ajax
  86. url: "/project/#{self.ide.project_id}/clone"
  87. type:'POST'
  88. data:
  89. projectName: projectName
  90. _csrf: window.csrfToken
  91. success: (data)->
  92. if data.redir?
  93. window.location = data.redir
  94. else if data.project_id?
  95. window.location = '/project/'+data.project_id
  96. $modal.on 'hide', ->
  97. $confirm.off 'click'
  98. $modal.find('.cancel').click (e)->
  99. $modal.modal('hide')
  100. setFontSize: () ->
  101. @fontSizeCss = $("<style/>")
  102. @fontSizeCss.text """
  103. .ace_editor, .ace_content {
  104. font-size: #{window.userSettings.fontSize}px;
  105. }
  106. """
  107. $(document.body).append(@fontSizeCss)
  108. bindToProjectName: () ->
  109. @project.on "change:name", (project, newName) ->
  110. $element = $('.projectName')
  111. $element.text(newName)
  112. window.document.title = newName
  113. $("input.projectName").val(newName)
  114. $("input.projectName").on "change", (e)=>
  115. # Check if event was triggered by the user, re:
  116. # http://stackoverflow.com/questions/6692031/check-if-event-is-triggered-by-a-human
  117. if e.originalEvent?
  118. if @ide.isAllowedToDoIt "readAndWrite"
  119. newName = e.target.value?.trim()
  120. $("input.projectName").val(newName)
  121. @project.set("name", newName)
  122. bindToCompiler: ->
  123. $('select#compilers').val(@project.get("compiler"))
  124. $('select#compilers').change (e)=>
  125. if @ide.isAllowedToDoIt "readAndWrite"
  126. @project.set("compiler", e.target.value)
  127. bindToSpellingPreference: ->
  128. $('select#spellCheckLanguageSelection').on "change", (e)=>
  129. languageCode = e.target.value
  130. @project.set("spellCheckLanguage", languageCode)
  131. bindToRootDocument: () ->
  132. $('#rootDocList').change (event)=>
  133. docId = $(event.target).val()
  134. @project.set("rootDoc_id", docId)
  135. # Repopulate the root document list when the settings page is shown. Updating
  136. # it in real time is just a little too complicated
  137. do refreshDocList = =>
  138. $docList = $('select#rootDocList')
  139. $docList.empty()
  140. @project.getRootDocumentsList (listOfDocs) =>
  141. template = _.template($('#rootDocListEntity').html())
  142. _.each listOfDocs, (doc)=>
  143. option = $(template(name:doc.path))
  144. option.attr('value', doc._id)
  145. $docList.append(option)
  146. hasRootDoc = _.find listOfDocs, (doc)=>
  147. if doc._id == @project.get("rootDoc_id")
  148. return true
  149. if hasRootDoc
  150. $docList.val(@project.get("rootDoc_id"))
  151. else
  152. option = $(template(name:"No Root Document Selected!"))
  153. option.attr('value', 'blank')
  154. $docList.append(option)
  155. $docList.val('blank')
  156. $('#settings-tab-li').on "click", refreshDocList
  157. bindToPublicAccessLevel: () ->
  158. $('select#publicAccessLevel').val(@project.get("publicAccesLevel"))
  159. @project.on "change:publicAccesLevel", (project, level) ->
  160. $('select#publicAccessLevel').val(level)
  161. $("select#publicAccessLevel").on "change", (event)=>
  162. newSetting = event.target.value
  163. cancelChange = =>
  164. @project.set("publicAccesLevel", "private")
  165. doChange = () =>
  166. @project.set("publicAccesLevel", newSetting)
  167. modalOptions =
  168. buttons: [{
  169. text : "Cancel",
  170. class : "btn",
  171. callback : cancelChange
  172. },{
  173. text : "OK",
  174. class : "btn-danger confirm",
  175. callback : doChange
  176. }]
  177. if newSetting == 'readOnly'
  178. modalOptions.title = 'Make Project Public - Read Only'
  179. modalOptions.message = 'Are you sure you want make this project public to the world? Google and search engines will be able to see it. Public users will not be able to edit the project'
  180. else if newSetting == 'readAndWrite'
  181. modalOptions.title = 'Make Project Public - Read and Write'
  182. modalOptions.message = 'Are you sure you want make this project public to the world? Google and search engines will be able to see it. Public users will be able to write and modify the project'
  183. if newSetting == 'private'
  184. modalOptions.title = 'Make Project Private'
  185. modalOptions.message = 'Are you sure you want make this project private? Only registered users who are given permission below will be able to view the project'
  186. Modal.createModal modalOptions