base-datalet.html 8.71 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>
            #base_datalet_container {
                font-family: 'Roboto', 'Helvetica Neue', Helvetica, Arial, sans-serif;
                padding: 16px;
            }
            #base_datalet_rtp {
                display: flex;
                padding-top: 8px;
            }
            #rtp{
                padding-top: 8px;
                padding-right: 8px;
                font-size: small;
            }
            #rtpalogo{
                height:32px;
                width: 32px;
            }
            #image_spin {
                height:32px;
                width: 32px;
                position: relative;
                center: center;
                width: 100%;
             }


        </style>

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

        </div>
        <div id="base_datalet_container">
            <b> Source:</b><span id="domain"></span>
            <b>  </b><span id="domainPage">
        </span>
            <div id="base_datalet_rtp">
                <div id="rtp">Powered by ROUTE-TO-PA</div>
                <div><a href="http://www.routetopa.eu/"><img id="rtpalogo" src="static/images/rtpalogo.png" ></a></div>
            </div>
        </div>

        <!--<b>Source : </b><span id="domain"></span><br><br>-->
        <!--<div id="footer">Powered by Route-to-PA&nbsp&nbsp<a href="http://routetopa.eu/"><img src="static/images/rtpalogo.png" id="rtpalogo"></a></div><br>-->
    </template>

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

    <script>
        BaseDatalet = Polymer({
            is: 'base-datalet',
            objectDatalet: {
                /**
                 * It contains the json data from async xhr call returned from core-ajax core component
                 *
                 * @attribute json_resultDatalet
                 * @type object
                 * @default 'null'.
                 */
                json_resultDatalet: {
                    type: Object,
                    value: {}
                }
            },
            /**
             * 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(){
                var urlDatalet;
                if(this.dataUrl != undefined){

                    var apiIdResource = "resource_show?id=";

                    //Resourceid of a datalet
                    var query = this.dataUrl.substring(this.dataUrl.indexOf("?"), this.dataUrl.length);
                    var params  = this.decomponeQueryString(query);
                    resultIdDatalet = params["resource_id"];

                    //cKan organized with or not catalog
                    queryInitial = this.dataUrl.substring(0,  this.dataUrl.indexOf("api"));
                    queryFinal = this.dataUrl.substring(this.dataUrl.indexOf("api"),  this.dataUrl.indexOf("datastore_search"));

                    urlDatalet = queryInitial +  queryFinal + apiIdResource + resultIdDatalet;
                    urlSource = this.dataUrl.split("/")[0] + "//" + this.dataUrl.split("/")[2];

                    this.$.domainPage.innerHTML = '<a href=' + urlSource +  ' target=' + "_blank>" + urlSource + '</a>'+ "." ;
                    this.requestDataletData(urlDatalet);
                }
            },
            /**
             * Set the domain url to show in the footer
             */
            setDomain: function(url){
                this.$.domain.textContent = url;
            },
            /**
             * Get the (key, value)  query string parameters
             **/
            decomponeQueryString: function (querystring) {
            // remove any preceding url and split
            querystring = querystring.substring(querystring.indexOf('?')+1).split('&');
            var params = {}, pair, d = decodeURIComponent;
            // march and parse
            for (var i = querystring.length - 1; i >= 0; i--) {
                pair = querystring[i].split('=');
                params[d(pair[0])] = d(pair[1]);
            }
            return params;
            },
            /**
             * Make an AJAX call to the dataset URL to retrieve package_id
             *
             * @method urlDatalet
             */
            requestDataletData: function(urlDatalet){
                var comp = this;
                $.ajax({
                    url: urlDatalet, //url con package
                    dataType: "json",
                    success: function(e){
                        comp.handleDataletResponse(e);
                    }
                });
            },
            /**
             * Called when core-ajax component receive the json data from called url.
             *
             * @method handleResponse
             */
            handleDataletResponse: function(e) {
                this.objectDatalet.json_resultDatalet.value = e;

                //packageId of a give datalet
                packageIdDatalet = this.objectDatalet.json_resultDatalet.value.result.package_id;
                resourceId = this.objectDatalet.json_resultDatalet.value.result.id;

                //url complete of download
                urlDatasetDownload = this.objectDatalet.json_resultDatalet.value.result.url;

                if(this.dataUrl != undefined){
                    //static url
                    lastStringResourceId =  this.dataUrl.split("/")[5];
                    resourceId  = lastStringResourceId.split('=')[1];
                    var urlDatalet = this.dataUrl.split("/")[0]+ "//" + this.dataUrl.split("/")[2]  + "/dataset/" + packageIdDatalet +"/resource/" +resourceId;


                    urlDatalet = urlDatasetDownload.replace(/\/download\/.*/, '');
                    urlSource =  this.dataUrl.split("/")[0] + "//" + this.dataUrl.split("/")[2];


                    this.$.domainPage.innerHTML += " (" + ' <a href=' + urlDatalet +  ' target=' + "_blank>" + "Dataset's page" + '</a>' + " )";

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