EditableLinkList

Table of contents
No headers
/***

    EditableLinkList

    (required) pageid : number
        The page for which the list of links is managed by this template.
        
    (optional) propertyKey : str
        The page property to fetch that contains the JSON-encoded array of page IDs.  
        If not supplied, this parameter will default to "mindtouch.idf#featured-articles".
    
***/

// read parameters
var parentPage = wiki.getpage($pageid);
var propertyKey = $propertyKey ?? 'mindtouch.idf#featured-articles';

// Add the javascript for this template here in the head section so it is only ever included once.
// This allows the template to be included multiple times but have just this single block of JS
//  controlling it.
<html>
    <head>
        <script type="text/javascript">
            "var propKey = '" .. propertyKey .. "';";
        """
            $(function() {
                $('.link-picker-trigger').bind(Deki.Ui.Events.TreeBrowser.LinkPicked, function(event, linkInfo) {
                    Deki.Special.EditableLinkList._addLink(linkInfo, $(this).attr('parentid'));
                });
                
                $('.mt-linklist-remove-link-button').live('click', function(e) {
                    e.preventDefault();
                    var parentId = $(this).parents('.mt-linklist-links').attr('parentid');
                    var linkId = $(this).parents('li').attr('linkid');
                    Deki.Special.EditableLinkList._removeLink(linkId, parentId);
                });
            });
            
            (function($) {
                Deki.Special.EditableLinkList = {
                    _propertyKey: propKey,
                    _addLink: function(linkInfo, parentId) {
                        var self = this;
                        var pageProp = new Deki.PageProperty(parentId);

                        // Fetch the page property for this product's recommended links.
                        pageProp.getRawProperty(self._propertyKey, function(resp) {
                            var idList = [];
                            if (resp.success) {
                                var idList = $.parseJSON(resp.contents) || [];
                            }
                            if (_(idList).indexOf(linkInfo.id) === -1) {
                                idList.push(linkInfo.id);

                                // Use _.uniq() to remove duplicates
                                idList = _.uniq(idList);

                                // Add the new list of links to the page property
                                pageProp.setProperty(
                                    self._propertyKey, 
                                    JSON.stringify(idList), 
                                    function(setResp) {
                                        if (setResp.success) {
                                            $('.mt-linklist-links[parentid=' + parentId + ']')
                                                .append(self._getNewListItem(linkInfo));
                                        }
                                    },
                                    'never'
                                );
                            }
                        });
                    },
                    _getNewListItem: function(linkInfo) {
                        var removeButton = $('<div></div>').addClass('mt-linklist-remove-link-button');
                        var link = $('<a>' + linkInfo.title + '</a>').attr('href', linkInfo.path);
                        var linkWrapper = $('<div></div>').addClass('mt-linklist-link-admin').append(link);
                        var clearDiv = $('<div></div>').addClass('clear');
                        
                        return $('<li></li>')
                            .attr('linkid', linkInfo.id)
                            .append(removeButton)
                            .append(linkWrapper)
                            .append(clearDiv);
                    },
                    _removeLink: function(idToRemove, parentId) {
                        var self = this;
                        var pageProp = new Deki.PageProperty(parentId);
         
                        // Fetch the page property for this product's recommended links.
                        pageProp.getRawProperty(self._propertyKey, function(resp) {
                            if (resp.success) {
                                var idList = $.parseJSON(resp.contents);
                  
                                // Remove the page ID from the idList array
                                idList = _(idList).without(idToRemove);
                  
                                // Add the new list of links to the page property
                                pageProp.setProperty(
                                    self._propertyKey,
                                    JSON.stringify(idList),
                                    function(setResp) {
                                        if (setResp.success) {
                                            $('ul[parentid=' + parentId + '] li[linkid='+ idToRemove +']').remove();
                                        }
                                    }, 'never'
                                );
                            }
                        });
                    }
                };
            })(jQuery);
        """</script>
    </head>
</html>

var links = parentPage.properties[propertyKey] ? [
    linkedPage foreach
        var id in json.parse(parentPage.properties[propertyKey].text),
        var linkedPage = wiki.getpage(num.cast(id)) !! nil
] : [];

<ul class="mt-linklist-links" parentid=(parentPage.id)>
    foreach (var link in links) {
        if (link.uri) {
        <li linkid=(link.id)>
            var linkClass = 'mt-linklist-link-plain';
            if (user.seated) {
                <div class="mt-linklist-remove-link-button"></>
                let linkClass = 'mt-linklist-link-admin';
            }
            <div class=(linkClass)>
                web.link(link.uri, link.title);
            </>
            <div class="clear"></>
        </>
        }
    }
</ul>

if (user.seated) {
    // class "link-picker-trigger" is required to trigger the link-picker plugin
    // class "mt-linklist-add-link" is for styling in template.css.
    <a href="#" class="link-picker-trigger mt-linklist-add-link" parentid=(parentPage.id)>
        wiki.localize("Page.ProductPage.recommended-add-text");
    </>
}

Page statistics
71 view(s), 3 edit(s) and 6858 character(s)

Tags

This page has no custom tags set.

Comments

You must to post a comment.

Attach file

Attachments