Skip to content

Instantly share code, notes, and snippets.

@dbleier
Created January 31, 2016 19:11
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 dbleier/aadd33871757872d7273 to your computer and use it in GitHub Desktop.
Save dbleier/aadd33871757872d7273 to your computer and use it in GitHub Desktop.
import Map from 'can/map/';
import List from 'can/list/';
import 'can/map/define/';
import { Config } from 'menuboard-manager/config/config/';
import { Storage } from 'menuboard-manager/utils/storage/';
import Sign from 'menuboard-manager/models/sign';
import Presentation from 'menuboard-manager/models/presentation';
import Parser from 'menuboard-manager/utils/parser/';
export const SignController = Map.extend({
define: {
signPromise: {
get() {
return Sign.getList({mac: this.attr('mac')});
}
},
sign: {
get(last, set) {
var self = this;
this.attr('signPromise').then(function(s) {
var sign = s[0], actpres, curpres;
// 1st check if there is only 1 active presentation,
// if only 1 presentation exists at all, use it regardless
// whether it is marked active or not (better than a blank screen)
if(sign.presentations.length === 1) {
// don't need to worry about scheduling or any other multi-presention logic
curpres = self.processPresentation(sign.presentations[0]);
new Presentation({id: curpres.id, data: curpres}).save();
self.attr('currentPresentation', curpres.id);
set(sign);
} else {
actpres = sign.presentations.filter(function(pres) {
return pres.props.active;
});
if(!actpres.length) {
// no active presentations, throw error...
} else if(actpres.length === 1) {
// only 1 active presentation, treat like only presentation
curpres = self.processPresentation(actpres[0]);
new Presentation({id: curpres.id, data: curpres}).save();
self.attr('currentPresentation', curpres.id);
set(sign);
} else {
// multiple presentations, need to check scheduling, etc.
}
}
});
}
},
currentPresentation: {
},
mac: {
get() {
var mac = Storage.get('mac');
if(!mac) {
if(Config().attr('isLFD')) {
// running on monitor, get from SEF plugin
} else {
// running in browser, use preset value (for now)
mac = 'a4be90d1';
}
}
return mac;
}
}
},
processPresentation(pres) {
var processed = Parser.parsePresentation(pres);
return processed;
}
});
export default SignController;
@dbleier
Copy link
Author

dbleier commented Jan 31, 2016

Here is the unit test calling the code where set is undefined

QUnit.test('Sign Setup', function () {
    var sc = new SignController(), oldmac = Storage.get('mac');
    Storage.store('mac', 'a1b2c3d4');
    QUnit.notOk(sc.attr('currentPresentation'));
    sc.attr('sign');
    QUnit.deepEqual(sc.attr('currentPresentation'), 'test', 'currentPresentation id match id from fixture');
    oldmac ? Storage.store('mac', oldmac) : Storage.delete('mac');
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment