2013-10-24 17:25:27 +00:00
|
|
|
(function(context,$) {
|
|
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
context.JK = context.JK || {};
|
2014-06-24 03:26:27 +00:00
|
|
|
context.JK.InstrumentSelectorDeferred = null;
|
2015-05-30 02:04:33 +00:00
|
|
|
context.JK.InstrumentSelector = (function(app, parentSelector) {
|
2013-10-24 17:25:27 +00:00
|
|
|
|
2014-06-01 18:45:17 +00:00
|
|
|
var logger = context.JK.logger;
|
2014-06-24 03:26:27 +00:00
|
|
|
var rest = new context.JK.Rest();
|
2014-06-01 18:45:17 +00:00
|
|
|
var _instruments = []; // will be list of structs: [ {label:xxx, value:yyy}, {...}, ... ]
|
2015-08-18 19:26:41 +00:00
|
|
|
var _instrumentsSorted = [];
|
2014-06-01 18:45:17 +00:00
|
|
|
var _rsvp = false;
|
2015-07-15 15:04:45 +00:00
|
|
|
var _noICheck = false;
|
2015-05-30 02:04:33 +00:00
|
|
|
if (typeof(_parentSelector)=="undefined") {_parentSelector=null}
|
|
|
|
|
var _parentSelector = parentSelector;
|
2014-06-24 03:26:27 +00:00
|
|
|
var deferredInstruments = null;
|
|
|
|
|
var self = this;
|
2013-10-24 17:25:27 +00:00
|
|
|
|
2014-06-01 18:45:17 +00:00
|
|
|
function reset() {
|
2015-05-30 02:04:33 +00:00
|
|
|
$('input[type="checkbox"]', _parentSelector).attr('checked', '');
|
2014-06-01 18:45:17 +00:00
|
|
|
if (_rsvp) {
|
|
|
|
|
$('select.rsvp_count option', _parentSelector).eq(0).prop('selected', true);
|
|
|
|
|
$('select.rsvp_level option', _parentSelector).eq(0).prop('selected', true);
|
2013-10-24 17:25:27 +00:00
|
|
|
}
|
2014-06-01 18:45:17 +00:00
|
|
|
else {
|
|
|
|
|
$('select option', _parentSelector).eq(0).prop('selected', true);
|
2013-10-24 17:25:27 +00:00
|
|
|
}
|
2014-06-01 18:45:17 +00:00
|
|
|
}
|
2013-10-24 17:25:27 +00:00
|
|
|
|
2014-06-01 18:45:17 +00:00
|
|
|
function instrumentsLoaded(response) {
|
|
|
|
|
$.each(response, function (index) {
|
|
|
|
|
_instruments.push({
|
|
|
|
|
id: 'instrument-' + _rsvp + '-' + index,
|
|
|
|
|
value: this.id,
|
|
|
|
|
label: this.description
|
|
|
|
|
});
|
|
|
|
|
});
|
2015-08-18 19:26:41 +00:00
|
|
|
|
|
|
|
|
_instrumentsSorted = _instruments.slice().sort(sortAlpha)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function sortAlpha(a, b) {
|
|
|
|
|
if (a.value == b.value)
|
|
|
|
|
return 0;
|
|
|
|
|
if (a.value < b.value)
|
|
|
|
|
return -1;
|
|
|
|
|
else
|
|
|
|
|
return 1;
|
2014-06-01 18:45:17 +00:00
|
|
|
}
|
|
|
|
|
|
2014-06-18 22:15:56 +00:00
|
|
|
function render(parentSelector, userInstruments) {
|
|
|
|
|
userInstruments = typeof userInstruments !== 'undefined' ? userInstruments : [];
|
|
|
|
|
|
2014-06-01 18:45:17 +00:00
|
|
|
_parentSelector = parentSelector;
|
|
|
|
|
$(parentSelector).empty();
|
|
|
|
|
var template = null;
|
|
|
|
|
if (_rsvp) {
|
|
|
|
|
template = $('#template-instrument-rsvp-option').html();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
template = $('#template-instrument-option').html();
|
2013-10-24 17:25:27 +00:00
|
|
|
}
|
2014-06-18 22:15:56 +00:00
|
|
|
|
|
|
|
|
var userInstrumentList = [];
|
|
|
|
|
$.each(userInstruments, function(index) {
|
|
|
|
|
userInstrumentList.push({
|
|
|
|
|
id: 'instrument-false-' + this.instrument_id.replace(' ', '-'),
|
|
|
|
|
value: this.instrument_id,
|
|
|
|
|
label: this.description,
|
|
|
|
|
level: this.level
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$.each(userInstrumentList, function (index, value) {
|
2014-06-24 03:26:27 +00:00
|
|
|
var instrumentOptionHtml = $(context.JK.fillTemplate(template, value));
|
2014-06-01 18:45:17 +00:00
|
|
|
$(_parentSelector).append(instrumentOptionHtml);
|
2014-06-24 03:26:27 +00:00
|
|
|
context.JK.dropdown(instrumentOptionHtml.find('select'));
|
2014-06-01 18:45:17 +00:00
|
|
|
});
|
2014-06-18 22:15:56 +00:00
|
|
|
|
2014-06-24 03:26:27 +00:00
|
|
|
// do not auto-pick anything for the user currently
|
|
|
|
|
//setSelectedInstruments(userInstrumentList);
|
2014-06-18 22:15:56 +00:00
|
|
|
|
|
|
|
|
$.each(_instruments, function(index, instrument) {
|
|
|
|
|
var isRendered = false;
|
|
|
|
|
$.each(userInstrumentList, function(index, userInstrument) {
|
|
|
|
|
if (instrument.value == userInstrument.value) {
|
|
|
|
|
isRendered = true;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
if (!isRendered) {
|
2014-06-24 03:26:27 +00:00
|
|
|
var instrumentOptionHtml = $(context.JK.fillTemplate(template, instrument));
|
2014-06-18 22:15:56 +00:00
|
|
|
$(_parentSelector).append(instrumentOptionHtml);
|
2014-06-24 03:26:27 +00:00
|
|
|
context.JK.dropdown(instrumentOptionHtml.find('select'));
|
2014-06-18 22:15:56 +00:00
|
|
|
}
|
2014-06-24 03:26:27 +00:00
|
|
|
});
|
|
|
|
|
|
2014-06-01 18:45:17 +00:00
|
|
|
}
|
|
|
|
|
|
2015-08-18 19:26:41 +00:00
|
|
|
function renderDropdown($select, notSelectedString) {
|
|
|
|
|
if(!notSelectedString) {
|
|
|
|
|
notSelectedString = 'Any Instrument'
|
|
|
|
|
}
|
|
|
|
|
$select.empty();
|
|
|
|
|
$select.append('<option value="">' + notSelectedString + '</option>');
|
|
|
|
|
var template = $('#template-instrument-option-simple').html();
|
|
|
|
|
$.each(_instrumentsSorted, function(index, value) {
|
|
|
|
|
// value will be a dictionary entry from _genres:
|
|
|
|
|
// { value: xxx, label: yyy }
|
|
|
|
|
var instrumentOptionHtml = context.JK.fillTemplate(template, value);
|
|
|
|
|
$select.append(instrumentOptionHtml);
|
|
|
|
|
});
|
|
|
|
|
context.JK.dropdown($select);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-06-01 18:45:17 +00:00
|
|
|
function getSelectedInstruments() {
|
|
|
|
|
var selectedInstruments = [];
|
2014-06-04 15:15:12 +00:00
|
|
|
var $selectedVal = $('input[type="checkbox"]:checked', _parentSelector);
|
|
|
|
|
$.each($selectedVal, function (index, value) {
|
|
|
|
|
var id = $(value).attr('session-instrument-id');
|
2015-05-30 02:04:33 +00:00
|
|
|
var name = $('label[for="' + $(value).attr('id') + '"]', _parentSelector).text().trim();
|
2014-06-01 18:45:17 +00:00
|
|
|
if (_rsvp) {
|
|
|
|
|
var count = $('select[session-instrument-id="' + id + '"].rsvp-count', _parentSelector).val();
|
|
|
|
|
var rsvp_level = $('select[session-instrument-id="' + id + '"].rsvp-level', _parentSelector).val();
|
|
|
|
|
selectedInstruments.push({id: id, name: name, count: count, level: rsvp_level});
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
var level = $('select[session-instrument-id="' + id + '"]', _parentSelector).val();
|
|
|
|
|
selectedInstruments.push({id: id, name: name, level: level});
|
|
|
|
|
}
|
|
|
|
|
});
|
2015-07-15 15:04:45 +00:00
|
|
|
|
2014-06-01 18:45:17 +00:00
|
|
|
return selectedInstruments;
|
|
|
|
|
}
|
2013-10-24 17:25:27 +00:00
|
|
|
|
2014-06-01 18:45:17 +00:00
|
|
|
function setSelectedInstruments(instrumentList) {
|
|
|
|
|
if (!instrumentList) {
|
|
|
|
|
return;
|
2015-05-30 02:04:33 +00:00
|
|
|
}
|
|
|
|
|
$.each(instrumentList, function (index, value) {
|
2015-07-15 15:04:45 +00:00
|
|
|
var $item = $('input[type="checkbox"][session-instrument-id="' + value.id + '"]')
|
2014-06-01 18:45:17 +00:00
|
|
|
.attr('checked', 'checked')
|
2015-07-15 15:04:45 +00:00
|
|
|
if(!_noICheck) {
|
|
|
|
|
$item.iCheck({
|
2014-06-01 18:45:17 +00:00
|
|
|
checkboxClass: 'icheckbox_minimal',
|
|
|
|
|
radioClass: 'iradio_minimal',
|
|
|
|
|
inheritClass: true
|
2015-07-15 15:04:45 +00:00
|
|
|
})
|
|
|
|
|
}
|
2014-06-01 18:45:17 +00:00
|
|
|
if (_rsvp) {
|
2015-05-30 02:04:33 +00:00
|
|
|
$('select[session-instrument-id="' + value.id + '"].rsvp-count', _parentSelector).val(value.count);
|
|
|
|
|
$('select[session-instrument-id="' + value.id + '"].rsvp-level', _parentSelector).val(value.level);
|
2014-06-01 18:45:17 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2015-05-30 02:04:33 +00:00
|
|
|
$('select[session-instrument-id="' + value.id + '"]').val(value.level);
|
2014-06-01 18:45:17 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2014-02-10 22:43:09 +00:00
|
|
|
|
2015-07-15 15:04:45 +00:00
|
|
|
function initialize(rsvp, noICheck) {
|
2014-06-01 18:45:17 +00:00
|
|
|
_rsvp = rsvp;
|
2015-07-15 15:04:45 +00:00
|
|
|
_noICheck = noICheck;
|
2014-06-24 03:26:27 +00:00
|
|
|
// XXX; _instruments should be populated in a template, rather than round-trip to server
|
|
|
|
|
if(!context.JK.InstrumentSelectorDeferred) {
|
|
|
|
|
// this dance is to make sure there is only one server request instead of InstrumentSelector instances *
|
|
|
|
|
context.JK.InstrumentSelectorDeferred = rest.getInstruments()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context.JK.InstrumentSelectorDeferred
|
|
|
|
|
.done(function(response) {instrumentsLoaded(response)})
|
|
|
|
|
.fail(app.ajaxError)
|
2013-10-24 17:25:27 +00:00
|
|
|
|
2014-06-01 18:45:17 +00:00
|
|
|
return this;
|
|
|
|
|
}
|
2013-10-24 17:25:27 +00:00
|
|
|
|
2014-06-01 18:45:17 +00:00
|
|
|
this.initialize = initialize;
|
|
|
|
|
this.getSelectedInstruments = getSelectedInstruments;
|
|
|
|
|
this.setSelectedInstruments = setSelectedInstruments;
|
2014-06-24 03:26:27 +00:00
|
|
|
this.render = function() {
|
|
|
|
|
var _args = arguments;
|
|
|
|
|
context.JK.InstrumentSelectorDeferred.done(function(){render.apply(self, _args)})
|
|
|
|
|
}
|
2015-08-18 19:26:41 +00:00
|
|
|
this.renderDropdown = function() {
|
|
|
|
|
var _args = arguments;
|
|
|
|
|
context.JK.InstrumentSelectorDeferred.done(function(){renderDropdown.apply(self, _args)})
|
|
|
|
|
}
|
2014-06-01 18:45:17 +00:00
|
|
|
});
|
2013-10-24 17:25:27 +00:00
|
|
|
|
2014-06-01 18:45:17 +00:00
|
|
|
})(window,jQuery);
|