SettingsManager.coffee 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. self.ide.socket.emit 'deleteProject', ->
  36. window.location = '/'
  37. modalOptions =
  38. templateId:'deleteEntityModal'
  39. isStatic: false
  40. title:'Delete Project'
  41. message: "Are you sure you want to delete this project?"
  42. buttons: [{
  43. text : "Cancel",
  44. class : "btn",
  45. }, {
  46. text : "Delete Forever",
  47. class : "btn-danger confirm",
  48. callback : deleteProject
  49. }]
  50. Modal.createModal modalOptions
  51. $('.cloneProject').click (event)=>
  52. event.stopPropagation()
  53. event.preventDefault()
  54. goToRegPage = ->
  55. window.location = "/register"
  56. modalOptions =
  57. isStatic: false
  58. title:'Registration Required'
  59. message: "You need to register to clone a project"
  60. buttons: [{
  61. text : "Cancel",
  62. class : "btn",
  63. }, {
  64. text : "Register Now",
  65. class : "btn-success confirm",
  66. callback : goToRegPage
  67. }]
  68. user = @project.get("ide").user
  69. if user.id == "openUser"
  70. return Modal.createModal modalOptions
  71. $modal = $('#cloneProjectModal')
  72. $confirm = $('#confirmCloneProject')
  73. $modal.modal({backdrop:true, show:true, keyboard:true})
  74. $modal.find('input').val('').focus()
  75. self = @
  76. $confirm.click (e)->
  77. $confirm.attr("disabled", true)
  78. $confirm.text("Cloning...")
  79. projectName = $modal.find('input').val()
  80. $.ajax
  81. url: "/project/#{self.ide.project_id}/clone"
  82. type:'POST'
  83. data:
  84. projectName: projectName
  85. _csrf: window.csrfToken
  86. success: (data)->
  87. if data.redir?
  88. window.location = data.redir
  89. else if data.project_id?
  90. window.location = '/project/'+data.project_id
  91. $modal.on 'hide', ->
  92. $confirm.off 'click'
  93. $modal.find('.cancel').click (e)->
  94. $modal.modal('hide')
  95. setFontSize: () ->
  96. @fontSizeCss = $("<style/>")
  97. @fontSizeCss.text """
  98. .ace_editor, .ace_content {
  99. font-size: #{window.userSettings.fontSize}px;
  100. }
  101. """
  102. $(document.body).append(@fontSizeCss)
  103. bindToProjectName: () ->
  104. @project.on "change:name", (project, newName) ->
  105. $element = $('.projectName')
  106. $element.text(newName)
  107. window.document.title = newName
  108. $("input.projectName").val(newName)
  109. $("input.projectName").on "change", (e)=>
  110. # Check if event was triggered by the user, re:
  111. # http://stackoverflow.com/questions/6692031/check-if-event-is-triggered-by-a-human
  112. if e.originalEvent?
  113. if @ide.isAllowedToDoIt "readAndWrite"
  114. newName = e.target.value?.trim()
  115. $("input.projectName").val(newName)
  116. @project.set("name", newName)
  117. bindToCompiler: ->
  118. $('select#compilers').val(@project.get("compiler"))
  119. $('select#compilers').change (e)=>
  120. if @ide.isAllowedToDoIt "readAndWrite"
  121. @project.set("compiler", e.target.value)
  122. bindToSpellingPreference: ->
  123. $('select#spellCheckLanguageSelection').on "change", (e)=>
  124. languageCode = e.target.value
  125. @project.set("spellCheckLanguage", languageCode)
  126. bindToRootDocument: () ->
  127. $('#rootDocList').change (event)=>
  128. docId = $(event.target).val()
  129. @project.set("rootDoc_id", docId)
  130. # Repopulate the root document list when the settings page is shown. Updating
  131. # it in real time is just a little too complicated
  132. do refreshDocList = =>
  133. $docList = $('select#rootDocList')
  134. $docList.empty()
  135. @project.getRootDocumentsList (listOfDocs) =>
  136. template = _.template($('#rootDocListEntity').html())
  137. _.each listOfDocs, (doc)=>
  138. option = $(template(name:doc.path))
  139. option.attr('value', doc._id)
  140. $docList.append(option)
  141. hasRootDoc = _.find listOfDocs, (doc)=>
  142. if doc._id == @project.get("rootDoc_id")
  143. return true
  144. if hasRootDoc
  145. $docList.val(@project.get("rootDoc_id"))
  146. else
  147. option = $(template(name:"No Root Document Selected!"))
  148. option.attr('value', 'blank')
  149. $docList.append(option)
  150. $docList.val('blank')
  151. $('#settings-tab-li').on "click", refreshDocList
  152. bindToPublicAccessLevel: () ->
  153. $('select#publicAccessLevel').val(@project.get("publicAccesLevel"))
  154. @project.on "change:publicAccesLevel", (project, level) ->
  155. $('select#publicAccessLevel').val(level)
  156. $("select#publicAccessLevel").on "change", (event)=>
  157. newSetting = event.target.value
  158. cancelChange = =>
  159. @project.set("publicAccesLevel", "private")
  160. doChange = () =>
  161. @project.set("publicAccesLevel", newSetting)
  162. modalOptions =
  163. buttons: [{
  164. text : "Cancel",
  165. class : "btn",
  166. callback : cancelChange
  167. },{
  168. text : "OK",
  169. class : "btn-danger confirm",
  170. callback : doChange
  171. }]
  172. if newSetting == 'readOnly'
  173. modalOptions.title = 'Make Project Public - Read Only'
  174. 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'
  175. else if newSetting == 'readAndWrite'
  176. modalOptions.title = 'Make Project Public - Read and Write'
  177. 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'
  178. if newSetting == 'private'
  179. modalOptions.title = 'Make Project Private'
  180. 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'
  181. Modal.createModal modalOptions