بیدگل به زبان کاریکاتور
بیدگل به زبان کاریکاتور

بیدگل به زبان کاریکاتور

میخندی باید...

لایک (Like) مطالب در بلاگ اسکای

لایک (Like) مطالب در بلاگ اسکای
لایک (Like) مطالب در بلاگ اسکای
یکی از امکاناتی که نسخه جدید بلاگ اسکای برای کاربران فراهم کرده قابلیت لایک مطالب هست. با توجه به اینکه این قابلیت هنوز بر روی قالب های پیش فرض بلاگ اسکای فعال نشده، اما امروز همیار بلاگ اسکای به شما نشان خواهد داد که چطور این ابزار رو فعال کنید.

خوب ، شروع کار :

1- ابتدا کد اسکریپت فعال سازی (اسکریپت 1) کپی کنید و اون رو بعد از تگ </style> و قبل از تگ <head> قرار بدید.


<script>

//<![CDATA[

var json_parse = (function () {

            var at,

                ch,

                escapee = {

                    '"':  '"',

                    '\\': '\\',

                    '/':  '/',

                    b:    '\b',

                    f:    '\f',

                    n:    '\n',

                    r:    '\r',

                    t:    '\t'

                },

                text,


                error = function (m) {

                    throw {

                        name:    'SyntaxError',

                        message: m,

                        at:      at,

                        text:    text

                    };

                },


                next = function (c) {

                    if (c && c !== ch) {

                        error("Expected '" + c + "' instead of '" + ch + "'");

                    }


                    ch = text.charAt(at);

                    at += 1;

                    return ch;

                },


                number = function () {

                    var number,

                        string = '';


                    if (ch === '-') {

                        string = '-';

                        next('-');

                    }

                    while (ch >= '0' && ch <= '9') {

                        string += ch;

                        next();

                    }

                    if (ch === '.') {

                        string += '.';

                        while (next() && ch >= '0' && ch <= '9') {

                            string += ch;

                        }

                    }

                    if (ch === 'e' || ch === 'E') {

                        string += ch;

                        next();

                        if (ch === '-' || ch === '+') {

                            string += ch;

                            next();

                        }

                        while (ch >= '0' && ch <= '9') {

                            string += ch;

                            next();

                        }

                    }

                    number = +string;

                    if (isNaN(number)) {

                        error("Bad number");

                    } else {

                        return number;

                    }

                },


                string = function () {

                    var hex,

                        i,

                        string = '',

                        uffff;


                    if (ch === '"') {

                        while (next()) {

                            if (ch === '"') {

                                next();

                                return string;

                            } else if (ch === '\\') {

                                next();

                                if (ch === 'u') {

                                    uffff = 0;

                                    for (i = 0; i < 4; i += 1) {

                                        hex = parseInt(next(), 16);

                                        if (!isFinite(hex)) {

                                            break;

                                        }

                                        uffff = uffff * 16 + hex;

                                    }

                                    string += String.fromCharCode(uffff);

                                } else if (typeof escapee[ch] === 'string') {

                                    string += escapee[ch];

                                } else {

                                    break;

                                }

                            } else {

                                string += ch;

                            }

                        }

                    }

                    error("Bad string");

                },


                white = function () {

                    while (ch && ch <= ' ') {

                        next();

                    }

                },


                word = function () {

                    switch (ch) {

                        case 't':

                            next('t');

                            next('r');

                            next('u');

                            next('e');

                            return true;

                        case 'f':

                            next('f');

                            next('a');

                            next('l');

                            next('s');

                            next('e');

                            return false;

                        case 'n':

                            next('n');

                            next('u');

                            next('l');

                            next('l');

                            return null;

                    }

                    error("Unexpected '" + ch + "'");

                },


                value,


                array = function () {

                    var array = [];


                    if (ch === '[') {

                        next('[');

                        white();

                        if (ch === ']') {

                            next(']');

                            return array;

                        }

                        while (ch) {

                            array.push(value());

                            white();

                            if (ch === ']') {

                                next(']');

                                return array;

                            }

                            next(',');

                            white();

                        }

                    }

                    error("Bad array");

                },


                object = function () {

                    var key,

                        object = {};


                    if (ch === '{') {

                        next('{');

                        white();

                        if (ch === '}') {

                            next('}');

                            return object;

                        }

                        while (ch) {

                            key = string();

                            white();

                            next(':');

                            if (Object.hasOwnProperty.call(object, key)) {

                                error('Duplicate key "' + key + '"');

                            }

                            object[key] = value();

                            white();

                            if (ch === '}') {

                                next('}');

                                return object;

                            }

                            next(',');

                            white();

                        }

                    }

                    error("Bad object");

                };


            value = function () {

                white();

                switch (ch) {

                    case '{':

                        return object();

                    case '[':

                        return array();

                    case '"':

                        return string();

                    case '-':

                        return number();

                    default:

                        return ch >= '0' && ch <= '9' ? number() : word();

                }

            };


            return function (source, reviver) {

                var result;


                text = source;

                at = 0;

                ch = ' ';

                result = value();

                white();

                if (ch) {

                    error("Syntax error");

                }


                return typeof reviver === 'function' ? (function walk(holder, key) {

                    var k, v, value = holder[key];

                    if (value && typeof value === 'object') {

                        for (k in value) {

                            if (Object.hasOwnProperty.call(value, k)) {

                                v = walk(value, k);

                                if (v !== undefined) {

                                    value[k] = v;

                                } else {

                                    delete value[k];

                                }

                            }

                        }

                    }

                    return reviver.call(holder, key, value);

                }({'': result}, '')) : result;

            };

        }());


        function SetCookie(c_name, c_value, e_days)

        {

            var exdate = new Date();

            exdate.setDate(exdate.getDate() + e_days);

            var value = "";

            if (c_value instanceof Array) {

                for(var i = 0; i < c_value.length; i++) {

                    if (i != 0)

                        value += "&";

                    value += c_value[i][0] + "=" + escape(c_value[i][1]);

                }

            }

            else {

                value = escape(c_value);

            }


            var parts = location.hostname.split('.');

            var subdomain = parts.shift();

            var upperleveldomain = parts.join('.');


            var domain = location.hostname.split('.').slice(-2).join('.').toLocaleLowerCase();

            if (domain == "blogsky.com") domain = ".blogsky.com";

            value += (e_days == null) ? "" : ";expires=" + exdate.toUTCString() + ";path=/;domain=" + domain;

            document.cookie = c_name + "=" + value;


        }


        function GetCookie(c_name)

        {

           var c_value = document.cookie;

            var c_start = c_value.indexOf(" " + c_name + "=");

            if (c_start == -1)

            {

                c_start = c_value.indexOf(c_name + "=");

            }

            if (c_start == -1)

            {

                c_value = null;

            }

            else

            {

                c_start = c_value.indexOf("=", c_start) + 1;

                var c_end = c_value.indexOf(";", c_start);

                if (c_end == -1)

                {

                    c_end = c_value.length;

                }


                var c_value = c_value.substring(c_start, c_end);


                var equalIndex = c_value.indexOf("=");

                var ampIndex = c_value.indexOf("&");

                if (ampIndex < equalIndex) {

                    c_end = ampIndex;

                    c_value = c_value.substring(0, c_end);

                }


                if (c_value.indexOf("=") != -1) {

                    var c_values = c_value.split("&");

                    c_value = new Array();

                    for(var i = 0; i < c_values.length; i++) {

                        c_value.push(new Array(c_values[i].split("=")[0], unescape(c_values[i].split("=")[1])));

                    }

                }

                else {

                    c_value = unescape(c_value);

                }

            }

            return c_value;

        }


        function DeleteCookie(c_name) {

            SetCookie(c_name, "", -10);

        }


        function PollVote(submitButton, pollId) {

            var pollVoteMessage = document.getElementById("poll-vote-message-" + pollId);


            submitButton.setAttribute("disabled", "disabled");


            var votes = "";

            var index = 0;

            var elements = document.getElementsByTagName("*");

            for (var i = 0; i < elements.length; i++) {

                var elementPollId = elements[i].getAttribute("data-poll-id");

                if (elementPollId != undefined && elementPollId == pollId) {

                    if (elements[i].checked) {

                        votes += "&votes[" + index + "]=" + elements[i].value;

                        index++;

                    }

                }

            }


            if (votes == "") {

                submitButton.removeAttribute("disabled");


                pollVoteMessage.innerHTML = "گزینه‌ای برای رای دادن انتخاب نشده است";

                pollVoteMessage.style.display = "block";

                setTimeout(function() { pollVoteMessage.style.display = "none"; }, 5000);

                return;

            }


            var xmlhttp;

            if (window.XMLHttpRequest) {

                xmlhttp = new XMLHttpRequest();

            }

            else {

                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

            }

            xmlhttp.onreadystatechange = function () {

                if (xmlhttp.readyState == 4) {

                    if (xmlhttp.status == 200) {

                        var response = json_parse(xmlhttp.responseText);

                        if (response.Success) {

                            document.getElementById("poll-vote-" + pollId).innerHTML = response.PollResult;

                        }

                        else {

                            submitButton.removeAttribute("disabled");


                            pollVoteMessage.innerHTML = response.Message;

                            pollVoteMessage.style.display = "block";

                            setTimeout(function() { pollVoteMessage.style.display = "none"; }, 5000);

                        }

                    }

                    else if (xmlhttp.status == 500) {

                        submitButton.removeAttribute("disabled");


                        pollVoteMessage.innerHTML = "خطا در انجام عملیات";

                        pollVoteMessage.style.display = "block";

                        setTimeout(function() { pollVoteMessage.style.display = "none"; }, 5000);

                    }

                }

            }

            xmlhttp.open("POST", "@pollActionUrl", true);

            xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

            xmlhttp.send("pollid=" + pollId + votes);

        }


        function PollResult(resultButton, pollId)

        {

            var pollVoteMessage = document.getElementById("poll-vote-message-" + pollId);

            var pollResultStatus = resultButton.getAttribute("data-status");

            if (pollResultStatus == "locked") return;

            resultButton.setAttribute("data-status", "locked");


            var xmlhttp;

            if (window.XMLHttpRequest) {

                xmlhttp = new XMLHttpRequest();

            }

            else {

                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

            }

            xmlhttp.onreadystatechange = function () {

                if (xmlhttp.readyState == 4) {

                    if (xmlhttp.status == 200) {

                        resultButton.setAttribute("data-status", "available");


                        var response = json_parse(xmlhttp.responseText);

                        if (response.Success) {

                            var pollVote = document.getElementById("poll-vote-" + pollId);

                            pollVote.style.display = "none";


                            var pollResult = document.createElement("div");

                            pollResult.id = "poll-result-" + pollId;

                            pollResult.innerHTML = response.PollResult;

                            pollVote.parentNode.insertBefore(pollResult, pollVote);


                            var backButton = document.createElement("a");

                            backButton.onclick = function(){ PollBack(this, pollId); };

                            backButton.style.cursor = "pointer";

                            backButton.innerHTML = "بازگشت به نظرسنجی";

                            document.getElementById("poll-result-" + pollId).appendChild(backButton);

                        }

                        else {

                            pollVoteMessage.innerHTML = response.Message;

                            pollVoteMessage.style.display = "block";

                            setTimeout(function() { pollVoteMessage.style.display = "none"; resultButton.setAttribute("data-status", "available"); }, 5000);

                        }                        

                    }

                    else if (xmlhttp.status == 500) {

                        pollVoteMessage.innerHTML = "خطا در انجام عملیات";

                        pollVoteMessage.style.display = "block";

                        setTimeout(function() { pollVoteMessage.style.display = "none"; resultButton.setAttribute("data-status", "available"); }, 5000);

                    }

                }

            }

            xmlhttp.open("POST", "@pollResultActionUrl", true);

            xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

            xmlhttp.send("pollid=" + pollId);

        }


        function PollBack(backElement, pollId) {

            var pollResult = document.getElementById("poll-result-" + pollId);

            pollResult.parentNode.removeChild(pollResult);


            document.getElementById("poll-vote-" + pollId).style.display = "block";

        }

