All Downloads are FREE. Search and download functionalities are using the official Maven repository.

playground.server.pages.js.editor.js Maven / Gradle / Ivy

Go to download

Middleware library based on Vert.x to build advanced JSON/REST communication servers

There is a newer version: 2.1.15
Show newest version
/* Copyright 2013 Laurent Bovet 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

function reformat() {
    try {
        var pos = editor.getCursorPosition();
        var json = JSON.parse(editor.getSession());
        editor.getSession().setValue(JSON.stringify(json, null, 2));
        editor.navigateTo(pos.row, pos.column);
    } catch(err) {
        // ignore
    }
}

function logReq(text) {
    log('
'+text+'
'); } function logOk(text) { log('
'+text+'

'); } function logError(text) { log('
'+$('
').text(text).html()+'

'); } function validateLog(status, message) { if(status === "error") { log(''+message+'

') } else if(status === "warn") { log(''+message+'

') } } function log(html) { $("#log").append(html); $("#log").scrollTop($("#log")[0].scrollHeight); } function get() { logReq("GET "+uri); var p = $.ajax(uri, { dataType:"text", headers: getHeaders() }); p.done( function(data, status, xhr) { $("#title").css({ "font-style": "normal" }); logOk(xhr.status+" "+xhr.statusText); var type=xhr.getResponseHeader("Content-Type"); if(type && type.indexOf("application/json") != -1) { editor.getSession().setMode("ace/mode/json"); editor.getSession().setValue(data); reformat(); validate(uri, "GET/out", JSON.parse(data), validateLog); } else { var mimeType = mimeTypes[type.split(';')[0]]; var path; if(mimeType && type.split(';')[0] != "text/plain") { if(mimeType[2]) { if(binary) { document.getElementById("frame").contentDocument.location.reload(true); } else { setBinary(true, true); } return; } else { if(mimeType[1]) { path=".xml"; } else { path="."+mimeType[0]; } } } else { path=uri.split('?')[0]; } var mode = getModeFromPath(path); if(mode) { editor.getSession().setMode(mode.mode); } else { editor.getSession().setMode("ace/mode/text"); } $("#reformat").hide(); editor.getSession().setValue(data); } }); p.fail( function(xhr, status, err) { $("#title").css({ "font-style": "italic" }); logError(xhr.status+" "+xhr.statusText+"\n"); initEmpty(); }); } function put() { var d = $.Deferred(); if(postMode) { logError("Use Ctrl-Enter to post"); return; } logReq("PUT "+uri); try { validate(uri, "PUT/in", JSON.parse(editor.getSession().getValue()), validateLog) } catch(e) { // ignore } var p = $.ajax(uri, { method: "PUT", data: binary ? blob: editor.getSession().getValue(), contentType: defaultType, processData: false, headers: getHeaders() }); p.done( function(data, status, xhr) { $("#title").css({ "font-style": "normal" }); logOk(xhr.status+" "+xhr.statusText); d.resolve(); }); p.fail( function(xhr, status, err) { logError(xhr.status+" "+xhr.statusText+"\n"+ ((status==400 || status >= 500) ? xhr.responseText : "")); d.reject(); }); return d.promise(); } function post() { logReq("POST "+uri); try { validate(uri, "POST/in", JSON.parse(editor.getSession().getValue()), validateLog) } catch(e) { // ignore } var p = $.ajax(uri, { method: "POST", data: binary ? blob: editor.getSession().getValue(), contentType: defaultType, processData: false, headers: getHeaders() }); p.done( function(data, status, xhr) { logOk(xhr.status+" "+xhr.statusText); try { logOk(JSON.stringify(data, null, 2)); } catch(e) { // ignore } try { validate(uri, "POST/out", JSON.parse(data), validateLog) } catch(e) { // ignore } }); p.fail( function(xhr, status, err) { logError(xhr.status+" "+xhr.statusText+"\n"+ ((status==400 || status >= 500) ? xhr.responseText : "")); }); } function del() { $.confirm("Are you sure you want to delete this resource?", "Confirm Delete", function() { logReq("DELETE "+uri); var p = $.ajax(uri, { method: "DELETE", headers: getHeaders() }); p.done( function(data, status, xhr) { $("#title").css({ "font-style": "italic" }); logOk(xhr.status+" "+xhr.statusText); }); p.fail( function(xhr, status, err) { logError(xhr.status+" "+xhr.statusText+"\n"); }); }); } function openHeader() { $("#header-button").hide(); $("#new-header-container").show(); $("#new-header").focus(); } function closeHeader() { $("#header-button").show(); $("#new-header-container").hide(); return false; } function addHeader(header, star) { $("#new-header-form").submit(); $("#headers").append('
'+header+'
'); $(".del-header").unbind("click"); $(".del-header").click( function() { $(this).parent().remove(); updateHeaderBar(); }); $(".header-star").unbind("click"); $(".header-star").click( function() { toggleStar($(this).parent()); }); $(".header-value").click( function() { $("#new-header").val($(this).text()); removeStar($(this).text()); $(this).parent().remove(); openHeader(); updateHeaderBar(); }); $("#editor").css("top", "80px"); } function toggleStar(headerElement) { var header = headerElement.children(".header-value").text(); var star = headerElement.children(".header-star"); star.toggleClass("icon-star"); star.toggleClass("icon-star-empty"); var headers = $.cookie("headers") || []; if(star.hasClass("icon-star")) { removeStar(header); } else { var pos = $.inArray(header, headers); if(pos != -1) { headers.splice(pos, 1); $.cookie("headers", headers); } } } function removeStar(header) { var headers = $.cookie("headers") || []; if($.inArray(header, headers)==-1) { headers.push(header); $.cookie("headers", headers); } } function updateHeaderBar() { if($("#headers").children().length === 0) { $("#editor").css("top", "48px"); } } function getHeaders() { var result = {}; $("#headers").find(".header-value").each(function() { var splitted = $(this).text().split(":"); result[splitted[0]]=splitted[1]; }); return result; } function switchMode() { window.onhashchange = function() {}; if(!postMode) { window.location.assign(window.location.href.replace(/\??.[^#]*#/, "?mode=post#")); } else { window.location.assign(window.location.href.replace(/mode=post/, "")); } } function setBinary(value, load) { var editorElement; if(value && !binary) { var frame = $('