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/9745155 to your computer and use it in GitHub Desktop.
Save piersadrian/9745155 to your computer and use it in GitHub Desktop.
'use strict';
var BaseCtrl, ConditionEditCtrl, ConditionsListCtrl, EditCtrl, app,
__slice = [].slice,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
app = angular.module('ngContentApp');
BaseCtrl = (function() {
BaseCtrl.$inject = ['$scope', '$location', '$route'];
function BaseCtrl() {
var dependencies, deps, fn, i, locals, name, _i, _len, _ref;
dependencies = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
deps = {};
_ref = this.constructor.$inject;
for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
name = _ref[i];
deps[name] = dependencies[i];
}
locals = {};
for (name in this) {
fn = this[name];
if (name[0] === "$") {
(function(_this) {
return (function(fn) {
return locals[name.slice(1)] = function() {
return fn.apply(_this, arguments);
};
});
})(this)(fn);
}
}
deps.$scope = angular.extend(deps.$scope, locals);
for (name in deps) {
fn = deps[name];
this[name] = fn;
}
this.initialize();
}
BaseCtrl.prototype.initialize = function() {};
BaseCtrl.prototype.pathFor = function(ctrlName, params) {
var p, route, routePath, _ref;
if (params == null) {
params = {};
}
_ref = this.$route.routes;
for (routePath in _ref) {
route = _ref[routePath];
if (ctrlName === route.controller) {
for (p in params) {
routePath = routePath.replace(':' + p, params[p]);
}
return routePath;
}
}
return null;
};
return BaseCtrl;
})();
EditCtrl = (function(_super) {
__extends(EditCtrl, _super);
function EditCtrl() {
return EditCtrl.__super__.constructor.apply(this, arguments);
}
EditCtrl.$inject = _super.$inject.concat(['MediaUploader']);
EditCtrl.prototype.initialize = function() {
EditCtrl.__super__.initialize.call(this);
return this.$scope.uploader = new this.MediaUploader();
};
EditCtrl.prototype.confirmNavigate = function() {
return this.$scope.contentForm.$pristine || confirm("Unsaved changes will be lost! Click OK to leave without saving.");
};
return EditCtrl;
})(BaseCtrl);
app.controller('ConditionsListCtrl', ConditionsListCtrl = (function(_super) {
__extends(ConditionsListCtrl, _super);
function ConditionsListCtrl() {
this.loadContent = __bind(this.loadContent, this);
this.loadUnpublishedConditions = __bind(this.loadUnpublishedConditions, this);
return ConditionsListCtrl.__super__.constructor.apply(this, arguments);
}
ConditionsListCtrl.$inject = _super.$inject.concat(['$http', 'Condition']);
ConditionsListCtrl.prototype.initialize = function() {
ConditionsListCtrl.__super__.initialize.call(this);
this.loadUnpublishedConditions();
return this.$scope.loadContent = _.debounce(this.loadContent, 200);
};
ConditionsListCtrl.prototype.loadUnpublishedConditions = function() {
return this.$scope.conditions = this.Condition.query({
published: false
});
};
ConditionsListCtrl.prototype.$editCondition = function(condition) {
return this.$location.path(this.pathFor('ConditionEditCtrl', {
conditionId: condition.id
}));
};
ConditionsListCtrl.prototype.$createCondition = function() {
return new this.Condition({
name: this.$scope.newConditionName
}).$save({}, (function(_this) {
return function(c) {
return _this.$scope.editCondition(c);
};
})(this));
};
ConditionsListCtrl.prototype.loadContent = function() {
var _ref;
if (((_ref = this.$scope.query) != null ? _ref.length : void 0) > 2) {
return this.$http.get(window.routes['conditionsSearch'], {
params: {
name: this.$scope.query
}
}).then((function(_this) {
return function(response) {
return _this.$scope.conditions = response.data;
};
})(this));
} else {
return this.loadUnpublishedConditions();
}
};
return ConditionsListCtrl;
})(BaseCtrl));
app.controller('ConditionEditCtrl', ConditionEditCtrl = (function(_super) {
__extends(ConditionEditCtrl, _super);
function ConditionEditCtrl() {
this.$cancel = __bind(this.$cancel, this);
this.$save = __bind(this.$save, this);
this.$listTreatments = __bind(this.$listTreatments, this);
this.$createMedia = __bind(this.$createMedia, this);
return ConditionEditCtrl.__super__.constructor.apply(this, arguments);
}
ConditionEditCtrl.$inject = _super.$inject.concat(['condition']);
ConditionEditCtrl.prototype.initialize = function() {
ConditionEditCtrl.__super__.initialize.call(this);
return this.$scope.condition = this.$scope.editingItem = this.condition;
};
ConditionEditCtrl.prototype.$createMedia = function(files) {
return this.$scope.uploader.createMedia(files, "Condition", this.$scope.condition.id, {
success: (function(_this) {
return function() {
return _this.$scope.condition.$get();
};
})(this),
error: function() {
return alert("Failed to upload! (code " + status + ")");
}
});
};
ConditionEditCtrl.prototype.$listTreatments = function(condition) {
return this.$location.path(this.pathFor('TreatmentsListCtrl', {
conditionId: condition.id
}));
};
ConditionEditCtrl.prototype.$save = function() {
return this.$scope.condition.$save({}, (function(_this) {
return function(event) {
return _this.$scope.listConditions();
};
})(this));
};
ConditionEditCtrl.prototype.$cancel = function() {
if (this.confirmNavigate()) {
return this.$scope.listConditions();
}
};
ConditionEditCtrl.prototype.$listConditions = function() {
return this.$location.path(this.pathFor('ConditionsListCtrl'));
};
return ConditionEditCtrl;
})(EditCtrl));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment