183 lines
7.9 KiB
JavaScript
183 lines
7.9 KiB
JavaScript
/**
|
|
* ComboBorderSize.js
|
|
*
|
|
* Created by Julia Radzhabova on 2/10/14
|
|
* Copyright (c) 2014 Ascensio System SIA. All rights reserved.
|
|
*
|
|
*/
|
|
|
|
/**
|
|
* Using template
|
|
*
|
|
* <div class="input-group input-group-nr combobox combo-border-size" id="id-combobox">
|
|
* <div class="form-control"></div>
|
|
* <div style="display: table-cell;"></div>
|
|
* <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"><span class="caret"></span></button>
|
|
* <ul class="dropdown-menu"></ul>
|
|
* </div>
|
|
*
|
|
*/
|
|
|
|
if (Common === undefined)
|
|
var Common = {};
|
|
|
|
define([
|
|
'common/main/lib/component/ComboBox'
|
|
], function () {
|
|
'use strict';
|
|
|
|
Common.UI.BordersModel = Backbone.Model.extend({
|
|
defaults: function() {
|
|
return {
|
|
value: null,
|
|
displayValue: null,
|
|
pxValue: null,
|
|
id: Common.UI.getId(),
|
|
offsety: undefined
|
|
}
|
|
}
|
|
});
|
|
|
|
Common.UI.BordersStore = Backbone.Collection.extend({
|
|
model: Common.UI.BordersModel
|
|
});
|
|
|
|
Common.UI.ComboBorderSize = Common.UI.ComboBox.extend(_.extend({
|
|
template: _.template([
|
|
'<div class="input-group combobox combo-border-size input-group-nr <%= cls %>" id="<%= id %>" style="<%= style %>">',
|
|
'<div class="form-control" style="<%= style %>"></div>',
|
|
'<div style="display: table-cell;"></div>',
|
|
'<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"><span class="caret img-commonctrl"></span></button>',
|
|
'<ul class="dropdown-menu <%= menuCls %>" style="<%= menuStyle %>" role="menu">',
|
|
'<% _.each(items, function(item) { %>',
|
|
'<li id="<%= item.id %>" data-value="<%= item.value %>"><a tabindex="-1" type="menuitem">',
|
|
'<span><%= item.displayValue %></span>',
|
|
'<% if (item.offsety!==undefined) { %>',
|
|
'<img src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" align="right" style="background-position: 0 -<%= item.offsety %>px;">',
|
|
'<% } %>',
|
|
'</a></li>',
|
|
'<% }); %>',
|
|
'</ul>',
|
|
'</div>'
|
|
].join('')),
|
|
|
|
initialize : function(options) {
|
|
Common.UI.ComboBox.prototype.initialize.call(this, _.extend({
|
|
editable: false,
|
|
store: new Common.UI.BordersStore(),
|
|
data: [
|
|
{displayValue: this.txtNoBorders, value: 0, pxValue: 0 },
|
|
{displayValue: '0.5 pt', value: 0.5, pxValue: 0.5, offsety: 0},
|
|
{displayValue: '1 pt', value: 1, pxValue: 1, offsety: 20},
|
|
{displayValue: '1.5 pt', value: 1.5, pxValue: 2, offsety: 40},
|
|
{displayValue: '2.25 pt', value: 2.25,pxValue: 3, offsety: 60},
|
|
{displayValue: '3 pt', value: 3, pxValue: 4, offsety: 80},
|
|
{displayValue: '4.5 pt', value: 4.5, pxValue: 5, offsety: 100},
|
|
{displayValue: '6 pt', value: 6, pxValue: 6, offsety: 120}
|
|
],
|
|
menuStyle: 'min-width: 150px;'
|
|
}, options));
|
|
},
|
|
|
|
render : function(parentEl) {
|
|
Common.UI.ComboBox.prototype.render.call(this, parentEl);
|
|
return this;
|
|
},
|
|
|
|
itemClicked: function (e) {
|
|
var el = $(e.currentTarget).parent();
|
|
|
|
this._selectedItem = this.store.findWhere({
|
|
id: el.attr('id')
|
|
});
|
|
if (this._selectedItem) {
|
|
$('.selected', $(this.el)).removeClass('selected');
|
|
el.addClass('selected');
|
|
this.updateFormControl(this._selectedItem);
|
|
|
|
this.trigger('selected', this, _.extend({}, this._selectedItem.toJSON()), e);
|
|
e.preventDefault();
|
|
}
|
|
},
|
|
|
|
updateFormControl: function(record) {
|
|
var formcontrol = $(this.el).find('.form-control');
|
|
|
|
if (record.get('value')>0) {
|
|
formcontrol[0].innerHTML = '';
|
|
formcontrol.removeClass('text').addClass('image');
|
|
formcontrol.css('background-position', '0 -' + record.get('offsety') + 'px');
|
|
} else {
|
|
formcontrol[0].innerHTML = this.txtNoBorders;
|
|
formcontrol.removeClass('image').addClass('text');
|
|
}
|
|
},
|
|
|
|
setValue: function(value) {
|
|
this._selectedItem = (value===null || value===undefined) ? undefined : _.find(this.store.models, function(item) {
|
|
if ( value<item.attributes.value+0.01 && value>item.attributes.value-0.01) {
|
|
return true;
|
|
}
|
|
});
|
|
|
|
$('.selected', $(this.el)).removeClass('selected');
|
|
|
|
if (this._selectedItem) {
|
|
this.updateFormControl(this._selectedItem);
|
|
$('#' + this._selectedItem.get('id'), $(this.el)).addClass('selected');
|
|
} else {
|
|
var formcontrol = $(this.el).find('.form-control');
|
|
formcontrol[0].innerHTML = '';
|
|
formcontrol.removeClass('image').addClass('text');
|
|
}
|
|
},
|
|
|
|
txtNoBorders: 'No Borders'
|
|
}, Common.UI.ComboBorderSize || {}));
|
|
|
|
Common.UI.ComboBorderSizeEditable = Common.UI.ComboBox.extend(_.extend({
|
|
template: _.template([
|
|
'<span class="input-group combobox combo-border-size input-group-nr <%= cls %>" id="<%= id %>" style="<%= style %>">',
|
|
'<input type="text" class="form-control text">',
|
|
'<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"><span class="caret img-commonctrl"></span></button>',
|
|
'<ul class="dropdown-menu <%= menuCls %>" style="<%= menuStyle %>" role="menu">',
|
|
'<% _.each(items, function(item) { %>',
|
|
'<li id="<%= item.id %>" data-value="<%= item.value %>"><a tabindex="-1" type="menuitem">',
|
|
'<span><%= item.displayValue %></span>',
|
|
'<% if (item.offsety!==undefined) { %>',
|
|
'<img src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" align="right" style="background-position: 0 -<%= item.offsety %>px;">',
|
|
'<% } %>',
|
|
'</a></li>',
|
|
'<% }); %>',
|
|
'</ul>',
|
|
'</span>'
|
|
].join('')),
|
|
|
|
initialize : function(options) {
|
|
this.txtNoBorders = options.txtNoBorders || this.txtNoBorders;
|
|
|
|
Common.UI.ComboBox.prototype.initialize.call(this, _.extend({
|
|
editable: true,
|
|
store: new Common.UI.BordersStore(),
|
|
data: [
|
|
{displayValue: this.txtNoBorders, value: 0, pxValue: 0 },
|
|
{displayValue: '0.5 pt', value: 0.5, pxValue: 0.5, offsety: 0},
|
|
{displayValue: '1 pt', value: 1, pxValue: 1, offsety: 20},
|
|
{displayValue: '1.5 pt', value: 1.5, pxValue: 2, offsety: 40},
|
|
{displayValue: '2.25 pt', value: 2.25,pxValue: 3, offsety: 60},
|
|
{displayValue: '3 pt', value: 3, pxValue: 4, offsety: 80},
|
|
{displayValue: '4.5 pt', value: 4.5, pxValue: 5, offsety: 100},
|
|
{displayValue: '6 pt', value: 6, pxValue: 6, offsety: 120}
|
|
],
|
|
menuStyle: 'min-width: 150px;'
|
|
}, options));
|
|
},
|
|
|
|
render : function(parentEl) {
|
|
Common.UI.ComboBox.prototype.render.call(this, parentEl);
|
|
return this;
|
|
},
|
|
|
|
txtNoBorders: 'No Borders'
|
|
}, Common.UI.ComboBorderSizeEditable || {}));
|
|
}); |