среда, 30 октября 2013 г.

Оценка “Мне нравится” на форме просмотра элемента

В SharePoint 2013 есть стандартный функционал оценок:

image

Он работает отлично на обычных табличных представлениях.

Но не очень понятно, как реализовать, например, оценку фотографий, когда пользователю необходимо просмотреть фотографии и тут же их оценить.

image

На странице DispForm нет кнопки “Нравится”, есть только в табличном представлении.

Но можно добавить эту кнопку следующим кодом:

<div class="LikeSection"><span class="likecount"></span><a href="#" onclick="LikePage()" class="LikeButton"></a></div>
<SharePoint:ScriptLink language="javascript" name="reputation.js" OnDemand="true" runat="server" Localizable="false"/>
<script type="text/javascript">
    function getQueryStringParameter(urlParameterKey) {
        var params = document.URL.split('?')[1].split('&');
        var strParams = '';
        for (var i = 0; i < params.length; i = i + 1) {
            var singleParam = params[i].split('=');
            if (singleParam[0] == urlParameterKey)
                return decodeURIComponent(singleParam[1]);
        }
    }

    function LikePage() {
        var like = false;
        var likeButtonText = $("a.LikeButton").text();
        if (likeButtonText != "") {
            if (likeButtonText == "Нравится")
                like = true;
   
            var aContextObject = new SP.ClientContext();
            EnsureScriptFunc('reputation.js', 'Microsoft.Office.Server.ReputationModel.Reputation', function () {
                Microsoft.Office.Server.ReputationModel.
                Reputation.setLike(aContextObject,
                    _spPageContextInfo.pageListId.substring(1, 37),
                    getQueryStringParameter('ID'), like);
   
                aContextObject.executeQueryAsync(
                    function () {
                        //alert(String(like));
                        GetLikeCount();
                    }, function (sender, args) {
                        //alert('F0');
                    });
            });
        }
   
    }
   
    function GetLikeCount() {
   
        var context = new SP.ClientContext(_spPageContextInfo.webServerRelativeUrl);
        var list = context.get_web().get_lists().getById(_spPageContextInfo.pageListId);
        var item = list.getItemById(getQueryStringParameter('ID'));
   
        context.load(item, "LikedBy", "ID", "LikesCount");
        context.executeQueryAsync(Function.createDelegate(this, function (success) {
            // Check if the user id of the current users is in the collection LikedBy.
            var likeDisplay = true;
            var $v_0 = item.get_item('LikedBy');
            var itemc = item.get_item('LikesCount');
            if (!SP.ScriptHelpers.isNullOrUndefined($v_0)) {
                for (var $v_1 = 0, $v_2 = $v_0.length; $v_1 < $v_2; $v_1++) {
                    var $v_3 = $v_0[$v_1];
                    if ($v_3.$1E_1 === _spPageContextInfo.userId) {
                        //cb(true, item.get_item('LikesCount'));
                        //alert("Liked by me");
                        likeDisplay = false;
                    }
                }
            }
            ChangeLikeText(likeDisplay, itemc);
   
        }), Function.createDelegate(this, function (sender, args) {
            //alert('F1');
        }));
   
    }
   
    function ChangeLikeText(like, count) {
        if (like) {
            $("a.LikeButton").text('Нравится');
        }
        else {
            $("a.LikeButton").text('Разонравилось');
        }
        var htmlstring = '<img alt="" src="/_layouts/15/images/LikeFull.11x11x32.png" />' + ' ' + String(count);
        if (count > 0)
            $(".likecount").html(htmlstring)
        else
            $(".likecount").html("");
    }
   
    $(document).ready(function () {
        GetLikeCount();
        $("a.LikeButton").click(function () {
            LikePage();
        });
    });
</script>

Получается вполне симпатично:

image

Комментариев нет:

Отправить комментарий