Skip to content

Commit

Permalink
CHG: setItems method
Browse files Browse the repository at this point in the history
  • Loading branch information
farthinker committed Jun 21, 2016
1 parent 5445b01 commit 5dd503f
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 80 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-select",
"version": "2.0.12",
"version": "2.0.13",
"homepage": "https://github.com/mycolorway/simple-select",
"authors": [
"kshift <[email protected]>"
Expand Down
56 changes: 36 additions & 20 deletions lib/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Select = (function(superClass) {
};

Select.prototype._render = function() {
var inputTpl, items;
var inputTpl;
Select._tpl.select = "<div class=\"simple-select\">\n <span class=\"link-expand\" title=\"" + (this._t('all_options')) + "\">\n <i class=\"icon-caret-down\"><span>&#9662;</span></i>\n </span>\n <span class=\"link-clear\" title=\"" + (this._t('clear_selection')) + "\">\n <i class=\"icon-delete\"><span>&#10005;</span></i>\n </span>\n <div class=\"select-list\">\n <div class=\"loading\">" + (this._t('loading')) + "</div>\n </div>\n</div>";
this.el = $(this.opts.el).hide();
this.el.data("select", this);
Expand All @@ -83,7 +83,7 @@ Select = (function(superClass) {
this.input = $(inputTpl).attr("placeholder", this.opts.placeholder || this.el.data('placeholder') || "").prependTo(this.select);
this.list = this.select.find(".select-list");
this.requireSelect = true;
items = this.el.find("option").map((function(_this) {
this.items = this.el.find("option").map((function(_this) {
return function(i, option) {
var $option, label, value;
$option = $(option);
Expand All @@ -99,10 +99,8 @@ Select = (function(superClass) {
}, $option.data());
};
})(this)).get();
if (this.requireSelect) {
this.select.addClass('require-select');
}
return this.setItems(items);
this.generateList();
return this.select.toggleClass('require-select', this.requireSelect);
};

Select.prototype._expand = function(expand) {
Expand Down Expand Up @@ -358,18 +356,12 @@ Select = (function(superClass) {
return this._focused = true;
};

Select.prototype.setItems = function(items) {
var $itemEl, idx, it, item, j, k, len, len1, results1;
if (!$.isArray(items)) {
return;
}
this.items = items;
if (!(items.length > 0)) {
return;
}
this.list.find(".loading, .select-item").remove();
for (j = 0, len = items.length; j < len; j++) {
item = items[j];
Select.prototype.generateList = function() {
var $itemEl, idx, it, item, j, k, len, len1, ref, ref1, results1;
this.list.empty();
ref = this.items;
for (j = 0, len = ref.length; j < len; j++) {
item = ref[j];
$itemEl = $(Select._tpl.item).data(item);
$itemEl.find(".label span").text(item.label);
$itemEl.find(".hint").text(item.hint);
Expand All @@ -378,9 +370,10 @@ Select = (function(superClass) {
this.opts.onItemRender.call(this, $itemEl, item);
}
}
ref1 = this.items;
results1 = [];
for (idx = k = 0, len1 = items.length; k < len1; idx = ++k) {
it = items[idx];
for (idx = k = 0, len1 = ref1.length; k < len1; idx = ++k) {
it = ref1[idx];
if (it._value === this.el.val()) {
this.selectItem(idx);
break;
Expand All @@ -391,6 +384,29 @@ Select = (function(superClass) {
return results1;
};

Select.prototype.setItems = function(items, requireSelect) {
var item, j, len;
if (requireSelect == null) {
requireSelect = true;
}
this.items = items;
this.clearSelection();
this.list.empty();
this.el.empty();
if ($.isArray(items) && items.length > 0) {
this.requireSelect = requireSelect;
this.select.toggleClass('require-select', this.requireSelect);
if (!this.requireSelect) {
this.el.prepend('<option></option>');
}
for (j = 0, len = items.length; j < len; j++) {
item = items[j];
this.el.append("<option value=\"" + item._value + "\">" + item.label + "</option>");
}
return this.generateList();
}
};

Select.prototype.selectItem = function(index) {
var item;
if (!this.items) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-select",
"version": "2.0.12",
"version": "2.0.13",
"description": "a simple select plugin based on Simple Module",
"main": "lib/select.js",
"repository": {
Expand Down
43 changes: 0 additions & 43 deletions spec/index.html

This file was deleted.

37 changes: 22 additions & 15 deletions src/select.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ class Select extends SimpleModule
@list = @select.find ".select-list"

@requireSelect = true

items = @el.find("option").map (i, option) =>
@items = @el.find("option").map (i, option) =>
$option = $(option)
value = $option.attr 'value'
label = $option.text().trim()
Expand All @@ -94,10 +93,8 @@ class Select extends SimpleModule
}, $option.data())
.get()

if @requireSelect
@select.addClass('require-select')

@setItems items
@generateList()
@select.toggleClass 'require-select', @requireSelect

_expand: (expand) ->
if expand is false
Expand Down Expand Up @@ -286,26 +283,36 @@ class Select extends SimpleModule
@_focused = true


setItems: (items) ->
return unless $.isArray(items)
@items = items

return unless items.length > 0
@list.find(".loading, .select-item").remove()

for item in items
generateList: ->
@list.empty()
for item in @items
$itemEl = $(Select._tpl.item).data(item)
$itemEl.find(".label span").text(item.label)
$itemEl.find(".hint").text(item.hint)

@list.append $itemEl
@opts.onItemRender.call(@, $itemEl, item) if $.isFunction @opts.onItemRender

for it, idx in items
for it, idx in @items
if it._value is @el.val()
@selectItem idx
break

setItems: (items, requireSelect = true) ->
@items = items
@clearSelection()
@list.empty()
@el.empty()

if $.isArray(items) && items.length > 0
@requireSelect = requireSelect
@select.toggleClass 'require-select', @requireSelect
@el.prepend('<option></option>') unless @requireSelect
for item in items
@el.append("<option value=\"#{item._value}\">#{item.label}</option>")

@generateList()

selectItem: (index) ->
return unless @items

Expand Down

0 comments on commit 5dd503f

Please sign in to comment.