foam-framework
Version:
MVC metaprogramming framework
100 lines (91 loc) • 2.51 kB
JavaScript
/**
* @license
* Copyright 2014 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
CLASS({
package: 'foam.ui',
name: 'ViewSwitcher',
extends: 'foam.ui.SimpleView',
help: 'A view which cycles between an array of views.',
properties: [
{
name: 'views',
factory: function() { return []; },
postSet: function() {
this.viewIndex = this.viewIndex;
},
},
{
name: 'data',
postSet: function(_, data) { this.activeView.data = data; }
},
{
name: 'activeView',
postSet: function(old, view) {
if ( old ) {
old.unsubscribe('nextview', this.onNextView);
old.unsubscribe('prevview', this.onPrevView);
}
view.subscribe('nextview', this.onNextView);
view.subscribe('prevview', this.onPrevView);
view.data = this.data;
}
},
{
model_: 'IntProperty',
name: 'viewIndex',
preSet: function(_, value) {
if ( value >= this.views.length ) return 0;
if ( value < 0 ) return this.views.length - 1;
return value;
},
postSet: function() {
this.activeView = this.views[this.viewIndex];
}
}
],
methods: {
toHTML: function() {
return '<div id="' + this.id + '" style="display:none"></div>' + this.toInnerHTML();
},
updateHTML: function() {
if ( ! this.$ ) return;
this.$.nextElementSibling.outerHTML = this.toInnerHTML();
this.initInnerHTML();
},
toInnerHTML: function() {
return this.activeView.toHTML();
},
initInnerHTML: function() {
this.activeView.initInnerHTML();
}
},
listeners: [
{
name: 'onNextView',
code: function() {
this.viewIndex = this.viewIndex + 1;
this.updateHTML();
}
},
{
name: 'onPrevView',
code: function() {
this.viewIndex = this.viewIndex - 1;
this.updateHTML();
}
}
]
});