Github user robertkowalski commented on a diff in the pull request: https://github.com/apache/couchdb-fauxton/pull/651#discussion_r55552118 --- Diff: app/addons/documents/index-editor/actions.js --- @@ -15,142 +15,282 @@ define([ 'api', 'addons/documents/resources', 'addons/documents/index-editor/actiontypes', - 'addons/documents/index-results/actions' + 'addons/documents/index-results/actions', + 'addons/documents/sidebar/actions', + 'addons/documents/sidebar/actiontypes' ], -function (app, FauxtonAPI, Documents, ActionTypes, IndexResultsActions) { +function (app, FauxtonAPI, Documents, ActionTypes, IndexResultsActions, SidebarActions, SidebarActionTypes) { - var ActionHelpers = { - findDesignDoc: function (designDocs, designDocId) { - return _.find(designDocs, function (doc) { - return doc.id === designDocId; - }).dDocModel(); - } - }; + function selectReduceChanged (reduceOption) { + FauxtonAPI.dispatch({ + type: ActionTypes.SELECT_REDUCE_CHANGE, + reduceSelectedOption: reduceOption + }); + } - return { - //helpers are added here for use in testing actions - helpers: ActionHelpers, + function changeViewName (name) { + FauxtonAPI.dispatch({ + type: ActionTypes.VIEW_NAME_CHANGE, + name: name + }); + } - selectReduceChanged: function (reduceOption) { - FauxtonAPI.dispatch({ - type: ActionTypes.SELECT_REDUCE_CHANGE, - reduceSelectedOption: reduceOption - }); - }, + function editIndex (options) { + FauxtonAPI.dispatch({ + type: ActionTypes.EDIT_INDEX, + options: options + }); + } - changeViewName: function (name) { - FauxtonAPI.dispatch({ - type: ActionTypes.VIEW_NAME_CHANGE, - name: name - }); - }, + function clearIndex () { + FauxtonAPI.dispatch({ type: ActionTypes.CLEAR_INDEX }); + } - editIndex: function (options) { - FauxtonAPI.dispatch({ - type: ActionTypes.EDIT_INDEX, - options: options - }); - }, + function fetchDesignDocsBeforeEdit (options) { + options.designDocs.fetch({reset: true}).then(function () { + this.editIndex(options); + }.bind(this)); + } - clearIndex: function () { - FauxtonAPI.dispatch({ type: ActionTypes.CLEAR_INDEX }); - }, + function saveView (viewInfo) { + var designDoc = viewInfo.designDoc; + designDoc.setDdocView(viewInfo.viewName, viewInfo.map, viewInfo.reduce); - fetchDesignDocsBeforeEdit: function (options) { - options.designDocs.fetch({reset: true}).then(function () { - this.editIndex(options); - }.bind(this)); - }, + FauxtonAPI.addNotification({ + msg: 'Saving View...', + type: 'info', + clear: true + }); - saveView: function (viewInfo) { - var designDoc = viewInfo.designDoc; - designDoc.setDdocView(viewInfo.viewName, viewInfo.map, viewInfo.reduce); + // if the view name just changed and it's in the SAME design doc, remove the old one before saving the doc + if (viewInfo.originalDesignDocName === viewInfo.designDocId && viewInfo.originalViewName !== viewInfo.viewName) { + designDoc.removeDdocView(viewInfo.originalViewName); + } + designDoc.save().then(function () { FauxtonAPI.addNotification({ - msg: "Saving View...", - type: "info", + msg: 'View Saved.', + type: 'success', clear: true }); - designDoc.save().then(function () { - FauxtonAPI.addNotification({ - msg: "View Saved.", - type: "success", - clear: true - }); + // if the user just saved the view to a different design doc, remove the view from the old design doc and + // delete if it's empty + if (viewInfo.originalDesignDocName !== viewInfo.designDocId) { + var oldDesignDoc = findDesignDoc(viewInfo.designDocs, viewInfo.originalDesignDocName); + safeDeleteView(oldDesignDoc, viewInfo.designDocs, viewInfo.database, viewInfo.originalViewName); + } - if (_.any([viewInfo.designDocChanged, viewInfo.hasViewNameChanged, viewInfo.newDesignDoc, viewInfo.newView])) { - FauxtonAPI.dispatch({ type: ActionTypes.VIEW_SAVED }); - var fragment = FauxtonAPI.urls('view', 'showNewlySavedView', viewInfo.database.safeID(), designDoc.safeID(), app.utils.safeURLName(viewInfo.viewName)); - FauxtonAPI.navigate(fragment, { trigger: true }); - } else { - this.updateDesignDoc(designDoc); - } - - // this can be removed after the Views are on their own page - IndexResultsActions.reloadResultsList(); - }.bind(this)); - }, + if (viewInfo.designDocId === 'new-doc') { + addDesignDoc(designDoc); + } - updateDesignDoc: function (designDoc) { - FauxtonAPI.dispatch({ - type: ActionTypes.VIEW_UPDATE_DESIGN_DOC, - designDoc: designDoc.toJSON() - }); - }, + FauxtonAPI.dispatch({ type: ActionTypes.VIEW_SAVED }); + var fragment = FauxtonAPI.urls('view', 'showView', viewInfo.database.safeID(), designDoc.safeID(), app.utils.safeURLName(viewInfo.viewName)); + FauxtonAPI.navigate(fragment, { trigger: true }); + }); + } - deleteView: function (options) { - var viewName = options.viewName; - var database = options.database; - var designDoc = ActionHelpers.findDesignDoc(options.designDocs, options.designDocId); - var promise; + function addDesignDoc (designDoc) { + FauxtonAPI.dispatch({ + type: ActionTypes.VIEW_ADD_DESIGN_DOC, + designDoc: designDoc.toJSON() + }); + } - designDoc.removeDdocView(viewName); + function deleteView (options) { - if (designDoc.hasViews()) { - promise = designDoc.save(); - } else { - promise = designDoc.destroy(); - } + var onSuccess = function () { - promise.then(function () { - var url = FauxtonAPI.urls('allDocs', 'app', database.safeID(), '?limit=' + FauxtonAPI.constants.DATABASES.DOCUMENT_LIMIT); + // if the user was on the index that was just deleted, redirect them back to all docs + if (options.isOnIndex) { + var url = FauxtonAPI.urls('allDocs', 'app', options.database.safeID(), '?limit=' + FauxtonAPI.constants.DATABASES.DOCUMENT_LIMIT); FauxtonAPI.navigate(url); - FauxtonAPI.triggerRouteEvent('reloadDesignDocs'); - }); - }, + } + + FauxtonAPI.triggerRouteEvent('reloadDesignDocs'); - updateMapCode: function (code) { - FauxtonAPI.dispatch({ - type: ActionTypes.VIEW_UPDATE_MAP_CODE, - code: code + FauxtonAPI.addNotification({ + msg: 'The <code>' + options.indexName + '</code> view has been deleted.', --- End diff -- we need to escape `options.indexName` to make sure to open no XSS vector something like ```js 'The <code>' + _.escape(options.indexName) + '</code> view has been deleted.', ```
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---