Click here to Skip to main content
15,881,172 members
Articles / Web Development / HTML

sBlog.Net - A Minimalistic Blog Engine - Using ASP.NET MVC 3

Rate me:
Please Sign up or sign in to vote.
4.94/5 (55 votes)
16 Aug 2013CPOL51 min read 196.7K   6.4K   182  
sBlog.Net is a minimalistic blog engine created using the ASP.NET MVC 3 framework.
/* *********************************************** */

// sBlog.Net

// sBlog.Net is a minimalistic blog engine software.

// Homepage: http://sblogproject.net
// Github: http://github.com/karthik25/sBlog.Net

// This project is licensed under the BSD license.  
// See the License.txt file for more information.

/* *********************************************** */
$(document).ready(function () {
    $('.chkClickable').live('click', function () {
        $(this).next('.hdnStatus').val($(this).is(':checked'));
    });

    $('#btnAdd').click(function () {
        var text = $('#txtCat').val().trim();

        var regex = /\<[\S* ]*\>/;
        var alphaRegex = /[a-zA-Z]{1,}/;

        if (text.match(regex) != null) {
            showMessage('Category name entered is invalid');
            return;
        }

        if (text.match(alphaRegex) == null) {
            showMessage('Category name entered is invalid, should have at least 1 alphabet');
            return;
        }

        if (text != '' && !isPresent(text)) {
            if (text.length <= 50) {
                ajaxAdd(text);
            }
            else {
                showMessage('Category name cannot be more than 50 characters');
            }
        }
    });
});

function showMessage(message) {
    alert(message);
    $('#txtCat').val('').focus();
}

function isPresent(text) {
    var status = false;
    var elements = $("label[for^='chkBox_selectcategories_']");
    $(elements).each(function () {
        if ($(this).text() == text) {
            status = true;

            var currentTop = $(this).offset().top;
            var parentTop = $('.cpCheckBoxContent').position().top;
            $('.cpCheckBoxContent').scrollTop(currentTop - parentTop);

            $('#txtCat').val('');
        }
    });

    return status;
}

function ajaxAdd(text) {
    $.ajax({
        type: 'GET',
        url: '/Admin/CategoryAdmin/AddCategory',
        data: { 'categoryName': text },
        success: function (data) {
            if (data != null && data.CategoryID != 0) {
                addCategory(data.CategoryName, data.CategoryID);
                $(".cpCheckBoxContent").animate({ scrollTop: $(".cpCheckBoxContent").prop("scrollHeight") }, 1000);
            }
            $('#txtCat').val('');
        },
        error: function (req, status, err) {
            alert('an error occurred');
        }
    });
}

// create a new category
function addCategory(name, value) {
    var max = parseInt($("input[id^='valValue_selectcategories_']").last().attr('id').split('_')[2]) + 1;
    var element1 = document.createElement('input');
    $(element1).attr('id', 'valValue_selectcategories_' + max)
	           .attr('name', 'valValue_selectcategories_' + max)
	           .attr('type', 'hidden')
	           .attr('value', value);
    var element2 = document.createElement('input');
    $(element2).attr('id', 'chkBox_selectcategories_' + max)
	           .attr('name', 'chkBox_selectcategories_' + max)
	           .attr('type', 'checkbox')
	           .attr('class', 'chkClickable')
               .attr('checked', 'checked')
	           .attr('value', 'true');
    var element3 = document.createElement('input');
    $(element3).attr('id', 'hdnChk_selectcategories_' + max)
	           .attr('name', 'hdnChk_selectcategories_' + max)
	           .attr('type', 'hidden')
	           .attr('class', 'hdnStatus')
	           .attr('value', 'true');
    var element4 = document.createElement('label');
    $(element4).attr('for', 'chkBox_selectcategories_' + max);
    element4.innerHTML = "&nbsp;" + name;
    var element5 = document.createElement('input');
    $(element5).attr('id', 'lblLabel_selectcategories_' + max)
	           .attr('name', 'lblLabel_selectcategories_' + max)
	           .attr('type', 'hidden')
	           .attr('value', name);
    var element6 = document.createElement('br');
    $('.cpCheckBoxContent').append(element1)
	               .append(element2)
	               .append(element3)
                   .append(element4)
                   .append(element5)
                   .append(element6);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Just another passionate software developer!

Some of the contributions to the open source world - a blog engine written in MVC 4 - sBlog.Net. Check it out here. For the codeproject article regarding sBlog.Net click here!

(Figuring out this section!)

Comments and Discussions