this.recline = this.recline || {}; // Views module following classic module pattern recline.View = function($) { var my = {}; my.DataExplorer = Backbone.View.extend({ tagName: 'div', className: 'data-explorer', template: ' \
\ \ ', events: { 'change input[name="nav-toggle"]': 'navChange' }, initialize: function() { this.el = $(this.el); this.render(); this.$dataViewContainer = this.el.find('.data-view-container'); var self = this; // retrieve basic data like headers etc // note this.model and dataset returned are the same this.model.fetch().then(function(dataset) { // initialize of dataTable calls render self.dataTable = new my.DataTable({ model: dataset }); self.flotGraph = new my.FlotGraph({ model: dataset }); self.flotGraph.el.hide(); self.$dataViewContainer.append(self.dataTable.el) self.$dataViewContainer.append(self.flotGraph.el); }); }, render: function() { $(this.el).html($(this.template)); }, navChange: function(e) { // TODO: really ugly and will not scale to more widgets ... var widgetToShow = $(e.target).val(); if (widgetToShow == 'datatable') { this.flotGraph.el.hide(); this.dataTable.el.show(); } else if (widgetToShow == 'graph') { this.flotGraph.el.show(); this.dataTable.el.hide(); // Have to call this here // If you attempt to render with flot when graph is hidden / invisible flot will complain with // Invalid dimensions for plot, width = 0, height = 0 // (Could hack this by moving plot left -1000 or similar ...) this.flotGraph.createPlot(); } } }); // DataTable provides a tabular view on a Dataset. // // Initialize it with a recline.Dataset object. my.DataTable = Backbone.View.extend({ tagName: "div", className: "data-table-container", initialize: function() { this.el = $(this.el); var self = this; this.model.getRows().then(function(documentList) { self._currentDocuments = documentList; self.render() }); this.state = {}; // this is nasty. Due to fact that .menu element is not inside this view but is elsewhere in DOM $('.menu li a').live('click', function(e) { // self.onMenuClick(e).apply(self); self.onMenuClick(e); }); }, events: { // see initialize // 'click .menu li': 'onMenuClick', 'click .column-header-menu': 'onColumnHeaderClick', 'click .row-header-menu': 'onRowHeaderClick', 'click .data-table-cell-edit': 'onEditClick', // cell editor 'click .data-table-cell-editor .okButton': 'onEditorOK', 'click .data-table-cell-editor .cancelButton': 'onEditorCancel' }, showDialog: function(template, data) { if (!data) data = {}; util.show('dialog'); util.render(template, 'dialog-content', data); util.observeExit($('.dialog-content'), function() { util.hide('dialog'); }) $('.dialog').draggable({ handle: '.dialog-header', cursor: 'move' }); }, // ====================================================== // Column and row menus onColumnHeaderClick: function(e) { this.state.currentColumn = $(e.target).siblings().text(); util.position('menu', e); util.render('columnActions', 'menu'); }, onRowHeaderClick: function(e) { this.state.currentRow = $(e.target).parents('tr:first').attr('data-id'); util.position('menu', e); util.render('rowActions', 'menu'); }, onMenuClick: function(e) { var self = this; e.preventDefault(); var actions = { bulkEdit: function() { self.showDialog('bulkEdit', {name: self.state.currentColumn}) }, transform: function() { showDialog('transform') }, csv: function() { window.location.href = app.csvUrl }, json: function() { window.location.href = "_rewrite/api/json" }, urlImport: function() { showDialog('urlImport') }, pasteImport: function() { showDialog('pasteImport') }, uploadImport: function() { showDialog('uploadImport') }, deleteColumn: function() { var msg = "Are you sure? This will delete '" + self.state.currentColumn + "' from all documents."; // TODO: alert('This function needs to be re-implemented'); return; if (confirm(msg)) costco.deleteColumn(self.state.currentColumn); }, deleteRow: function() { // TODO: alert('This function needs to be re-implemented'); return; var doc = _.find(app.cache, function(doc) { return doc._id === app.currentRow }); doc._deleted = true; costco.uploadDocs([doc]).then( function(updatedDocs) { util.notify("Row deleted successfully"); recline.initializeTable(app.offset); }, function(err) { util.notify("Errorz! " + err) } ) } } util.hide('menu'); actions[$(e.target).attr('data-action')](); }, // ====================================================== // Cell Editor onEditClick: function(e) { var editing = this.el.find('.data-table-cell-editor-editor'); if (editing.length > 0) { editing.parents('.data-table-cell-value').html(editing.text()).siblings('.data-table-cell-edit').removeClass("hidden"); } $(e.target).addClass("hidden"); var cell = $(e.target).siblings('.data-table-cell-value'); cell.data("previousContents", cell.text()); util.render('cellEditor', cell, {value: cell.text()}); }, onEditorOK: function(e) { var cell = $(e.target); var rowId = cell.parents('tr').attr('data-id'); var header = cell.parents('td').attr('data-header'); var newValue = cell.parents('.data-table-cell-editor').find('.data-table-cell-editor-editor)').val(); // TODO: alert('Update: ' + header + ' with value ' + newValue + '(But no save as not yet operational'); return; var doc = _.find(app.cache, function(cacheDoc) { return cacheDoc._id === rowId; }); doc[header] = newValue; util.notify("Updating row...", {persist: true, loader: true}); costco.updateDoc(doc).then(function(response) { util.notify("Row updated successfully"); recline.initializeTable(); }) }, onEditorCancel: function(e) { var cell = $(e.target).parents('.data-table-cell-value'); cell.html(cell.data('previousContents')).siblings('.data-table-cell-edit').removeClass("hidden"); }, // ====================================================== // Core Templating toTemplateJSON: function() { var modelData = this.model.toJSON() modelData.rows = _.map(this._currentDocuments.models, function(doc) { var cellData = _.map(modelData.headers, function(header) { return {header: header, value: doc.get(header)} }) return { id: 'xxx', cells: cellData } }) modelData.notEmpty = ( modelData.headers.length > 0 ) return modelData; }, render: function() { var template = $( ".dataTableTemplate:first" ).html() , htmls = $.mustache(template, this.toTemplateJSON()) ; $(this.el).html(htmls); return this; } }); // DataTableRow View for rendering an individual document. // // Since we want this to update in place it is up to creator to provider the element to attach to. // In addition you must pass in a headers in the constructor options. This should be list of headers for the DataTable. my.DataTableRow = Backbone.View.extend({ initialize: function(options) { this._headers = options.headers; this.el = $(this.el); }, template: ' \