/*jslint indent: 2, stupid: true, nomen: true, maxlen: 100, sloppy: true, vars: true, white: true, plusplus: true */ /*global require, window, exports, Backbone, EJS, $, arangoHelper */ var collectionView = Backbone.View.extend({ el: '#modalPlaceholder', initialize: function () { }, template: new EJS({url: 'js/templates/collectionView.ejs'}), render: function() { var self = this; $(this.el).html(this.template.text); $('#change-collection').modal('show'); $('#change-collection').on('hidden', function () { }); $('#change-collection').on('shown', function () { $('#change-collection-name').focus(); }); this.fillModal(); $('.modalTooltips, .glyphicon-info-sign').tooltip({ placement: "top" }); $('#collectionTab a').click(function (e) { e.preventDefault(); $(this).tab('show'); if ($(this).attr('href') === '#editIndex') { self.resetIndexForms(); } }); return this; }, events: { "click #save-modified-collection" : "saveModifiedCollection", "hidden #change-collection" : "hidden", "click #delete-modified-collection" : "deleteCollection", "click #load-modified-collection" : "loadCollection", "click #unload-modified-collection" : "unloadCollection", "click #confirmDeleteCollection" : "confirmDeleteCollection", "click #abortDeleteCollection" : "abortDeleteCollection", "keydown #change-collection-name" : "listenKey", "keydown #change-collection-size" : "listenKey", "click #editIndex .glyphicon-plus-sign" : "toggleNewIndexView", "click #editIndex .glyphicon-remove-circle" : "toggleNewIndexView", "change #newIndexType" : "selectIndexType", "click #createIndex" : "createIndex", "click .deleteIndex" : "deleteIndex" }, listenKey: function(e) { if (e.keyCode === 13) { this.saveModifiedCollection(); } }, hidden: function () { window.App.navigate("#", {trigger: true}); }, toggleNewIndexView: function () { $('#indexEditView').toggle("fast"); $('#newIndexView').toggle("fast"); this.resetIndexForms(); }, selectIndexType: function () { $('.newIndexClass').hide(); var type = $('#newIndexType').val(); $('#newIndexType'+type).show(); }, fillModal: function() { try { this.myCollection = window.arangoCollectionsStore.get(this.options.colId).attributes; } catch (e) { // in case the collection cannot be found or something is not present (e.g. after a reload) window.App.navigate("#"); return; } $('#change-collection-name').val(this.myCollection.name); $('#change-collection-id').text(this.myCollection.id); $('#change-collection-type').text(this.myCollection.type); $('#change-collection-status').text(this.myCollection.status); if (this.myCollection.status === 'unloaded') { $('#colFooter').prepend( '' ); $('#collectionSizeBox').hide(); $('#collectionSyncBox').hide(); // } else if (this.myCollection.status === 'loaded') { $('#colFooter').prepend( '' ); var data = window.arangoCollectionsStore.getProperties(this.options.colId, true); this.fillLoadedModal(data); } }, stringToArray: function (fieldString) { var fields = []; fieldString.split(',').forEach(function(field){ field = field.replace(/(^\s+|\s+$)/g,''); if (field !== '') { fields.push(field); } }); return fields; }, checkboxToValue: function (id) { var checked = $(id).is('checked'); return checked; }, createIndex: function (e) { //e.preventDefault(); var self = this; var collection = this.myCollection.name; var indexType = $('#newIndexType').val(); var result; var postParameter = {}; switch(indexType) { case 'Cap': var size = parseInt($('#newCapSize').val(), 10) || 0; var byteSize = parseInt($('#newCapByteSize').val(), 10) || 0; postParameter = { type: 'cap', size: size, byteSize: byteSize }; break; case 'Geo': //HANDLE ARRAY building var fields = $('#newGeoFields').val(); var geoJson = self.checkboxToValue('#newGeoJson'); var constraint = self.checkboxToValue('#newGeoConstraint'); var ignoreNull = self.checkboxToValue('#newGeoIgnoreNull'); postParameter = { type: 'geo', fields: self.stringToArray(fields), geoJson: geoJson, constraint: constraint, ignoreNull: ignoreNull }; break; case 'Hash': var fields = $('#newHashFields').val(); var unique = self.checkboxToValue('#newHashUnique'); postParameter = { type: 'hash', fields: self.stringToArray(fields), unique: unique }; break; case 'Fulltext': var fields = ($('#newFulltextFields').val()); var minLength = parseInt($('#newFulltextMinLength').val(), 10) || 0; postParameter = { type: 'fulltext', fields: self.stringToArray(fields), minLength: minLength }; break; case 'Skiplist': var fields = $('#newSkiplistFields').val(); var unique = self.checkboxToValue('#newSkiplistUnique'); postParameter = { type: 'skiplist', fields: self.stringToArray(fields), unique: unique }; break; } result = window.arangoCollectionsStore.createIndex(collection, postParameter); if (result === true) { $('#collectionEditIndexTable tr').remove(); self.getIndex(); self.toggleNewIndexView(); self.resetIndexForms(); } else { //notification for user missing } }, resetIndexForms: function () { $('#change-collection input').val('').prop("checked", false); $('#newIndexType').val('Cap').prop('selected',true); }, deleteIndex: function (e) { var collection = this.myCollection.name; var id = $(e.currentTarget).parent().parent().first().children().first().text();; var result = window.arangoCollectionsStore.deleteIndex(collection, id); if (result === true) { $(e.currentTarget).parent().parent().remove(); } else { alert("ERROR"); } }, getIndex: function () { this.index = window.arangoCollectionsStore.getIndex(this.options.colId, true); var cssClass = 'collectionInfoTh modal-text'; if (this.index) { var fieldString = ''; var indexId = ''; var actionString = ''; $.each(this.index.indexes, function(k,v) { if (v.type === 'primary' || v.type === 'edge') { actionString = '' } else { actionString = '' } if (v.fields !== undefined) { fieldString = v.fields.join(", "); } //cut index id var position = v.id.indexOf('/'); var indexId = v.id.substr(position+1, v.id.length); $('#collectionEditIndexTable').append( '