Commit 6440efb1 authored by Jan Köster's avatar Jan Köster
Browse files

overwritre templates

parent ec3db0d0
Loading
Loading
Loading
Loading
+4 −4
Changes for editor/html/js/api.js: 4 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -208,12 +208,12 @@ var EditorApi = (function() {
            return request('POST', '/api/document/import-xml', payload);
        },

        saveDocument: function(name, group) {
            return request('POST', '/api/document/save', { name: name, group: group || '' });
        saveDocument: function(name, group, overwrite) {
            return request('POST', '/api/document/save', { name: name, group: group || '', overwrite: !!overwrite });
        },

        saveDocumentAs: function(name, group) {
            return request('POST', '/api/document/save-as', { name: name, group: group || '' });
        saveDocumentAs: function(name, group, overwrite) {
            return request('POST', '/api/document/save-as', { name: name, group: group || '', overwrite: !!overwrite });
        },

        listTemplates: function() {
+27 −4
Changes for editor/html/js/editor.js: 27 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -193,7 +193,9 @@
            if (name === 'Untitled') {
                openSaveDialog('save', '', currentDocGroup);
            } else {
                EditorApi.saveDocument(name, currentDocGroup).then(function(resp) {
                saveWithConflictConfirm(function(overwrite) {
                    return EditorApi.saveDocument(name, currentDocGroup, overwrite);
                }, function(resp) {
                    document.getElementById('doc-name').textContent = resp.name;
                    currentDocGroup = resp.group || '';
                });
@@ -353,15 +355,36 @@

    // --- Dialog bindings ---

    // Runs a save request; if the server reports a name/group conflict, asks
    // the user for confirmation before retrying the same request with
    // overwrite=true.
    function saveWithConflictConfirm(saveFn, onSuccess) {
        saveFn(false).then(function(resp) {
            if (resp && resp.status === 'conflict') {
                if (confirm(I18n.t('I18N_DOC_EXISTS_OVERWRITE',
                        'In dieser Gruppe existiert bereits ein Dokument mit diesem Namen. Überschreiben?'))) {
                    saveFn(true).then(onSuccess).catch(function(err) {
                        alert(I18n.t('I18N_ACTION_FAILED', 'Aktion fehlgeschlagen') + ': ' + ((err && err.error) || ''));
                    });
                }
                return;
            }
            onSuccess(resp);
        }).catch(function(err) {
            alert(I18n.t('I18N_ACTION_FAILED', 'Aktion fehlgeschlagen') + ': ' + ((err && err.error) || ''));
        });
    }

    function bindDialogs() {
        // Save dialog
        document.getElementById('btn-save-confirm').addEventListener('click', function() {
            var name = document.getElementById('save-name').value.trim();
            if (!name) return;
            var group = document.getElementById('save-group').value.trim();
            var savePromise = saveDialogMode === 'save-as' ?
                EditorApi.saveDocumentAs(name, group) : EditorApi.saveDocument(name, group);
            savePromise.then(function(resp) {
            saveWithConflictConfirm(function(overwrite) {
                return saveDialogMode === 'save-as' ?
                    EditorApi.saveDocumentAs(name, group, overwrite) : EditorApi.saveDocument(name, group, overwrite);
            }, function(resp) {
                document.getElementById('doc-name').textContent = resp.name;
                currentDocGroup = resp.group || '';
                document.getElementById('save-dialog').close();
+45 −6
Changes for editor/src/webedit_api.cpp: 45 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -2138,11 +2138,13 @@ void webedit::Api::handleSaveDocument(libhttppp::HttpRequest &curreq,
        std::string body = getRequestBody(curreq);
        req = json_tokener_parse(body.c_str());

        json_object *nameObj = nullptr, *groupObj = nullptr;
        json_object *nameObj = nullptr, *groupObj = nullptr, *overwriteObj = nullptr;
        if (req) {
            json_object_object_get_ex(req, "name", &nameObj);
            json_object_object_get_ex(req, "group", &groupObj);
            json_object_object_get_ex(req, "overwrite", &overwriteObj);
        }
        const bool overwrite = overwriteObj && json_object_get_boolean(overwriteObj);

        auto &doc = getDocState(sessionid);
        std::lock_guard<std::mutex> lk(doc.mtx);
@@ -2162,7 +2164,25 @@ void webedit::Api::handleSaveDocument(libhttppp::HttpRequest &curreq,
        _session.getData(sessionid, "domain", domain);

        if (doc.currentDocId.empty()) {
            std::string existingId;
            const bool hasConflict = _db.findDocumentIdByNameGroup(uid, domain, doc.currentDocName,
                                                                    doc.currentDocGroup, existingId);
            if (hasConflict && !overwrite) {
                json_object *resp = json_object_new_object();
                json_object_object_add(resp, "status", json_object_new_string("conflict"));
                json_object_object_add(resp, "id", json_object_new_string(existingId.c_str()));
                json_object_object_add(resp, "name", json_object_new_string(doc.currentDocName.c_str()));
                json_object_object_add(resp, "group", json_object_new_string(doc.currentDocGroup.c_str()));
                sendJson(curreq, resp);
                json_object_put(resp);
                if (req) json_object_put(req);
                return;
            }
            if (hasConflict) {
                docId = _db.updateDocument(uid, existingId, doc.currentDocName, xml, author, doc.currentDocGroup);
            } else {
                docId = _db.saveDocument(uid, domain, doc.currentDocName, xml, author, doc.currentDocGroup);
            }
            doc.currentDocId = docId;
        } else {
            docId = _db.updateDocument(uid, doc.currentDocId, doc.currentDocName, xml, author, doc.currentDocGroup);
@@ -2193,11 +2213,13 @@ void webedit::Api::handleSaveDocumentAs(libhttppp::HttpRequest &curreq,
        std::string body = getRequestBody(curreq);
        req = json_tokener_parse(body.c_str());

        json_object *nameObj = nullptr, *groupObj = nullptr;
        json_object *nameObj = nullptr, *groupObj = nullptr, *overwriteObj = nullptr;
        if (req) {
            json_object_object_get_ex(req, "name", &nameObj);
            json_object_object_get_ex(req, "group", &groupObj);
            json_object_object_get_ex(req, "overwrite", &overwriteObj);
        }
        const bool overwrite = overwriteObj && json_object_get_boolean(overwriteObj);

        if (!nameObj || std::string(json_object_get_string(nameObj)).empty()) {
            if (req) json_object_put(req);
@@ -2219,10 +2241,27 @@ void webedit::Api::handleSaveDocumentAs(libhttppp::HttpRequest &curreq,
        _session.getData(sessionid, "uid", uid);
        _session.getData(sessionid, "domain", domain);

        // Always insert a new document, regardless of whether the currently
        // open document was already saved — the open document then becomes
        // this new copy, matching classic "Save As" semantics.
        std::string docId = _db.saveDocument(uid, domain, doc.currentDocName, xml, author, doc.currentDocGroup);
        std::string existingId;
        const bool hasConflict = _db.findDocumentIdByNameGroup(uid, domain, doc.currentDocName,
                                                                doc.currentDocGroup, existingId);
        if (hasConflict && !overwrite) {
            json_object *resp = json_object_new_object();
            json_object_object_add(resp, "status", json_object_new_string("conflict"));
            json_object_object_add(resp, "id", json_object_new_string(existingId.c_str()));
            json_object_object_add(resp, "name", json_object_new_string(doc.currentDocName.c_str()));
            json_object_object_add(resp, "group", json_object_new_string(doc.currentDocGroup.c_str()));
            sendJson(curreq, resp);
            json_object_put(resp);
            if (req) json_object_put(req);
            return;
        }

        // Insert a new document unless we're overwriting an existing one with
        // the same name in the same group — the open document then becomes
        // that document, matching classic "Save As" semantics.
        std::string docId = hasConflict
            ? _db.updateDocument(uid, existingId, doc.currentDocName, xml, author, doc.currentDocGroup)
            : _db.saveDocument(uid, domain, doc.currentDocName, xml, author, doc.currentDocGroup);
        doc.currentDocId = docId;

        json_object *resp = json_object_new_object();