//]]>

    </script>



2- سپس بلاک مربوط به لایک مطالب همراه با اسکریپت لایک که کدش رو در زیر مشاهده می کنید بین دو بلاک  <post><many></many></post> و درجایی که میخواهید نمایش داده شود قرار بدید.(بروزرسانی شد.)
تغییرات : کد اسکریپت عملیات لایک که قبلا در انتهای کد قالب قرار میدادید حذف شده و به درون بلاک لایک و بعد از تگ <like> انتقال داده شده است.







<like>
<script>
function PostLike(likeElement, postId) {
var postLikeStatus = likeElement.getAttribute("data-status");
if (postLikeStatus == "locked") return;
likeElement.setAttribute("data-status", "locked");

var postLikeCount = parseInt(likeElement.getAttribute("data-like-count"));
var postLikeMessageElement = document.getElementById("post-like-message-" + postId);

if (GetCookie(".bspl") != null) {
postLikeMessageElement.innerHTML = "شما یکبار این مطلب را لایک کرده‌اید";
setTimeout(function() { postLikeMessageElement.innerHTML = ""; likeElement.setAttribute("data-status", "available"); }, 5000);
return;
}

var postLikeCountElement = document.getElementById("post-like-count-" + postId);
postLikeCountElement.innerHTML = postLikeCount + 1;

var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
var response = json_parse(xmlhttp.responseText);
if (response.Success) {
likeElement.setAttribute("data-status", "available");
likeElement.setAttribute("data-like-count", postLikeCount + 1);
}
else {
postLikeCountElement.innerHTML = postLikeCount;
postLikeMessageElement.innerHTML = response.Message;
setTimeout(function() { postLikeMessageElement.innerHTML = ""; likeElement.setAttribute("data-status", "available"); }, 5000);
}
}
else if (xmlhttp.status == 500) {
postLikeCountElement.innerHTML = postLikeCount;
postLikeMessageElement.innerHTML = "خطا در انجام عملیات";
setTimeout(function() { postLikeMessageElement.innerHTML = ""; likeElement.setAttribute("data-status", "available"); }, 5000);
}
}
}
xmlhttp.open("POST", "@postLikeActionUrl", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(null);
}
</script>
   <div class="like">
       <span onclick="PostLike(this, @id)" data-like-count="@count" data-status="available" style="cursor: pointer;">
       <img style="vertical-align: middle;" src="http://www.blogsky.com/images/template/blue/like.gif">
       (<span style="" id="post-like-count-@id">@count</span> لایک)</span>
        <span id="post-like-message-@id" class="message"></span>
   </div>
</like>

4- در آخر هم برای اینکه یه شکل منظمی به این عنصر بدید میتونید از کد css برای استایل دهی استفاده کنید.
.like { margin: 5px 10px; }
.like .message { color: Red; }

نکته : 
- تیک گزینه "علاقه‌مندی (لایک)" رو از قسمت "تنظیمات" -> "یادداشت ها" فعال کنید.
- دکمه لایک فقط در ادامه مطلب نمایش داده خواهد شد.
- ابزار لایک فقط در صفحه همان پست، یعنی در ادامه مطلب نمایش داده میشود.

عزیزان فقط دقت داشته باشن که کدها رو درست در جایی که گفته شده قرار بدن تا ابزار به درستی نمایش داده بشه و کار کنه.
امیدوارم که مفید بوده باشه.

منبع : همیار بلاگ اسکای