text:
var api_doc =
{
    "swagger": "2.0",
    "paths": {},
    "info": {
        "title": "",
        "version": ""
    }
};

Handlebars.registerHelper('countKeys', function(value){
    return Object.keys(value).length * 2 + 1;
});

Handlebars.registerHelper('setIndex', function(value){
    this.index = Number(value);
});

Handlebars.registerHelper('console_log', function(value){
    console.log(value);
});

Handlebars.registerHelper( 'toString', function returnToString( x ){
    return ( x === void 0 ) ? 'undefined' : x.toString();
} );

Handlebars.registerHelper('ref', function(items, options) {
  if(items["$ref"] != undefined){
    var api = APIDesigner();
    var result = api.query(items["$ref"].replace("#","$").replace(/\//g,"."));
    if(result.length > 0){
        items = result[0];
    }
  }
  out = options.fn(items);
  return out;
});

var content_types = [
       { value : "application/json", text :  "application/json"},
       { value : "application/xml", text :  "application/xml"},
       { value : "text/plain", text :  "text/plain"},
       { value : "text/html", text :  "text/html"}
];

//function to check if an attribute exists in a nested series of objects
function checkNested(obj) {
  for (var i = 1; i < arguments.length; i++) {
    if (!obj.hasOwnProperty(arguments[i])) {
      return false;
    }
    obj = obj[arguments[i]];
  }
  return true;
}

//Create a designer class
function APIDesigner(){
    //implement singleton pattern
    this.baseURLValue = "";

    if ( arguments.callee._singletonInstance )
        return arguments.callee._singletonInstance;
    arguments.callee._singletonInstance = this;

    this.api_doc = {};
    this.resources = [] ;

    this.container = $( "#api_designer" );

    //initialise the partials
    source   = $("#designer-resources-template").html();
    Handlebars.partials['designer-resources-template'] = Handlebars.compile(source);
    source   = $("#designer-resource-template").html();
    Handlebars.partials['designer-resource-template'] = Handlebars.compile(source);
    if($('#scopes-template').length){
        source   = $("#scopes-template").html();
        Handlebars.partials['scopes-template'] = Handlebars.compile(source);
    }

    this.init_controllers();

    $( "#api_designer" ).delegate( "#more", "click", this, function( event ) {
                        $("#options").css("display", "inline-block");
                        $("#more").hide();
    });
  $( "#api_designer" ).delegate( "#less", "click", this, function( event ) {
                         $("#options").hide();
                         $("#more").css("display", "inline-block");
    });

    $( "#api_designer" ).delegate( "a.help_popup", "mouseover", this, function( event ) {
        $('a.help_popup').popover({
            html : true,
            content: function() {
                return $('#'+$(this).attr('help_data')).html();
            }
        });
    });

    $( "#api_designer" ).delegate( ".resource_expand", "click", this, function( event ) {
        if(this.resource_created == undefined){
            event.data.render_resource($(this).parent().next().find('.resource_body'));
            this.resource_created = true;
            $(this).parent().next().find('.resource_body').show();
        }
        else{
            $(this).parent().next().find('.resource_body').toggle();
        }
    });

    $( "#api_designer" ).delegate( "#add_resource", "click", this, function( event ) {
        var designer = APIDesigner();
        if($("#resource_url_pattern").val() == ""){
            jagg.message({content:"URL pattern cannot be empty.",type:"error"});
            return;
        }
        var path = $("#resource_url_pattern").val();
        if(path.charAt(0) != "/")
            path = "/"+path;

    	var resource_exist = false;
        $(".http_verb_select").each(function(){    //added this validation to fix https://wso2.org/jira/browse/APIMANAGER-2671
            if($(this).is(':checked')){
                if(designer.check_if_resource_exist( path , $(this).val() ) ){
                	resource_exist = true;
                    var err_message = "Resource already exist for URL Pattern "+path+" and Verb "+$(this).val();
                    jagg.message({content:err_message,type:"error"});
                    return;
                }
            }
        });
        if(resource_exist){
        	return;
        }

        var resource = {

        };
        //create parameters
        var re = /\{[a-zA-Z0-9_-]*\}/g;
        var parameters = [];

        while ((m = re.exec($("#resource_url_pattern").val())) != null) {
            if (m.index === re.lastIndex) {
                re.lastIndex++;
            }
            parameters.push({
                name : m[0].replace("{","").replace("}",""),
                "in": "path",
                "required": true,
				"type":"string"
            })
        }

        var vc=0;
        var ic=0;
        $(".http_verb_select").each(function(){
            if($(this).is(':checked')){
                if(!designer.check_if_resource_exist( path , $(this).val() ) ){
                    parameters = $.extend(true, [], parameters);

    		        var method = $(this).val();
                    var tempPara = parameters.concat();

                    if(method.toUpperCase() == "POST" || method.toUpperCase() == "PUT" || method.toUpperCase() == "PATCH") {
                        tempPara.push({
                            "name" : "Payload",
                            "description": "Request Body",
                            "required": false,
                            "in": "body",
                            "schema": {
                                "type" : "object"
                            }
                        });
                    }
                    resource[method] = {
                        responses : { '200':{}}
                    };
                    if(tempPara.length > 0){
                       resource[method].parameters = tempPara;
                    }
                    ic++
                }
                vc++;
            }
        });
        if(vc==0){
            jagg.message({content:"You should select at least one HTTP verb." ,type:"error"});
            return;
        }
        event.data.add_resource(resource, path);
        //RESOURCES.unshift(resource);
        $("#resource_url_pattern").val("");
        updateContextPattern();
        $(".http_verb_select").attr("checked",false);
    });

}

APIDesigner.prototype.check_if_resource_exist = function(path, method){

    for (var key in this.api_doc.paths) {

	//remove tailing slash
	if (path.lastIndexOf('/') == path.length -1) {
		path = path.substring(0, path.length -1);
	}

	var keyWithoutTailingSlash = key;
	if (key.lastIndexOf('/') == key.length -1) {
		keyWithoutTailingSlash = key.substring(0, key.length -1);
	}

        if(keyWithoutTailingSlash.toLowerCase() == path.toLowerCase()){

            if (this.api_doc.paths[key].hasOwnProperty(method)) {

                return true;
            }
        }
    }
    return false;
}

APIDesigner.prototype.set_default_management_values = function(){
    var operations = this.query("$.paths.*.*");
    for(var i=0;i < operations.length;i++){
        if(!operations[i]["x-auth-type"]){
            if(operations[i].method == "OPTIONS"){
                operations[i]["x-auth-type"] = OPTION_DEFAULT_AUTH;
            }
            else{
                operations[i]["x-auth-type"] = DEFAULT_AUTH;
            }
        }
        if(!operations[i]["x-throttling-tier"]){
            operations[i]["x-throttling-tier"] = DEFAULT_TIER;
        }
    }
}

APIDesigner.prototype.add_default_resource = function(){
    $("#resource_url_pattern").val("*");
    $(".http_verb_select:lt(5)").attr("checked","checked");
    $("#inputResource").val("Default");
    $("#add_resource").trigger('click');
}

APIDesigner.prototype.get_scopes = function() {
    var options = [{ "value": "" , "text": "" }];
    if(checkNested(this.api_doc, 'x-wso2-security','apim','x-wso2-scopes')){
    	var scopes = this.api_doc['x-wso2-security'].apim['x-wso2-scopes'];
    	for(var i =0; i < scopes.length ; i++ ){
    	    options.push({ "value": scopes[i].key , "text": scopes[i].name });
    	}
    }
    return options;
}

APIDesigner.prototype.has_resources = function(){
    if(Object.keys(this.api_doc.paths).length == 0)
        return false;
    else
        return true;
}

APIDesigner.prototype.display_elements = function(value,source){
    for(var i =0; i < source.length; i++ ){
        if(value == source[i].value){
            $(this).text(source[i].text);
        }
    }
};

APIDesigner.prototype.update_elements = function(resource, newValue){
    var swaggerSchema = JSON.parse('{"type":"object"}');
    var API_DESIGNER = APIDesigner();
    var obj = API_DESIGNER.query($(this).attr('data-path'));
    var obj = obj[0]
    if(obj["$ref"]!=undefined){
        var obj = API_DESIGNER.query(obj["$ref"].replace("#","$").replace(/\//g,"."));
        var obj = obj[0];
    }
    if ($(this).attr('data-attr-type') == "comma_seperated") {
        newValue = $.map(newValue.split(","), $.trim);
    }
    var i = $(this).attr('data-attr');
    obj[i] = newValue;
    if (i == "in") {
        //Add body parameter to the swagger
        if (newValue == "body") {
            delete obj.type;
            obj['schema'] = swaggerSchema;
        } else { //other parameters
            delete obj.schema;
            obj['type'] = "string";
        }
    }

};

APIDesigner.prototype.update_elements_boolean = function(resource, newValue){
    if(newValue == "true")
        newValue = true;
    else
        newValue = false;
    var API_DESIGNER = APIDesigner();
    var obj = API_DESIGNER.query($(this).attr('data-path'));
    var obj = obj[0];
    if(obj["$ref"]!=undefined ){
        var obj = API_DESIGNER.query(obj["$ref"].replace("#","$").replace(/\//g,"."));
        var obj = obj[0];
    }
    var i = $(this).attr('data-attr');
    obj[i] = newValue;
};

APIDesigner.prototype.init_controllers = function(){
    var API_DESIGNER = this;

    $("#version").change(function(e){
        APIDesigner().api_doc.info.version = $(this).val();
        // We do not need the version anymore. With the new plugable version strategy the context will have the version
        APIDesigner().baseURLValue = "http://localhost:8280/"+$("#context").val().replace("/","")});
    $("#context").change(function(e){ APIDesigner().baseURLValue = "http://localhost:8280/"+$(this).val().replace("/","")});
    $("#name").change(function(e){ APIDesigner().api_doc.info.title = $(this).val() });
    $("#description").change(function(e){ APIDesigner().api_doc.info.description = $(this).val() });

    this.container.delegate( ".delete_resource", "click", function( event ) {
        var operations = API_DESIGNER.query($(this).attr('data-path'));
        var operations = operations[0]
        var i = $(this).attr('data-index');
        var pn = $(this).attr('data-path-name');
        var op = $(this).attr('data-operation');
        jagg.message({content:'Do you want to remove "'+op+' : '+pn+'" resource from list.',type:'confirm',title:"Remove Resource",
        okCallback:function(){
            API_DESIGNER = APIDesigner();
            delete API_DESIGNER.api_doc.paths[pn][op];
            API_DESIGNER.render_resources();
            if(Object.keys(API_DESIGNER.api_doc.paths[pn]).length == 0) {
                delete API_DESIGNER.api_doc.paths[pn];
            }
        }});
        //delete resource if no operations
    });

    this.container.delegate(".movedown_resource","click", function(){
        var operations = API_DESIGNER.query($(this).attr('data-path'));
        var operations = operations[0]
        var i = parseInt($(this).attr('data-index'));
        if(i != (operations.length - 1)){
            var tmp = operations[i];
            operations[i] = operations[i+1];
            operations[i+1] = tmp;
        }
        API_DESIGNER.render_resources();
    });

    this.container.delegate(".moveup_resource","click", function(){
        var operations = API_DESIGNER.query($(this).attr('data-path'));
        var operations = operations[0];
        var i = parseInt($(this).attr('data-index'));
        if(i != 0){
            var tmp = operations[i];
            operations[i] = operations[i-1];
            operations[i-1] = tmp;
        }
        API_DESIGNER.render_resources();
    });

    this.container.delegate(".add_parameter", "click", function(event){
        var parameter = $(this).parent().find('.parameter_name').val();
        if(parameter == "") return false;
        var resource_body = $(this).parent().parent();
        var resource = API_DESIGNER.query(resource_body.attr('data-path'));
        var resource = resource[0]
        if(resource.parameters ==undefined){
            resource.parameters = [];
        }
        resource.parameters.push({ name : parameter , in : "query", required : false , type: "string"});
        //@todo need to checge parent.parent to stop code brak when template change.
        API_DESIGNER.render_resource(resource_body);
    });

    this.container.delegate(".delete_parameter", "click", function (event) {
        //var elementToDelete =  $(this).parent().parent();
        var deleteData = $(this).attr("data-path");
        var i = $(this).attr("data-index");

        var deleteDataArray = deleteData.split(".");
        var operations = deleteDataArray[2];
        var operation = deleteDataArray[3];
        var paramName = API_DESIGNER.api_doc.paths[operations][operation]['parameters'][i]['name'];

        jagg.message({content: i18n.t('confirm.deleteParam') + ' <strong>' + paramName + '</strong> ?',
            type: 'confirm', title: "Delete Parameter",
            okCallback: function () {
                API_DESIGNER = APIDesigner();
                API_DESIGNER.api_doc.paths[operations][operation]['parameters'].splice(i,1);
                API_DESIGNER.render_resources();
            }});
    });

    this.container.delegate(".delete_scope","click", function(){
        var i = $(this).attr("data-index");
        jagg.message({content: i18n.t('confirm.deleteScope'),
            type: 'confirm', title: "Delete Scope",
            okCallback: function () {
                API_DESIGNER.api_doc['x-wso2-security'].apim['x-wso2-scopes'].splice(i, 1);
                API_DESIGNER.render_scopes();
            }});
    });

    this.container.delegate("#define_scopes" ,'click', function(){
        $("#scopeName").val('');
        $("#scopeDescription").val('');
        $("#scopeKey").val('');
        $("#scopeRoles").val('');
        $("#define_scope_modal").modal('show');
    });

    $("#scope_submit").click(function(){
        if(!$("#scope_form").valid()){
            return;
        }
        var securityDefinitions = {
            "apim":{
                "x-wso2-scopes":[]
            }
        };
        var API_DESIGNER = APIDesigner();
		var scope = {
			name : $("#scopeName").val(),
			description : $("#scopeDescription").val(),
			key : $("#scopeKey").val(),
			roles : $("#scopeRoles").val()
		};

		jagg.post("/site/blocks/item-design/ajax/add.jag", { action:"validateScope", scope:$("#scopeKey").val()},
			function (result) {
			    if (!result.error) {

				API_DESIGNER.api_doc['x-wso2-security'] = $.extend({}, securityDefinitions, API_DESIGNER.api_doc['x-wso2-security']);

				for (var i = 0; i < API_DESIGNER.api_doc['x-wso2-security'].apim['x-wso2-scopes'].length; i++) {
					if (API_DESIGNER.api_doc['x-wso2-security'].apim['x-wso2-scopes'][i].key === $(
							"#scopeKey").val() || API_DESIGNER.api_doc['x-wso2-security'].apim['x-wso2-scopes'][i].key === $(
							"#scopeName").val()) {
						jagg.message({
							content : "Scope " + $("#scopeKey").val() + " already exists",
							type : "error"
						});
						return;
					}
				}
		      		if (result.isScopeExist == "true") {
					jagg.message({
						content : "Scope " + $("#scopeKey").val() + " already assigned by an API.",
						type : "error"
					});
					return;
				}

				API_DESIGNER.api_doc['x-wso2-security'].apim['x-wso2-scopes'].push(scope);
				$("#define_scope_modal").modal('hide');
				API_DESIGNER.render_scopes();
				API_DESIGNER.render_resources();

			    } else {
				jagg.message({
					content : result.message,
					type : "error"
				});
					return;
				}

		}, "json");
	});

    $("#swaggerEditor").click(API_DESIGNER.edit_swagger);

    $("#update_swagger").click(API_DESIGNER.update_swagger);

    $("#close_swagger_editor").click(API_DESIGNER.close_swagger_editor);
}

APIDesigner.prototype.load_api_document = function(api_document){
    this.api_doc = api_document;
    this.render_resources();
    this.render_scopes();
    $("#version").val(api_document.info.version);
    $("#name").val(api_document.info.title);
    if(api_document.info.description){
    	$("#description").val(api_document.info.description);
    }
};

APIDesigner.prototype.render_scopes = function(){
    if($('#scopes-template').length){
        context = {
            "api_doc" : this.api_doc
        }
        var output = Handlebars.partials['scopes-template'](context);
        $('#scopes_view').html(output);
    }
};

APIDesigner.prototype.transform = function(api_doc){
    var swagger = jQuery.extend(true, {}, this.api_doc);
    for(var pathkey in swagger.paths){
        var path = swagger.paths[pathkey];
        var parameters = path.parameters;
        delete path.parameters;
        for(var verbkey in path){
            var verb = path[verbkey];
            verb.path = pathkey;
        }
    }
    return swagger;
}

APIDesigner.prototype.render_resources = function(){
    context = {
        "doc" : this.transform(this.api_doc),
        "verbs" :VERBS,
        "has_resources" : this.has_resources()
    }
    var output = Handlebars.partials['designer-resources-template'](context);
    $('#resource_details').html(output);
    $('#resource_details').find('.scope_select').editable({
        emptytext: '+ Scope',
        source: this.get_scopes(),
        success : this.update_elements
    });

    if(typeof(TIERS) !== 'undefined'){
        $('#resource_details').find('.throttling_select').editable({
            emptytext: '+ Throttling',
            source: TIERS,
            success : this.update_elements
        });
    }

    if(typeof(AUTH_TYPES) !== 'undefined'){
        $('#resource_details').find('.auth_type_select').editable({
            emptytext: '+ Auth Type',
            source: AUTH_TYPES,
            autotext: "always",
            display: this.display_element,
            success : this.update_elements
        });
    }

    $('#resource_details').find('.change_summary').editable({
        emptytext: '+ Summary',
        success : this.update_elements,
        inputclass : 'resource_summary'
    });
};

APIDesigner.prototype.render_resource = function(container){
    var isBodyRequired = false;
    var operation = this.query(container.attr('data-path'));
    var context = jQuery.extend(true, {}, operation[0]);
    context.resource_path = container.attr('data-path');
    if (context.resource_path.match(/post/i) || context.resource_path.match(/put/i)) {
        isBodyRequired = true;
    }
    var output = Handlebars.partials['designer-resource-template'](context);
    container.html(output);
    container.show();

    if(container.find('.editor').length){
        var textarea = container.find('.editor').ace({ theme: 'textmate', lang: 'javascript' ,fontSize: "10pt"});
        var decorator = container.find('.editor').data('ace');
        var aceInstance = decorator.editor.ace;
        aceInstance.getSession().on('change', function(e) {
            operation[0]["x-mediation-script"] = aceInstance.getValue();
        });
    }

    container.find('.notes').editable({
        type: 'textarea',
        emptytext: '+ Add Implementation Notes',
        success : this.update_elements
    });
    container.find('.produces').editable({
        source: content_types,
        success : this.update_elements
    });
    container.find('.consumes').editable({
        source: content_types,
        success : this.update_elements
    });
    container.find('.param_desc').editable({
        emptytext: '+ Empty',
        success : this.update_elements
    });
    if(isBodyRequired){
        container.find('.param_paramType').editable({
            emptytext: '+ Set Param Type',
            source: [ { value:"body", text:"body" },{ value:"query", text:"query" },{ value:"header", text:"header" }, { value:"formData", text:"formData"} ],
            success : this.update_elements
        });
    } else {
        container.find('.param_paramType').editable({
            emptytext: '+ Set Param Type',
            source: [{ value:"query", text:"query" },{ value:"header", text:"header" }, { value:"formData", text:"formData"} ],
            success : this.update_elements
        });
    }

    container.find('.param_type').editable({
        emptytext: '+ Empty',
        success : this.update_elements
    });
    container.find('.param_required').editable({
        emptytext: '+ Empty',
        autotext: "always",
        display: function(value, sourceData){
            if(value == true || value == "true")
                $(this).text("True");
            if(value == false || value == "false")
                $(this).text("False");
        },
        source: [ { value:true, text:"True" },{ value:false, text:"False"} ],
        success : this.update_elements_boolean
    });
};

APIDesigner.prototype.query = function(path){
    return jsonPath(this.api_doc,path);
}

APIDesigner.prototype.add_resource = function(resource, path){

    if(path.charAt(0) != "/")
        path = "/" + path;
    if(this.api_doc.paths[path] == undefined){
        this.api_doc.paths[path] = resource;
    }
    else{
        this.api_doc.paths[path] = $.extend({}, this.api_doc.paths[path], resource);
    }
    this.render_resources();
};

APIDesigner.prototype.edit_swagger = function(){
    $("body").addClass("modal-open");
    $("#swaggerEditer").append('<iframe id="se-iframe"  style="border:0px;"background: #4a4a4a; width="100%" height="100%"></iframe>');
    document.getElementById('se-iframe').src = $("#swaggerEditer").attr("editor-url");
    $("#swaggerEditer").fadeIn("fast");
};

APIDesigner.prototype.close_swagger_editor = function(){
    $("body").removeClass("modal-open");
    $("#se-iframe").remove();
    $("#swaggerEditer").fadeOut("fast");
};

APIDesigner.prototype.update_swagger = function(){
    $("body").removeClass("modal-open");
    $("#se-iframe").remove();
    $("#swaggerEditer").fadeOut("fast");
    var designer =  APIDesigner();
    var json = jsyaml.safeLoad(designer.yaml);
    designer.load_api_document(json);
};

$(document).ready(function(){
    $.fn.editable.defaults.mode = 'inline';
    var designer = new APIDesigner();
    designer.load_api_document(api_doc);

    $("#swaggerEditer").on("keyup", function () {
        try {
            jsyaml.load(designer.swagger_editor.getSession().getValue());
            document.getElementById('output_string').innerHTML = "";
        } catch (err) {
            document.getElementById('output_string').innerHTML = err;
            console.log(err);
        }
    });

    $("#clearThumb").on("click", function () {
        $('#apiThumb-container').html('<input type="file" id="apiThumb" class="input-xlarge validateImageFile" name="apiThumb" />');
    });

    $("#clearSeqFile").on("click", function () {
        $('#apiThumb-container').html('<input type="file" id="apiThumb" class="input-xlarge" name="apiThumb" />');
    });

    $('#import_swagger').attr('disabled','disabled');
    $('.toggleRadios input[type=radio]').click(function(){
        if (($(this).val() == 'swagger_import_file' &&
            typeof jsonFile != 'undefined') ||
            ($(this).val() == 'swagger_import_url' &&
            $('#swagger_import_url').val().length != 0)) {
            $('#import_swagger').removeAttr("disabled");
        } else {
            $('#import_swagger').attr('disabled','disabled');
        }
        $('#swagger_help').hide();
        $('#swagger_file_help').hide();
        $('.toggleContainers .controls').hide();
        $('.toggleRadios input[type=radio]').prop('checked', false);
        $('#' + $(this).val()).closest('div').fadeIn();
        $(this).prop('checked', true);
    });

    $('#swagger_import_file').change(function (event) {
        var file = event.target.files[0];
        var fileReader = new FileReader();
        fileReader.addEventListener("load", function (event) {
            jsonFile = event.target;
        });
        //Read the text file
        fileReader.readAsText(file);
        $('#import_swagger').removeAttr("disabled");
    });

    $('#swagger_import_url').keyup(function(){
        if($('#swagger_import_url').val().length != 0) {
            $('#import_swagger').removeAttr("disabled");
        } else {
            $('#import_swagger').attr('disabled','disabled');
        }
    });

    $('#import_swagger').click(function () {
        if ($('.toggleRadios input[type=radio]:checked').val() == 'swagger_import_file') {

            $('#import_swagger').buttonLoader('start');
            $('#swagger_help').hide();
            $('#swagger_file_help').hide();
            try{
                var data = JSON.parse(jsonFile.result); //swagger file content
                var designer = APIDesigner();
                designer.load_api_document(data);
                $('#import_swagger').buttonLoader('stop');
                $("#swaggerUpload").modal('hide');
            } catch (err){
                $('#swagger_file_help').show();
                $('#import_swagger').buttonLoader('stop');
                $('#fileErrorMsgClose').on('click', function (e) {
                    $('#swagger_file_help').hide();
                });
            }
        } else {
            $('#import_swagger').buttonLoader('start');
            $('#swagger_help').hide();
            $('#swagger_file_help').hide();
            var data = {
                "swagger_url": $("#swagger_import_url").val() // "http://petstore.swagger.wordnik.com/api/api-docs"
            }
            $.get(jagg.site.context + "/site/blocks/item-design/ajax/import.jag", data, function (data) {
                $('#import_swagger').buttonLoader('stop');
                $('#swagger_help').hide();
                $("#swaggerUpload").modal('hide');
                var designer = APIDesigner();
                designer.load_api_document(data);
            }).fail(function (data) {
                $('#swagger_help').show();
                $('#import_swagger').buttonLoader('stop');
                $('#errorMsgClose').on('click', function (e) {
                    $('#swagger_help').hide();
                });
            });
        }
    });

    $("#resource_url_pattern").live('change',function(){
        var re = new RegExp("^/?([a-zA-Z0-9]|-|_)+");
        var arr = re.exec($(this).val());
        if(arr && arr.length)
            $('#inputResource').val(arr[0]);
    });

    var v = $("#design_form").validate({
        contentType : "application/x-www-form-urlencoded;charset=utf-8",
        dataType: "json",
	    onkeyup: false,
        submitHandler: function(form) {
        var designer = APIDesigner();

        if(designer.has_resources() == false){
            jagg.message({
                content:"At least one resource should be specified. Do you want to add a wildcard resource (/*)?" ,
                type:"confirm",
                title:"Resource not specified",
                anotherDialog:true,
                okCallback:function(){
                    var designer = APIDesigner();
                    designer.add_default_resource();
                    $("#design_form").submit();
                }
            });
            return false;
        }

        $('#swagger').val(JSON.stringify(designer.api_doc));

        $('#'+thisID).buttonLoader('start');

        $(form).ajaxSubmit({
            success:function(responseText, statusText, xhr, $form){

                $('#'+thisID).buttonLoader('stop');
                if (!responseText.error) {
                    var designer = APIDesigner();
                    designer.saved_api = {};
                    designer.saved_api.name = responseText.data.apiName;
                    designer.saved_api.version = responseText.data.version;
                    designer.saved_api.provider = responseText.data.provider;
                    $( "body" ).trigger( "api_saved" );
                } else {
                    if (responseText.message == "timeout") {
                        if (ssoEnabled) {
                             var currentLoc = window.location.pathname;
                             var queryString=encodeURIComponent(window.location.search);
                             if (currentLoc.indexOf(".jag") >= 0) {
                                 location.href = "login.jag?requestedPage=" + currentLoc + queryString;
                             } else {
                                 location.href = 'site/pages/login.jag?requestedPage=' + currentLoc + queryString;
                             }
                        } else {
                             jagg.showLogin();
                        }
                    } else {
                        jagg.message({content:responseText.message,type:"error"});
                    }
                }
            },
            error: function() {
                $('#'+thisID).buttonLoader('stop');
                jagg.message({content:"Error occurred while updating API",type:"error"});
            },
            dataType: 'json'
        });
        }
    });

    $("#design_form").keypress(function(e){
        $('.tagContainer .bootstrap-tagsinput input').keyup(function(e) {
            var tagName = $(this).val();
            $tag = $(this);

            if(tagName.match(/[^a-zA-Z0-9_ -]/g)){
                $tag.val( $tag.val().replace(/[^a-zA-Z0-9_ -]/g, function(str) {
                        $('.add-tags-error').html('');
                        $('.tags-error').html('The tag "' + tagName + '" contains one or more illegal characters  (~ ! @ #  ; % ^ * + = { } | < >, \' " \\ ) .');
                        return '';
                }));
            }
        });
        $('.tags-error').html('');
        $("#tags").on('itemAdded', function(event) {
             $('.tags-error').html('');
             $('.add-tags-error').html('');
        });
    });

    $('.tagContainer .bootstrap-tagsinput input').blur(function() {
        if($(this).val().length > 0){
            $('.add-tags-error').html('Please press Enter to add the tag.')
            $('.tags-error').html('');
        }
        else if($(this).val().length == 0){
            $('.add-tags-error').html('');
        }
    });

    $('#apiThumb').live('change', function() {
        var imageFileSize = this.files[0].size/1024/1024;
        if (imageFileSize > 1){
          $('#error-invalidImageFileSize').modal('show');
          $('#apiThumb-container').html('<input type="file" id="apiThumb" class="input-xlarge validateImageFile" name="apiThumb" />');
        }
    });
});

var thisID;
$('#saveBtn').click(function(e){
    thisID = $(this).attr('id');
});

$('#go_to_implement').click(function(e){
    thisID = $(this).attr('id');
});

//To reset the tab to overview
$('.goTo_api_overview').mousedown(function () {
    $.cookie("selectedTab", "view");
});

function getContextValue() {
    var context = $('#context').val();
    var version = $('#apiVersion').val();

    if (context == "" && version != "") {
        $('#contextForUrl').html("/{context}/" + version);
        $('#contextForUrlDefault').html("/{context}/" + version);
    }
    if (context != "" && version == "") {
        if (context.charAt(0) != "/") {
            context = "/" + context;
        }
        $('#contextForUrl').html(context + "/{version}");
        $('#contextForUrlDefault').html(context + "/{version}");
    }
    if (context != "" && version != "") {
        if (context.charAt(0) != "/") {
            context = "/" + context;
        }
        $('.contextForUrl').html(context + "/" + version);
    }
    updateContextPattern();
}

function updateContextPattern() {
    var context = $('#context').val();
    var version = $('#version').val();

    if (context != "") {
        if (context.indexOf("{version}") < 0) {
            context = context + '/';
            context = context + "{version}";
        }
        $('#resource_url_pattern_refix').text(context);
    } else {
        $('#resource_url_pattern_refix').text("/{context}/{version}/");
    }

    if (version) {
        context = context.replace("{version}",version);
        $('#resource_url_pattern_refix').text(context);
    }
}


text:
require 'bundler/capistrano'
require 'capistrano-rbenv'
set :rbenv_ruby_version, '2.0.0-p195'

def validate_env(*env_keys)
  raise RuntimeError.new("Necessary ENVs are not defined") unless ENV.values_at(*env_keys).all?
end

validate_env(*%w(
  DEPLOY_USER
  DEPLOY_SERVER
  DEPLOY_TO_DIR
  SHIITA_USER
  SHIITA_GROUP
  DATABASE_URI
  GOOGLE_KEY
  GOOGLE_SECRET
  REMOTE_SHIITA_DOMAIN
))

set :application, "shiita"
set :use_sudo, true
set :user, ENV["DEPLOY_USER"]
set :deploy_to, ENV["DEPLOY_TO_DIR"]
set :rails_env, "production"
set :rake, "rake RAILS_ENV=#{rails_env}"
server ENV["DEPLOY_SERVER"], :web, :app, :db, primary: true
set :default_environment, {
  "SHIITA_USER" => ENV["SHIITA_USER"],
  "SHIITA_GROUP" => ENV["SHIITA_GROUP"],
  "DATABASE_URI" => ENV["DATABASE_URI"],
  "GOOGLE_KEY" => ENV["GOOGLE_KEY"],
  "GOOGLE_SECRET" => ENV["GOOGLE_SECRET"],
  "SHIITA_DOMAIN" => ENV["REMOTE_SHIITA_DOMAIN"],
  "RBENV_ROOT" => "#{rbenv_path}",
  "PATH" => "#{rbenv_path}/shims:#{rbenv_path}/bin:$PATH"
}

set :scm, :git
set :branch, (ENV["SHIITA_BRANCH"] || "master")
set :repository, (ENV["SHIITA_REPOSITORY"] || "https://github.com/taiki45/shiita.git")
set :deploy_via, :remote_cache
set :bundle_without, [:development, :test]
set :keep_releases, 5

namespace :setup do
  task :fix_permissions do
    try_sudo "chown -R #{user}.#{user} #{deploy_to}"
  end
end
after "deploy:setup", "setup:fix_permissions"

namespace :deploy do

  desc "Start unicorn server"
  task :start, :roles => :app do
    run "cd #{current_path} && bundle exec unicorn_rails -c config/unicorn.conf -E #{rails_env} -D"
  end

  desc "Restart unicorn server"
  task :restart, :roles => :app do
    if File.exist? "tmp/pids/unicorn.pid"
      run "cd #{current_path} && kill -s USR2 `cat tmp/pids/unicorn.pid`"
    end
  end

  desc "Stop unicorn server"
  task :stop, :roles => :app do
    run "cd #{current_path} && kill -s QUIT `cat tmp/pids/unicorn.pid`"
  end

  namespace :db do
    desc "Migrate DB to head"
    task :migrate, :roles => :db do
      run "cd #{current_path} && #{rake} db:migrate"
    end
    after "deploy:update", "deploy:db:migrate"

    desc "Ensure indexes for MongoDB"
    task :create_indexes, :roles => :db do
      run "cd #{current_path} && #{rake} db:mongoid:create_indexes"
    end
    after "deploy:db:migrate", "deploy:db:create_indexes"
  end

  namespace :assets do
    desc "Run the assets precompilation only if assets changed"
    task :precompile, :roles => :web, :except => { :no_release => true } do
      from = source.next_revision(current_revision)
      modified = capture(
        "cd #{latest_release} && #{source.local.log(from)} vendor/assets/ app/assets/ | wc -l"
      ).to_i > 0
      if previous_release.nil? || modified
        run %Q{cd #{latest_release} && #{rake} #{asset_env} assets:precompile}
      else
        logger.info "Skipping asset pre-compilation because there were no asset changes"
      end
    end
  end

end

redpajama_set_name: RedPajamaGithub


text:
package org.apache.ignite.ml.math.stat;

import org.apache.ignite.internal.util.typedef.internal.A;
import org.apache.ignite.ml.math.primitives.matrix.Matrix;
import org.apache.ignite.ml.math.primitives.vector.Vector;

/**
 * Distribution represents multidimensional gaussian distribution.
 */
public class MultivariateGaussianDistribution implements Distribution {
    /** Mean. */
    private Vector mean;

    /** Covariance^-1. */
    private Matrix invCovariance;

    /** Normalizer. */
    private double normalizer;

    /** Covariance. */
    private Matrix covariance;

    /**
     * Constructs an instance of MultivariateGaussianDistribution.
     *
     * @param mean Mean.
     * @param covariance Covariance.
     */
    public MultivariateGaussianDistribution(Vector mean, Matrix covariance) {
        A.ensure(covariance.columnSize() == covariance.rowSize(), "Covariance matrix should be square");
        A.ensure(mean.size() == covariance.rowSize(), "Covariance matrix should be built from same space as mean vector");

        this.mean = mean;
        this.covariance = covariance;
        invCovariance = covariance.inverse();

        double determinant = covariance.determinant();
        A.ensure(determinant > 0, "Covariance matrix should be positive definite");
        normalizer = Math.pow(2 * Math.PI, ((double)invCovariance.rowSize()) / 2) * Math.sqrt(determinant);
    }

    /** {@inheritDoc} */
    @Override public double prob(Vector x) {
        Vector delta = x.minus(mean);
        Matrix ePower = delta.toMatrix(true)
            .times(invCovariance)
            .times(delta.toMatrix(false))
            .times(-0.5);
        assert ePower.columnSize() == 1 && ePower.rowSize() == 1;

        return Math.pow(Math.E, ePower.get(0, 0)) / normalizer;
    }

    /** {@inheritDoc} */
    @Override public int dimension() {
        return mean.size();
    }

    /**
     * @return Mean vector.
     */
    public Vector mean() {
        return mean.copy();
    }

    /**
     * @return Covariance matrix.
     */
    public Matrix covariance() {
        return covariance;
    }
}


text:
<?php

namespace App\Http\Controllers\Admin;

use App\Models\User;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Cartalyst\Sentinel\Laravel\Facades\Sentinel;
use Illuminate\Support\Facades\Redirect;

class AuthController extends Controller
{

    /**
     * authenticate
     *
     * This performs admin authentication.
     * This checks the role as to which group the user belongs to as well.
     *
     * @param Request $request
     * @throws Exception
     */
    public function authenticate(Request $request)
    {
        $this->validate($request, [
            'email'    => 'required',
            'password' => 'required',
        ]);

        $auth = Sentinel::authenticateAndRemember($request->all());
        if (! $auth) {
            return Redirect::back()
                ->withInput($request->except('password'))
                ->with('flash_message', [
                    'status'  => 'error',
                    'message' => 'Invalid email/password!'
                ]);
        }

        if ($auth->inRole('admin')) {
            return Redirect::route('admin.dashboard');
        }

        if ($auth->inRole('superadmin')) {
            return Redirect::route('superadmin.dashboard');
        }
    }

    /**
     * change Password
     *
     * Change the user's password
     *
     * @param Request $request
     * @return mixed
     * @throws \Exception
     */
    public function changePassword(Request $request)
    {
        $params = $request->all();
        $user = Sentinel::getUser();
        $credentials = [
            'email'     => $user->email,
            'password'  => (! empty($params['currentPassword'])) ? $params['currentPassword'] : ''
        ];
        $re_auth_user = Sentinel::validateCredentials($user, $credentials);

        //check if current password is correct
        if (! $re_auth_user) {
            return Redirect::back()->with([
                'passwordErrors'   => 'Current password is incorrect'
            ]);
        }

        //check if new passwords match
        $new_pass     = (! empty($params['newPassword']))    ? $params['newPassword'] : '';
        $re_new_pass  = (! empty($params['re-newPassword'])) ? $params['re-newPassword'] : '';
        if ($new_pass != $re_new_pass) {
            return Redirect::back()->with([
                'passwordErrors'   => 'New passwords does not match'
            ]);
        }

        $new_credentials = ['password' => $params['newPassword']];
        $user = Sentinel::update($user, $new_credentials);

        return Redirect::route('admin.login')
            ->with([
                'success-message' => 'Password successfully changed. Please login with your new password.'
            ]);
    }

    /**
     * logout
     *
     * Logs out the current admin user and destroys all
     * Session keys.
     *
     * @return Redirect
     */
    public function logout()
    {
        Sentinel::logout(Sentinel::getUser());
        return Redirect::to('/');
    }
}


text:
var mongoose = require('mongoose')
  , ObjectID = mongoose.ObjectID
  , BinaryParser = mongoose.mongo.BinaryParser;

exports.useTimestamps = function (schema, options) {
  schema.add({
      createdAt: Date
    , updatedAt: Date
  });
  schema.pre('save', function (next) {
    if (!this.createdAt) {
      this.createdAt = this.updatedAt = new Date;
    } else {
      this.updatedAt = new Date;
    }
    next();
  });
};


text:
<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC
        "-//Puppy Crawl//DTD Suppressions 1.1//EN"
        "http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">
<suppressions>
    <suppress checks="MethodName" files="core/src/test/java/android/util/Log.java" />
</suppressions>


text:
namespace NSMenu
{
	using AppKit;

	public static class Program
	{
		public Int32 Main(Int32 argc, AnsiChar **argv)
		{
			using (__autoreleasepool)
			{
				return NSApplicationMain(argc, argv);
			}
		}
	}
}


text:
/*NOCHKSRC*/
//==============================================================================
//
// PLEASE DO NOT EDIT; THIS FILE WAS AUTOMATICALLY GENERATED BY GENCLASS 1.1.2
//
//==============================================================================

#ifndef _cimple_CIM_System_h
#define _cimple_CIM_System_h

#include <cimple/cimple.h>
#include "CIM_EnabledLogicalElement.h"

CIMPLE_NAMESPACE_BEGIN

// CIM_System keys:
//     Name
//     CreationClassName

/*[928]*/
class CIM_System : public Instance
{
public:
    // CIM_ManagedElement features:
    Property<String> Caption;
    Property<String> Description;
    Property<String> ElementName;

    // CIM_ManagedSystemElement features:
    Property<Datetime> InstallDate;
    Property<String> Name;
    Property<Array_uint16> OperationalStatus;
    Property<Array_String> StatusDescriptions;
    Property<String> Status;
    Property<uint16> HealthState;

    // CIM_LogicalElement features:

    // CIM_EnabledLogicalElement features:
    Property<uint16> EnabledState;
    Property<String> OtherEnabledState;
    Property<uint16> RequestedState;
    Property<uint16> EnabledDefault;
    Property<Datetime> TimeOfLastStateChange;

    // CIM_System features:
    Property<String> CreationClassName;
    Property<String> NameFormat;
    Property<String> PrimaryOwnerName;
    Property<String> PrimaryOwnerContact;
    Property<Array_String> Roles;

    CIMPLE_CLASS(CIM_System)
};

typedef CIM_EnabledLogicalElement_RequestStateChange_method CIM_System_RequestStateChange_method;

CIMPLE_NAMESPACE_END

#endif /* _cimple_CIM_System_h */


text:
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**IsFreeMove** | **bool** | Should free-move be enabled in the fleet | [optional] [default to null]
**Motd** | **string** | New fleet MOTD in CCP flavoured HTML | [optional] [default to null]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


text:
        ServiceNow table name is incorrect

        There are no ServiceNow issues found

 0">

                        {{value.counter}}
                        {{value.state}}

 5">

                        More...


text:
from __future__ import unicode_literals

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('listing', '0001_initial'),
    ]

    operations = [
        migrations.AlterModelOptions(
            name='creator',
            options={'ordering': ['first']},
        ),
        migrations.AddField(
            model_name='creator',
            name='url',
            field=models.URLField(default='http://marvel.com/comics/creators'),
        ),
    ]


text:
package com.xhw.toucan.news.base;

import android.app.Activity;
import android.view.View;

public abstract class BaseMenuDetailPager
{
	public Activity mActivity;

	public View mRootView;

	public BaseMenuDetailPager(Activity activity){
		mActivity=activity;
		mRootView=initView();
	}

	public abstract View initView();

	public void initData(){}

	public void initListener(){}
}


text:
var args = process.argv.slice(2),
    fs = require('fs'),
    xml2js = require('xml2js'),
    xmlParser = new xml2js.Parser(),
    builder = new xml2js.Builder(),
    exec = require('child_process').exec;

var packageJSON = require(__dirname + '/../package.json'),
    pluginXML = fs.readFileSync(__dirname + '/../plugin.xml');

xmlParser.parseString(pluginXML, function (err, result) {
    result.plugin.$.version = packageJSON.version;
    var xml = builder.buildObject(result, {includeWhiteChars: true});
    fs.writeFile(__dirname + '/../plugin.xml', xml, function (err) {
        if (err) throw err;
    });
});


text:
Welcome to Tastypie!
====================

Tastypie is an webservice API framework for Django. It provides a convenient,
yet powerful and highly customizable, abstraction for creating REST-style
interfaces.

.. toctree::
   :maxdepth: 2

   tutorial
   interacting
   settings
   non_orm_data_sources
   tools

   resources
   bundles
   api
   fields
   caching
   validation
   authentication_authorization
   serialization
   throttling

   cookbook
   debugging
   who_uses

Getting Help
============

There are two primary ways of getting help. We have a `mailing list`_ hosted at
Google (http://groups.google.com/group/django-tastypie/) and an IRC channel
(`#tastypie on irc.freenode.net`_) to get help, want to bounce idea or
generally shoot the breeze.

.. _`mailing list`: http://groups.google.com/group/django-tastypie/
.. _#tastypie on irc.freenode.net: irc://irc.freenode.net/tastypie

Quick Start
===========

1. Add ``tastypie`` to ``INSTALLED_APPS``.
2. Create an ``api`` directory in your app with a bare ``__init__.py``.
3. Create an ``<my_app>/api/resources.py`` file and place the following in it::

    from tastypie.resources import ModelResource
    from my_app.models import MyModel

    class MyModelResource(ModelResource):
        class Meta:
            queryset = MyModel.objects.all()
            allowed_methods = ['get']

4. In your root URLconf, add the following code (around where the admin code might be)::

    from tastypie.api import Api
    from my_app.api.resources import MyModelResource

    v1_api = Api(api_name='v1')
    v1_api.register(MyModelResource())

    urlpatterns = patterns('',
      # ...more URLconf bits here...
      # Then add:
      (r'^api/', include(v1_api.urls)),
    )

5. Hit http://localhost:8000/api/v1/?format=json in your browser!

Requirements
============

Tastypie requires the following modules. If you use Pip_, you can install
the necessary bits via the included ``requirements.txt``:

Required
--------

* Python 2.5+
* Django 1.2+
* mimeparse 0.1.3+ (http://code.google.com/p/mimeparse/)

  * Older versions will work, but their behavior on JSON/JSONP is a touch wonky.

* dateutil (http://labix.org/python-dateutil) >= 1.5, < 2.0

Optional
--------

* python_digest (https://bitbucket.org/akoha/python-digest/)
* lxml (http://codespeak.net/lxml/) if using the XML serializer
* pyyaml (http://pyyaml.org/) if using the YAML serializer
* biplist (http://explorapp.com/biplist/) if using the binary plist serializer

.. _Pip: http://pip.openplans.org/