UNPKG

backgrid-select2-cell

Version:

Backgrid.js extension to extend the core SelectCell using select2.

49 lines (44 loc) 2.42 kB
/* backgrid-select2-cell http://github.com/wyuenho/backgrid Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors Licensed under the MIT @license. */ !function(a,b){"function"==typeof define&&define.amd? // AMD define(["underscore","backgrid"],b):"object"==typeof exports?( // CommonJS require("select2"),module.exports=b(require("underscore"),require("backgrid"))): // Browser globals b(a._,a.Backgrid)}(this,function(a,b){"use strict";var c={},d=c.Select2CellEditor=b.Extension.Select2CellEditor=b.SelectCellEditor.extend({/** @property */ events:{change:"save"},/** @property */ select2Options:{openOnEnter:!1},initialize:function(){b.SelectCellEditor.prototype.initialize.apply(this,arguments),this.close=a.bind(this.close,this)},/** Sets the options for `select2`. Called by the parent Select2Cell during edit mode. */ setSelect2Options:function(b){this.select2Options=a.extend(b||{})},/** Renders a `select2` select box instead of the default `<select>` HTML element using the supplied options from #select2Options. @chainable */ render:function(){return b.SelectCellEditor.prototype.render.apply(this,arguments),this.$el.select2(this.select2Options),this},/** Attach event handlers to the select2 box and focus it. */ postRender:function(){var a=this;a.$el.on("select2:close",function(b){b.type="blur",a.close(b)}).select2("focus")},remove:function(){return this.$el.select2("destroy"),b.SelectCellEditor.prototype.remove.apply(this,arguments)}});/** Select2Cell is a cell class that renders a `select2` select box during edit mode. @class Backgrid.Extension.Select2Cell @extends Backgrid.SelectCell */ return c.Select2Cell=b.Extension.Select2Cell=b.SelectCell.extend({/** @property */ className:"select2-cell",/** @property */ editor:d,/** @property */ select2Options:null,/** Initializer. @param {Object} options @param {Backbone.Model} options.model @param {Backgrid.Column} options.column @param {Object} [options.select2Options] @throws {TypeError} If `optionsValues` is undefined. */ initialize:function(a){b.SelectCell.prototype.initialize.apply(this,arguments),this.select2Options=a.select2Options||this.select2Options,this.listenTo(this.model,"backgrid:edit",function(a,b,c,d){b.get("name")==this.column.get("name")&&d.setSelect2Options(this.select2Options)})}}),c});