diff --git a/.meteor/versions b/.meteor/versions index 3dd43c7c04497f08e4c50646bca3bba502918f66..2c2032779615f4dfcea5cd8b0c56559fd17d0e08 100644 --- a/.meteor/versions +++ b/.meteor/versions @@ -42,7 +42,7 @@ iron:core@1.0.11 iron:dynamic-template@1.0.12 iron:layout@1.0.12 iron:location@1.0.11 -iron:middleware-stack@1.0.11 +iron:middleware-stack@1.1.0 iron:router@1.0.12 iron:url@1.0.11 jquery@1.11.4 diff --git a/client/helper.js b/client/helper.js index a81547820d08e1f08e6509a2c965153feffd999a..55e38576b315da16f505671c85ff82e7cfa1d94e 100644 --- a/client/helper.js +++ b/client/helper.js @@ -114,6 +114,25 @@ Template.convertAndCheckJSON = function (json) { return result; }; +Template.convertAndCheckJSONAsArray = function (json) { + if (json == "") return []; + var result = []; + try { + result = JSON.parse(json); + } + catch (err) { + throw err.message; + } + + if (!$.isArray(result)) { + var res = []; + res.push(result); + return res; + } + + return result; +}; + Template.checkCodeMirrorSelectorForOption = function (option, result, optionEnum) { if ($.inArray(option, Session.get(Template.strSessionSelectedOptions)) != -1) { var val = Template.selector.getValue(); diff --git a/client/views/pages/browse_collection/browse_collection.html b/client/views/pages/browse_collection/browse_collection.html index a6a1623318f4001c027fccf7b18284f2799af635..2a1fed354449c157dd053d0a822694643e72aac5 100644 --- a/client/views/pages/browse_collection/browse_collection.html +++ b/client/views/pages/browse_collection/browse_collection.html @@ -70,6 +70,18 @@ + + diff --git a/client/views/pages/browse_collection/browse_collection.js b/client/views/pages/browse_collection/browse_collection.js index 0858f3f82235803fdbaf024e0a5855679224aff5..80192876c6b3533fd44980279d86403af81a49e8 100644 --- a/client/views/pages/browse_collection/browse_collection.js +++ b/client/views/pages/browse_collection/browse_collection.js @@ -41,10 +41,16 @@ Template.browseCollection.onRendered(function () { if (Session.get(Template.strSessionSelectedQuery) != QUERY_TYPES.FIND) { Template.changeConvertOptionsVisibility(false); } + Template.browseCollection.clearQueryIfAdmin(); }); Template.browseCollection.events({ + 'click #btnSaveFindFindOne': function (e) { + e.preventDefault(); + Template.browseCollection.saveEditor(); + }, + 'click #btnShowQueryHistories': function () { $('#queryHistoriesModal').modal('show'); }, @@ -206,6 +212,23 @@ Template.browseCollection.setResult = function (result, queryInfo, queryParams, var tabID = $(this).parents('a').attr('href'); $(this).parents('li').remove(); $(tabID).remove(); + + if (resultTabs.find('li').length == 0 || resultTabs.find('li.active').length == 0) { + $('#divBrowseCollectionFooter').hide(); + } + }); + + resultTabs.on('shown.bs.tab', function (e) { + var activeTabText = $(e.target).text(); + var activeTabQueryInfo = activeTabText.substring(0, activeTabText.indexOf(' ')); + // see #104 + + if (activeTabQueryInfo == 'findOne' || activeTabQueryInfo == 'find') { + $('#divBrowseCollectionFooter').show(); + } else { + $('#divBrowseCollectionFooter').hide(); + } + }); // show last tab @@ -218,6 +241,8 @@ Template.browseCollection.setResult = function (result, queryInfo, queryParams, if (saveHistory) { Template.browseCollection.saveQueryHistory(queryInfo, queryParams); } + + }; Template.browseCollection.saveQueryHistory = function (queryInfo, queryParams) { @@ -340,4 +365,72 @@ Template.browseCollection.getEditor = function (tabID) { } return tabView.data('jsoneditor'); -}; \ No newline at end of file +}; + +Template.browseCollection.getActiveEditorValue = function () { + var resultTabs = $('#resultTabs'); + var resultContents = $('#resultTabContents'); + + var whichIsDisplayed = Template.browseCollection.getWhichResultViewShowing(); + if (whichIsDisplayed == 'aceEditor') { + var foundAceEditor = resultContents.find('div.active').find('pre').attr('id'); + if (foundAceEditor) { + return ace.edit(foundAceEditor).getValue(); + } + } + else if (whichIsDisplayed == 'jsonEditor') { + var tabId = resultTabs.find('li.active').find('a').attr('href'); + if ($(tabId).data('jsoneditor')) { + return JSON.stringify($(tabId).data('jsoneditor').get()); + } + } +}; + +Template.browseCollection.saveEditor = function () { + var convertedDocs; + try { + convertedDocs = Template.convertAndCheckJSONAsArray(Template.browseCollection.getActiveEditorValue()); + } + catch (e) { + toastr.error('Syntax error, can not save document(s): ' + e); + return; + } + + swal({ + title: "Are you sure ?", + text: convertedDocs.length + ' document(s) will be updated (_id field is unchangeable), are you sure ?', + type: "info", + showCancelButton: true, + confirmButtonColor: "#DD6B55", + confirmButtonText: "Yes!", + cancelButtonText: "No" + }, function (isConfirm) { + if (isConfirm) { + var l = $('#btnSaveFindFindOne').ladda(); + l.ladda('start'); + + var selectedCollection = Session.get(Template.strSessionSelectedCollection); + var convertIds = $('#aConvertObjectIds').iCheck('update')[0].checked; + var convertDates = $('#aConvertIsoDates').iCheck('update')[0].checked; + var i = 0; + _.each(convertedDocs, function (doc) { + if (doc._id) { + Meteor.call("updateOne", selectedCollection, {_id: doc._id}, doc, {}, convertIds, convertDates, function (err, result) { + if (err || result.error) { + Template.showMeteorFuncError(err, result, "Couldn't update one of the documents"); + Ladda.stopAll(); + } else { + if ((i++) == (convertedDocs.length - 1)) { + // last time + toastr.success('Successfully updated document(s)'); + Ladda.stopAll(); + } + } + }); + } + }); + } + }); + +}; +