2016-07-24 16:45:11 -04:00
|
|
|
(function() {
|
|
|
|
var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
|
|
|
|
|
|
|
|
this.TemplateSelector = (function() {
|
|
|
|
function TemplateSelector(opts) {
|
|
|
|
var ref;
|
|
|
|
if (opts == null) {
|
|
|
|
opts = {};
|
|
|
|
}
|
|
|
|
this.onClick = bind(this.onClick, this);
|
|
|
|
this.dropdown = opts.dropdown, this.data = opts.data, this.pattern = opts.pattern, this.wrapper = opts.wrapper, this.editor = opts.editor, this.fileEndpoint = opts.fileEndpoint, this.$input = (ref = opts.$input) != null ? ref : $('#file_name');
|
2016-06-24 15:43:46 -04:00
|
|
|
this.dropdownIcon = $('.fa-chevron-down', this.dropdown);
|
2016-07-24 16:45:11 -04:00
|
|
|
this.buildDropdown();
|
|
|
|
this.bindEvents();
|
|
|
|
this.onFilenameUpdate();
|
2016-09-09 10:11:37 -04:00
|
|
|
|
|
|
|
this.autosizeUpdateEvent = document.createEvent('Event');
|
|
|
|
this.autosizeUpdateEvent.initEvent('autosize:update', true, false);
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
TemplateSelector.prototype.buildDropdown = function() {
|
|
|
|
return this.dropdown.glDropdown({
|
|
|
|
data: this.data,
|
|
|
|
filterable: true,
|
|
|
|
selectable: true,
|
|
|
|
toggleLabel: this.toggleLabel,
|
|
|
|
search: {
|
|
|
|
fields: ['name']
|
|
|
|
},
|
|
|
|
clicked: this.onClick,
|
|
|
|
text: function(item) {
|
|
|
|
return item.name;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
TemplateSelector.prototype.bindEvents = function() {
|
|
|
|
return this.$input.on('keyup blur', (function(_this) {
|
|
|
|
return function(e) {
|
|
|
|
return _this.onFilenameUpdate();
|
|
|
|
};
|
|
|
|
})(this));
|
|
|
|
};
|
|
|
|
|
|
|
|
TemplateSelector.prototype.toggleLabel = function(item) {
|
|
|
|
return item.name;
|
|
|
|
};
|
|
|
|
|
|
|
|
TemplateSelector.prototype.onFilenameUpdate = function() {
|
|
|
|
var filenameMatches;
|
|
|
|
if (!this.$input.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
filenameMatches = this.pattern.test(this.$input.val().trim());
|
|
|
|
if (!filenameMatches) {
|
|
|
|
this.wrapper.addClass('hidden');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return this.wrapper.removeClass('hidden');
|
|
|
|
};
|
|
|
|
|
|
|
|
TemplateSelector.prototype.onClick = function(item, el, e) {
|
|
|
|
e.preventDefault();
|
|
|
|
return this.requestFile(item);
|
|
|
|
};
|
|
|
|
|
2016-06-24 15:43:46 -04:00
|
|
|
TemplateSelector.prototype.requestFile = function(item) {
|
|
|
|
// This `requestFile` method is an abstract method that should
|
|
|
|
// be added by all subclasses.
|
|
|
|
};
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2016-07-26 23:32:10 -04:00
|
|
|
// To be implemented on the extending class
|
|
|
|
// e.g.
|
|
|
|
// Api.gitignoreText item.name, @requestFileSuccess.bind(@)
|
2016-06-24 15:43:46 -04:00
|
|
|
TemplateSelector.prototype.requestFileSuccess = function(file, skipFocus) {
|
2016-07-24 16:45:11 -04:00
|
|
|
this.editor.setValue(file.content, 1);
|
2016-06-24 15:43:46 -04:00
|
|
|
if (!skipFocus) this.editor.focus();
|
2016-09-09 10:11:37 -04:00
|
|
|
|
|
|
|
if (this.editor instanceof jQuery) {
|
|
|
|
this.editor.get(0).dispatchEvent(this.autosizeUpdateEvent);
|
|
|
|
}
|
2016-06-24 15:43:46 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
TemplateSelector.prototype.startLoadingSpinner = function() {
|
|
|
|
this.dropdownIcon
|
|
|
|
.addClass('fa-spinner fa-spin')
|
|
|
|
.removeClass('fa-chevron-down');
|
|
|
|
};
|
|
|
|
|
|
|
|
TemplateSelector.prototype.stopLoadingSpinner = function() {
|
|
|
|
this.dropdownIcon
|
|
|
|
.addClass('fa-chevron-down')
|
|
|
|
.removeClass('fa-spinner fa-spin');
|
2016-07-24 16:45:11 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
return TemplateSelector;
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
}).call(this);
|