// Generated by CoffeeScript 1.7.1 (function() { var Db, Model, ViewModel, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; ViewModel = (function() { function ViewModel() { this.loading = ko.observable(false); this.flashMessage = ko.observable(''); this.flashMessage.subscribe((function(_this) { return function(newValue) { if ((newValue != null) && newValue !== '') { setTimeout(function() { return _this.flashMessage(''); }, 10000); } return true; }; })(this)); this.errorMessage = ko.observable(''); this.errorMessage.subscribe((function(_this) { return function(newValue) { if ((newValue != null) && newValue !== '') { setTimeout(function() { return _this.errorMessage(''); }, 10000); } return true; }; })(this)); } ViewModel.prototype.systemNotification = function(name, value) { $('body').attr("data-" + name, value); return this.loading(/ing$/.test(value)); }; ViewModel.prototype.flashPanel = function(elementId) { $(elementId).fadeIn(); return setTimeout((function(_this) { return function() { return $(elementId).fadeOut(); }; })(this), 10000); }; return ViewModel; })(); Db = (function() { function Db(viewModel, name, url, pushStateUri) { this.viewModel = viewModel; this.name = name; this.pushStateUri = pushStateUri; this.errorMessage = __bind(this.errorMessage, this); this.flashMessage = __bind(this.flashMessage, this); this.doDelete = __bind(this.doDelete, this); this.urlFor = __bind(this.urlFor, this); this.toJS = __bind(this.toJS, this); this.itemDataFrom = __bind(this.itemDataFrom, this); this.newItem = __bind(this.newItem, this); this.doDestroy = __bind(this.doDestroy, this); this.doUpdate = __bind(this.doUpdate, this); this.doCreate = __bind(this.doCreate, this); this.doLoad = __bind(this.doLoad, this); this.save = __bind(this.save, this); this.add = __bind(this.add, this); this.load = __bind(this.load, this); this.canLoad = __bind(this.canLoad, this); this.findOrCreate = __bind(this.findOrCreate, this); this.find = __bind(this.find, this); this.url = ko.observable(url); this.createUrl = ko.observable(url); this.items = ko.observableArray([]); this.selected = ko.observable(null); this.selected.subscribe((function(_this) { return function(newValue) { if (_this.selected() == null) { return; } if (_this.selected().hasBeenSelected) { return _this.selected().hasBeenSelected(); } }; })(this)); this.plural = "" + this.name + "s"; this.sortFunction = null; this.onAfterLoad = null; this.onBeforeLoad = null; this.onAfterDelete = null; this.onAfterSave = null; this.onAfterPost = null; this.autoLoading = false; this.loading = false; this.url.subscribe((function(_this) { return function(newValue) { if (newValue != null) { return _this.load(false); } }; })(this)); if ((this.pushStateUri != null) && (history.pushState != null)) { this.selected.subscribe((function(_this) { return function(newValue) { if ((newValue != null) && (newValue.id != null)) { return history.pushState(_this.selected().name(), _this.selected().name(), "" + _this.pushStateUri + "/" + (_this.selected().id)); } else { return history.pushState('', '', _this.pushStateUri); } }; })(this)); } } Db.prototype.systemNotification = function(name, value) { return this.viewModel.systemNotification(name, value); }; Db.prototype.find = function(id) { var item, _i, _len, _ref; _ref = this.items(); for (_i = 0, _len = _ref.length; _i < _len; _i++) { item = _ref[_i]; if (item.id === id) { return item; } } return null; }; Db.prototype.findOrCreate = function(id) { var item; item = this.find(id); if (item == null) { item = this.newItem(id); this.items.push(item); } return item; }; Db.prototype.canLoad = function() { if ((this.url() == null) || this.url() === '') { return false; } return true; }; Db.prototype.load = function(autoReload, afterLoad) { if (autoReload == null) { autoReload = false; } if (afterLoad == null) { afterLoad = null; } if (!this.canLoad()) { return; } if (!autoReload || !this.selected()) { this.viewModel.systemNotification(this.plural, 'loading'); this.viewModel.loading(true); if (this.onBeforeLoad != null) { this.onBeforeLoad(); } $.get(this.url(), (function(_this) { return function(data) { _this.doLoad(data); if (afterLoad != null) { afterLoad(data); } if (_this.onAfterLoad != null) { _this.onAfterLoad(data); } _this.viewModel.loading(false); return _this.viewModel.systemNotification(_this.plural, 'loaded'); }; })(this)); } if (autoReload) { this.autoLoading = true; setTimeout((function(_this) { return function() { return _this.load(true, afterLoad); }; })(this), 120000); } return false; }; Db.prototype.postTo = function(url, data, afterPost) { this.viewModel.systemNotification(this.name, 'saving'); this.viewModel.loading(true); $.ajax({ url: url, dataType: 'json', type: 'POST', data: data, success: (function(_this) { return function(data) { if (afterPost != null) { afterPost(data); } if (_this.onAfterPost != null) { _this.onAfterPost(data); } _this.viewModel.loading(false); _this.viewModel.systemNotification(_this.name, 'saved'); return _this.load(false); }; })(this) }); return true; }; Db.prototype.add = function() { var itemToAdd; itemToAdd = this.newItem(null); this.selected(itemToAdd); this.viewModel.systemNotification(this.name, 'new'); return itemToAdd; }; Db.prototype.save = function(item, afterSave, onError) { if (!item.valid() && (onError != null)) { onError(); } if (item.id != null) { return this.doUpdate(item, afterSave); } else { return this.doCreate(item, afterSave); } }; Db.prototype.doLoad = function(data) { var item, itemData, _i, _len, _ref; _ref = this.itemDataFrom(data); for (_i = 0, _len = _ref.length; _i < _len; _i++) { itemData = _ref[_i]; item = this.findOrCreate(itemData.id); if (item.updating != null) { item.updating(true); } item.updateAttributes(itemData); if (item.updating != null) { item.updating(false); } } if (this.sortFunction != null) { return this.items.sort(this.sortFunction); } }; Db.prototype.doCreate = function(item, afterSave) { this.viewModel.systemNotification(this.name, 'saving'); this.viewModel.loading(true); $.ajax({ url: this.createUrl(), dataType: 'json', type: 'POST', data: this.toJS(item), success: (function(_this) { return function(data) { if (data.id != null) { item.id = data.id; } _this.selected(null); if (afterSave != null) { afterSave(item); } if (_this.onAfterSave != null) { _this.onAfterSave(item); } _this.viewModel.systemNotification(_this.name, 'saved'); _this.viewModel.loading(false); return _this.load(); }; })(this) }); return false; }; Db.prototype.doUpdate = function(item, afterSave) { this.viewModel.systemNotification(this.name, 'saving'); this.viewModel.loading(true); $.ajax({ url: this.urlFor(item), dataType: 'json', type: 'PUT', data: this.toJS(item), success: (function(_this) { return function(data) { _this.selected(null); if (afterSave != null) { afterSave(item); } if (_this.onAfterSave != null) { _this.onAfterSave(item); } _this.viewModel.systemNotification(_this.name, 'saved'); _this.viewModel.loading(false); return _this.load(); }; })(this) }); return false; }; Db.prototype.doDestroy = function(item, afterDelete) { this.viewModel.systemNotification(this.name, 'deleting'); this.viewModel.loading(true); $.ajax({ url: this.urlFor(item), dataType: 'json', type: 'DELETE', success: (function(_this) { return function(data) { item.id = null; _this.selected(null); _this.items.remove(item); if (afterDelete != null) { afterDelete(); } if (_this.onAfterDelete != null) { _this.onAfterDelete(item); } _this.viewModel.systemNotification(_this.name, 'deleted'); _this.viewModel.loading(false); return _this.load(); }; })(this) }); return false; }; Db.prototype.newItem = function(id) { return null; }; Db.prototype.itemDataFrom = function(data) { return null; }; Db.prototype.toJS = function(item) { return null; }; Db.prototype.urlFor = function(item) { return null; }; Db.prototype.doDelete = function(client) { return null; }; Db.prototype.flashMessage = function(message) { return this.viewModel.flashMessage(message); }; Db.prototype.errorMessage = function(message) { return this.viewModel.errorMessage(message); }; return Db; })(); Model = (function() { function Model(id, db) { this.id = id; this.db = db; this.elementSelector = __bind(this.elementSelector, this); this.elementId = __bind(this.elementId, this); this.viewModel = __bind(this.viewModel, this); this.editing = ko.observable(false); this.deleting = ko.observable(false); this.updating = ko.observable(false); this.selected = ko.computed((function(_this) { return function() { return _this.db.selected() === _this; }; })(this)); } Model.prototype.viewModel = function() { return this.db.viewModel; }; Model.prototype.elementId = function() { return "" + this.db.name + "-" + this.id; }; Model.prototype.elementSelector = function() { return "#" + (this.elementId()); }; Model.prototype.updateAttributes = function(data) { return null; }; Model.prototype.valid = function() { return true; }; Model.prototype.select = function() { return this.db.selected(this); }; Model.prototype.deselect = function() { this.deleting(false); this.editing(false); return this.db.selected(null); }; Model.prototype.hasBeenSelected = function() {}; Model.prototype.edit = function() { this.select(); return this.editing(true); }; Model.prototype.stopEditing = function() { this.editing(false); return this.deselect(); }; Model.prototype.requiresDatePicker = function() { var i; i = document.createElement('input'); i.setAttribute('type', 'date'); return i.type !== 'date'; }; Model.prototype.startDeleting = function() { this.edit(); return this.deleting(true); }; Model.prototype.stopDeleting = function() { return this.deleting(false); }; Model.prototype.save = function() { if (!this.valid()) { return; } this.deselect(); return this.db.save(this); }; Model.prototype.doDestroy = function() { this.deselect(); return this.db.doDestroy(this); }; Model.prototype.systemNotification = function(name, value) { return this.db.systemNotification(name, value); }; return Model; })(); window.ViewModel = ViewModel; window.Db = Db; window.Model = Model; window.viewModel = new ViewModel; }).call(this);