Initial commit
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
var Backbone = require('backbone');
|
||||
var MosaicModel = require('./mosaic-item-model');
|
||||
|
||||
/*
|
||||
* List collection, it parses pairs like:
|
||||
*
|
||||
* [{ val, label }]
|
||||
* ["string"]
|
||||
*/
|
||||
|
||||
module.exports = Backbone.Collection.extend({
|
||||
|
||||
model: function (attrs, opts) {
|
||||
var d = {};
|
||||
if (typeof attrs === 'string') {
|
||||
d.val = attrs;
|
||||
} else {
|
||||
d = attrs;
|
||||
}
|
||||
return new MosaicModel(d);
|
||||
},
|
||||
|
||||
initialize: function () {
|
||||
this._initBinds();
|
||||
},
|
||||
|
||||
_initBinds: function () {
|
||||
this.bind('change:selected', this._onSelectedChange, this);
|
||||
},
|
||||
|
||||
_onSelectedChange: function (changedModel, isSelected) {
|
||||
if (isSelected) {
|
||||
this.each(function (m) {
|
||||
if (m.cid !== changedModel.cid && m.get('selected')) {
|
||||
m.set('selected', false);
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
getSelected: function () {
|
||||
return this.findWhere({ selected: true });
|
||||
},
|
||||
|
||||
getHighlighted: function () {
|
||||
return this.findWhere({ highlighted: true });
|
||||
}
|
||||
|
||||
});
|
||||
@@ -0,0 +1,27 @@
|
||||
var Backbone = require('backbone');
|
||||
|
||||
/*
|
||||
* List item model
|
||||
*
|
||||
*/
|
||||
|
||||
module.exports = Backbone.Model.extend({
|
||||
|
||||
defaults: {
|
||||
disabled: false,
|
||||
selected: false,
|
||||
label: '',
|
||||
template: function () {
|
||||
return '';
|
||||
}
|
||||
},
|
||||
|
||||
getName: function () {
|
||||
return this.get('label') || this.getValue();
|
||||
},
|
||||
|
||||
getValue: function () {
|
||||
return this.get('val');
|
||||
}
|
||||
|
||||
});
|
||||
@@ -0,0 +1,54 @@
|
||||
var CoreView = require('backbone/core-view');
|
||||
var template = require('./mosaic-item.tpl');
|
||||
|
||||
module.exports = CoreView.extend({
|
||||
|
||||
className: 'Mosaic-item',
|
||||
|
||||
tagName: 'li',
|
||||
|
||||
events: {
|
||||
'mouseenter': '_onMouseEnter',
|
||||
'mouseleave': '_onMouseLeave',
|
||||
'click': '_onClick'
|
||||
},
|
||||
|
||||
initialize: function (opts) {
|
||||
this._disabled = opts.disabled;
|
||||
|
||||
this._initBinds();
|
||||
},
|
||||
|
||||
render: function () {
|
||||
this.$el.html(
|
||||
template({
|
||||
name: this.model.getName(),
|
||||
template: this.model.get('template')()
|
||||
})
|
||||
);
|
||||
this.$el.addClass('js-' + this.model.getValue());
|
||||
this.$el.toggleClass('is-selected', !!this.model.get('selected'));
|
||||
return this;
|
||||
},
|
||||
|
||||
_initBinds: function () {
|
||||
this.model.bind('change:selected', this.render, this);
|
||||
},
|
||||
|
||||
_onMouseEnter: function () {
|
||||
this.model.set('highlighted', true);
|
||||
},
|
||||
|
||||
_onMouseLeave: function () {
|
||||
this.model.set('highlighted', false);
|
||||
},
|
||||
|
||||
_onClick: function () {
|
||||
if (this._disabled) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this.model.set('selected', true);
|
||||
}
|
||||
|
||||
});
|
||||
@@ -0,0 +1,3 @@
|
||||
<button class="Mosaic-inner">
|
||||
<%= template %>
|
||||
</button>
|
||||
@@ -0,0 +1 @@
|
||||
<img src="<%= imgURL %>" class="Mosaic-image js-thumbnailImg" />
|
||||
@@ -0,0 +1,60 @@
|
||||
var CoreView = require('backbone/core-view');
|
||||
var template = require('./mosaic.tpl');
|
||||
var MosaicCollection = require('./mosaic-collection');
|
||||
var MosaicItemView = require('./mosaic-item-view');
|
||||
|
||||
/*
|
||||
* A mosaic selector
|
||||
*
|
||||
* It accepts a collection of (val, label) model attributes or a values array
|
||||
* with the same content or only strings.
|
||||
*
|
||||
* new Mosaic({
|
||||
* options: [
|
||||
* {
|
||||
* val: 'hello',
|
||||
* label: 'hi'
|
||||
* }
|
||||
* ]
|
||||
* });
|
||||
*/
|
||||
|
||||
module.exports = CoreView.extend({
|
||||
|
||||
className: 'Mosaic',
|
||||
|
||||
tagName: 'div',
|
||||
|
||||
initialize: function (opts) {
|
||||
if (!opts.collection) {
|
||||
if (!opts.options) throw new Error('options array {value, label} is required');
|
||||
|
||||
this.collection = new MosaicCollection(opts.options);
|
||||
}
|
||||
|
||||
this._disabled = opts.disabled;
|
||||
},
|
||||
|
||||
render: function () {
|
||||
this.clearSubViews();
|
||||
this.$el.html(template());
|
||||
this._renderList();
|
||||
return this;
|
||||
},
|
||||
|
||||
_renderList: function () {
|
||||
this.collection.each(function (mdl) {
|
||||
this._createItem(mdl);
|
||||
}, this);
|
||||
},
|
||||
|
||||
_createItem: function (mdl) {
|
||||
var view = new MosaicItemView({
|
||||
model: mdl,
|
||||
disabled: this._disabled
|
||||
});
|
||||
this.$('.js-mosaic').append(view.render().el);
|
||||
this.addView(view);
|
||||
}
|
||||
|
||||
});
|
||||
@@ -0,0 +1 @@
|
||||
<ul class="Mosaic-list js-mosaic"></ul>
|
||||
Reference in New Issue
Block a user