Skip to content

Instantly share code, notes, and snippets.

@piersadrian
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piersadrian/9745148 to your computer and use it in GitHub Desktop.
Save piersadrian/9745148 to your computer and use it in GitHub Desktop.
'use strict'
app = angular.module('ngContentApp')
class BaseCtrl
@$inject: ['$scope', '$location', '$route']
constructor: (dependencies...) ->
# Construct a map of injected dependencies.
deps = {}
deps[name] = dependencies[i] for name, i in @constructor.$inject
# Controller properties and methods to attach to `$scope` are
# marked with a leading '$'.
locals = {}
for name, val of this when name[0] == "$"
do (val) =>
locals[name.slice(1)] = if typeof val == "function"
# Wrap `$scope` methods so they execute in the controller's context.
then => val.apply(this, arguments)
else val
# Add the controller API to the injected `$scope`.
deps.$scope = angular.extend(deps.$scope, locals)
# Attach the dependency map.
@[name] = val for name, val of deps
# Set up the controller's initial state.
@initialize()
initialize: ->
# noop
pathFor: (ctrlName, params = {}) ->
for routePath, route of @$route.routes
if ctrlName == route.controller
routePath = routePath.replace(':' + name, val) for name, val of params
return routePath
return null
class ContentEditCtrl extends BaseCtrl
@$inject: _super.$inject.concat ['MediaUploader']
initialize: ->
super()
@$scope.uploader = new @MediaUploader()
window.onbeforeunload = =>
@confirmUnload()
$tinyMCEConfig:
resize: false
height: 300
browser_spellcheck: true
init_instance_callback: (editor) ->
editor.getBody().style.fontSize = "16px"
confirmMessage: "Unsaved changes will be lost! Press 'Cancel'
or 'Stay on Page' to return to editing."
confirmNavigate: ->
@$scope.contentForm.$pristine || confirm(@confirmMessage)
confirmUnload: ->
if @$scope.contentForm.$pristine then null else @confirmMessage
app.controller 'ConditionsListCtrl',
class ConditionsListCtrl extends BaseCtrl
@$inject: _super.$inject.concat ['$http', 'Condition']
initialize: ->
super()
@loadUnpublishedConditions()
@$scope.loadContent = _.debounce(@loadContent, 200)
loadUnpublishedConditions: =>
@$scope.conditions = @Condition.query({ published: false })
$editCondition: (condition) ->
@$location.path( @pathFor('ConditionEditCtrl',
conditionId: condition.id
))
$createCondition: ->
new @Condition({ name: @$scope.newConditionName })
.$save {}, (c) =>
@$scope.editCondition(c)
loadContent: =>
if @$scope.query?.length > 2
@$http.get window.routes['conditionsSearch'],
params:
name: @$scope.query
.then (response) =>
@$scope.conditions = response.data
else
@loadUnpublishedConditions()
app.controller 'ConditionEditCtrl',
class ConditionEditCtrl extends ContentEditCtrl
@$inject: _super.$inject.concat ['condition']
initialize: ->
super()
@$scope.condition = @$scope.editingItem = @condition
$createMedia: (files) =>
@$scope.uploader.createMedia files, "Condition", @$scope.condition.id,
success: =>
@$scope.condition.$get()
error: ->
alert("Failed to upload! (code #{ status })")
$listTreatments: (condition) =>
if @confirmNavigate()
@$location.path @pathFor 'TreatmentsListCtrl',
conditionId: condition.id
$save: =>
@$scope.condition.$save {}, => @$scope.listConditions()
$cancel: =>
@$scope.listConditions() if @confirmNavigate()
$listConditions: ->
@$location.path( @pathFor('ConditionsListCtrl') )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment