base-datalet.html 14.3 KB
<!--
@license
    The MIT License (MIT)

    Copyright (c) 2015 Dipartimento di Informatica - Universit� di Salerno - Italy

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
-->

<!--
* Developed by :
* ROUTE-TO-PA Project - grant No 645860. - www.routetopa.eu
*
-->

<link rel="import" href="../../bower_components/polymer/polymer.html">

<!--
The `base-datalet` is the base component that includes datalet footer (with information about dataset domain and ROUTE-TO-PA project).
Base datalet includes polymer.html and the BaseDataletBehaviors javascript file that define the datalet workcycle and the datalet base properties : dataUrl, fields and data.
Every datalet must include this one in its `<template>` section.

Example :

    <dom-module id="every-datalet">
        <template>
            ...
            <base-datalet data-url="{{dataUrl}}" fields="{{fields}}"></base-datalet>
            ...
        </template>
    </dom-module>

@element base-datalet
@status v0.1
@homepage
@group datalets
-->

<dom-module id="base-datalet">
    <template>
        <style>
            a {
                color: #00BCD4;
                text-decoration: none;
            }
            a:hover {
                color: #2db395;
                text-decoration: underline;
            }
            #base_datalet_container {
                font-family: 'Roboto', 'Helvetica Neue', Helvetica, Arial, sans-serif;
                padding-top: 2px;
                border-top: 2px solid #B6B6B6;
                margin-top: 8px;
            }
            #base_datalet_rtp {
                display: flex;
                padding-top: 4px;
            }
            #rtp {
                padding-top: 8px;
                padding-right: 8px;
                font-size: 12px;
            }
            #rtpalogo {
                height: 32px;
                width: 32px;
            }
            #base_datalet_spin {
                height: 64px;
                width: 100%;
                margin-top: 64px;
            }

            #base_datalet_source_link{
                padding-top: 12px;
            }

            #span_title{
                font-size: small;
            }

            #span_description{
                font-style: italic;
                color: #727272;
            }
/*EXPORT*/
            .export
            {
                cursor: pointer;
                background-image:url("./static/images/more_horiz_grey600_36x36.png");
                background-repeat: no-repeat;
                position: absolute;
                padding: 31px;
                right: 21px;
                top: 85px;
            }

            .highcharts-contextmenu
            {
                position: absolute;
                right: 50px;
                top: 114px;
                display: none;
                border: solid 1px #000000;
            }

            .highcharts-contextmenu-container
            {
                background: rgb(255, 255, 255);
            }

            .highcharts-contextmenu-container div
            {
                cursor: pointer;
                padding: 0px 10px;
                color: rgb(48, 48, 48);
                font-size: 11px;
                background: none;
            }

            .highcharts-contextmenu-container div:hover
            {
                background: #cccccc;
            }
/*EXPORT*/
        </style>

        <div id="base_datalet_imgWaitDatalet">
            <img id="base_datalet_spin" src="static/images/spin.svg">
        </div>

        <div id="base_datalet_container">

            <div id="base_datalet_title_description">
                <span id="span_title"><b>{{title}}</b> <span id="span_description">{{description}}</span></span>
            </div>

            <div id="base_datalet_source_link">
                <b>Source:</b> <a id="base_datalet_source" target="_blank"></a> (<a id="base_datalet_link" target="_blank">dataset</a>)
            </div>

            <div id="base_datalet_rtp">
                <div id="rtp">Powered by ROUTE-TO-PA</div>
                <div><a href="http://www.routetopa.eu/" target="_blank"><img id="rtpalogo" src="static/images/rtpalogo.png" ></a></div>
            </div>

            <div id="export">
                <div class="export" on-click="showHideExportMenu"></div>
                <div class="highcharts-contextmenu" id="highcharts-contextmenu">
                    <div class="highcharts-contextmenu-container">
                        <div id="export_html" on-click="exportToHTML">Export to HTML</div>
                        <div id="export_png"  on-click="exportToIMG">Export to PNG</div>
                        <div id="export_rtf">Export to RTF</div>
                        <div id="export_my_space" on-click="importToMySpace">Import in My Space</div>
                    </div>
                </div>
            </div>

        </div>

    </template>

    <script src="static/js/BaseDataletBehavior.js"></script>
    <script src="static/js/WorkcycleBehavior.js"></script>

    <script>
        BaseDatalet = Polymer({
            is: 'base-datalet',

            /**
             *
             * It is called after the element's template has been stamped and all elements inside the element's local
             * DOM have been configured (with values bound from parents, deserialized attributes, or else default values)
             * and had their ready method called.
             *
             * Extract
             *   1) the dataset domain
             *   2) the page of a datalet
             *   from the entire URL and set the text content of the datalet footer.
             * @method ready
             */
            ready: function(){

                this.setExportMenu();

                if(this.dataUrl != undefined) {

                    var urlSource = this.dataUrl.split("/")[0] + "//" + this.dataUrl.split("/")[2];
                    this.$.base_datalet_source.innerHTML = urlSource;
                    this.$.base_datalet_source.setAttribute("href", urlSource);


                    //OpenDataSoft check
                    if(this.dataUrl.indexOf("/records/") > -1 )
                    {
                        var i;
                        if(this.dataUrl.indexOf("&") > -1)
                            i = this.dataUrl.indexOf("&");
                        else
                            i = this.dataUrl.length;

                        this.$.base_datalet_link.setAttribute("href", urlSource + "/explore/dataset/" + this.dataUrl.substring(this.dataUrl.indexOf("=")+1, i));
                    }
                    // CKAN
                    else if(this.dataUrl.indexOf("datastore_search?resource_id") > -1 )
                    {
                        var comp = this;
                        $.ajax({
                            url: this.dataUrl.replace("datastore_search?resource_id", "resource_show?id"),
                            dataType: "json",
                            success: function(e){
                                comp.$.base_datalet_link.setAttribute("href", urlSource + "/dataset/" + e.result.package_id + "/resource/" + e.result.id);
                            }
                        });
                    }
                }

                else {
                    this.hideFooter();
                    this.$.base_datalet_spin.remove();
                }
            },

            /**
             * Set the domain url to show in the footer
             */
//            setDomain: function(url){
//                this.$.domain.textContent = url;
//            },

            hideFooter: function() {
                this.$.base_datalet_container.innerHTML = "";
                this.$.base_datalet_container.style.margin = 0;
                this.$.base_datalet_container.style.border = 0;
            },

            setExportMenu: function()
            {
                // xxxx BITMASK. FROM RIGHT : HTML, PNG, RTF, MY SPACE (eg. 1111 show all, 0000 hide all)
                var bitmask = this.export_menu;
                var menu = ["export_html", "export_png", "export_rtf", "export_my_space"];

                if(bitmask == 15) return;

                if(bitmask == 0)
                {
                    this.$.export.style.display = "none";
                    return;
                }

                //var max_i = Math.floor(Math.log2(this.export_menu))+1;
                for(var i=0; i<menu.length; i++)
                {
                    if(bitmask % 2 == 0)
                        this.$[menu[i]].style.display = "none";

                    bitmask = Math.floor(bitmask/2);
                }
            },

            showHideExportMenu: function()
            {
                if(this.$["highcharts-contextmenu"].style.display == "block")
                    this.$["highcharts-contextmenu"].style.display = "none";
                else
                    this.$["highcharts-contextmenu"].style.display = "block";
            },

            exportToHTML: function () {
                var parent = this.parentElement;
                var html = '';
                while(!parent.hasAttribute("data-url"))
                    parent = parent.parentElement;

                html += '<'+parent.is;
                for(var i=0; i<parent.attributes.length; i++)
                    html += ' '+parent.attributes[i].name+'=\''+parent.attributes[i].value+'\'';
                html += '></'+parent.is+'>';

                var dataletImport = '\<script type="text/javascript" src="https://cdn.jsdelivr.net/webcomponentsjs/0.7.16/webcomponents-lite.min.js"\>\<\/script\>\n' +
                        '\<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"\>\<\/script\>\n' +
                        '<!-- REMOVE THE PREVIOUS SCRIPT TAGS IF YOUR PAGE ALREADY USES JQUERY AND POLYFILL LIBRARIES  -->\n';

                var dataletLink = '<link rel="import" href="http://deep.routetopa.eu/COMPONENTS/datalets/'+parent.is+'/'+parent.is+'.html" /> \n';

                window.prompt("Copy to clipboard: Ctrl+C, Enter", dataletImport+dataletLink+html);
            },


            exportToIMG: function () {
                var parent = this.parentElement;
                while(!parent.hasAttribute("data-url"))
                    parent = parent.parentElement;


                var svg = $(parent.is + " .highcharts-container");
                svg = $(svg[0]).html();

                var xhr = new XMLHttpRequest();

                xhr.onreadystatechange = function(){
                    if (this.readyState == 4 && this.status == 200){
                        var blob = new Blob([this.response], {type:'octet/stream'});
                        var downloadUrl = window.URL.createObjectURL(blob);
                        var a = document.createElement("a");
                        a.href = downloadUrl;
                        a.download = "image.png";
                        document.body.appendChild(a);
                        a.click();
                        //window.URL.revokeObjectURL(downloadUrl);
                    }
                };

                //xhr.open('POST', 'http://172.16.15.77/DEEalerProvider/DEEP/export/export-datalet-as-img', true);
                xhr.open('POST', 'http://stage.routetopa.eu/deep/DEEP/export/export-datalet-as-img', true);
                xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

                var post = 'svg_data=' + window.encodeURIComponent(svg);
                xhr.responseType = 'blob';
                xhr.send(post);
            },

            importToMySpace: function() {
                var parent = this.parentElement;
                var html = '';
                while(!parent.hasAttribute("data-url"))
                    parent = parent.parentElement;

                var params = {};
                var fields;
                var component = parent.is;

                for(var i=0; i<parent.attributes.length; i++)
                    params[parent.attributes[i].name] = parent.attributes[i].value;

                delete params["fields"];
                fields = parent.attributes["fields"].value.replace(/\[|\]/g, "");
                params = JSON.stringify(params);

                $.ajax({
                    type: 'post',
                    url: ODE.ajax_private_room_datalet,
                    data: {"component":component, "fields":fields, "params":params, "data":""},
                    dataType: 'JSON',
                    success: function(data){
                        if(data.status == "ok")
                        {
                            alert("Datalet added to Myspace");
                        }
                        else
                        {
                            alert("Error");
                        }
                    },
                    error: function( XMLHttpRequest, textStatus, errorThrown ){
                        OW.error(textStatus);
                    },
                    complete: function(){}
                });
            }

         });
    </script>
</dom-module>