propaganda-chart.html 19 KB
Newer Older
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
1
2
3
4
5
6
7
8
<link rel="import" href="/bower_components/polymer/polymer.html">
<link rel="import" href="/bower_components/iron-icons/iron-icons.html">
<link rel="import" href="/bower_components/paper-dialog/paper-dialog.html">
<link rel="import" href="/bower_components/iron-collapse/iron-collapse.html">


<dom-module id="propaganda-chart">

9
10


Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
11
12
13
14
15
16
<link rel="import" type="css" href="tweet-chart.css">

  <template>

    <!-- Shadow DOM -->
    <paper-material elevation="1">
Daniel Suarez Souto's avatar
Styled    
Daniel Suarez Souto committed
17
18
        <!-- <table class="table table-striped">
      
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
19
20
21
22
23
          <tr>
            <th style="width: 20%; text-align: center">Source</th>
            <th style="width: 50%; text-align: center">Article</th>

          </tr>
Daniel Suarez Souto's avatar
Styled    
Daniel Suarez Souto committed
24
25
       
        </table> -->
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
26
        <div id="nodata">No entries match the current filters</div>
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
27
28
29
        <div style="height: 600px; overflow-x: hidden; overflow-y: auto;">
        <template is="dom-repeat" items="{{data}}" as="news">
          
30
            <div class$="tweet {{checkSentiment(news.sentiments)}} same-height" ident$="subitem[[index]]" style=" text-align: center; min-height: 55px">
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
31
              <div class="row">
32
                <button data-toggle="modal" data-target$="#article[[index]]" style="background: none; border: none; cursor: pointer;" title="Check the article">
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
33
34
                  
                <div class="col-md-12 headline">
35
                  <div class="col-md-2" style="justify-self: center; align-self: center;">
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
36
37
38
                    <img src={{checkSource(news)}} style="width: 100%; display:block; position: relative;" align="left">
                  </div>
                  <div class="col-md-7">
39
40
                      <a class="text" style="font-size: 15px; display: inline-block; vertical-align: middle;"> {{getHeadline(news)}}</a> 
                      
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
41
                  </div>
42
                </div>
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
43
                </button>
44
                <!-- Modal -->
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
45
                <div class="modal fade" id$="article[[index]]" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="overflow: hidden;">
Daniel Suarez Souto's avatar
Styled    
Daniel Suarez Souto committed
46
                  <div class="modal-dialog modal-lg">
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
47
48
49
50
51
52
53
54
55
56
57
58
                    <div class="modal-content" style="height: fit-content;">
                      <div class="modal-header" style="height:80px;background-color: #449DD5; box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.2); border-radius: 5px 5px 0 0;">

                        <!--                         <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> --> 
                        <div class="col-md-12" style="display: flex;flex-direction: row;justify-content: space-around;align-content: center;">
                          <div class="col-md-2 img-wrapper" style="width:13%">
                            <img src="{{checkSource(news)}}" style="width: 100%; display:block; position: relative;" align="left">
                          </div>
                          <div class="col-md-10" style="display: flex; flex-direction: row; justify-content: center; align-content: center;">
                            <h4 class="modal-title" id="myModalLabel" style="display: flex; justify-self: center; align-self: center; color: #fff;display:inline-block; vertical-align: middle;">{{getHeadline(news)}}</h4>
                          </div>
                        </div>
59
                      </div>
60
61
62
                      <div class="modal-body" style="height: 520px" >
                        <div class="col-md-5" style=" overflow-x: hidden; overflow-y: auto; height: 100%;">
                          <br>
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
63
                          <br>
64
65
66
67
68
69
70
71
72
73
74
75
76
77
                          <br>
                          <div class="leyend" style="display: flex; flex-direction: row; justify-content: center; margin-bottom: 5px">
                            <div class="leyend_place" style="display: flex; flex-direction: row;">
                              <div style="width: 7px;height: 7px;background-color: #f90; float: left;    margin-top: 7px;margin-right: 3px; margin-left: 6px" class="style-scope news-chart"></div>
                              Places
                            </div>
                            <div class="leyend_org" style="display: flex; flex-direction: row;">
                              <div style="width: 7px;height: 7px;background-color: #dd4b39; float: left;    margin-top: 7px;margin-right: 3px; margin-left: 6px" class="style-scope news-chart"></div>
                              Organization
                            </div>
                            <div class="leyend_person" style="display: flex; flex-direction: row;">
                              <div style="width: 7px;height: 7px;background-color: #00c0ef; float: left;    margin-top: 7px;margin-right: 3px; margin-left: 6px" class="style-scope news-chart"></div>
                              Person
                            </div>
Daniel Suarez's avatar
Daniel Suarez committed
78
79
80
81
                            <div class="leyend_person" style="display: flex; flex-direction: row;">
                              <div style="width: 7px;height: 7px;background-color: #9b009b; float: left;    margin-top: 7px;margin-right: 3px; margin-left: 6px" class="style-scope news-chart"></div>
                              Category
                            </div>
82
83

                          </div>
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
84
                          <template is="dom-repeat" items="{{news.entities}}" as="entity"> 
85
                            
86
87
                            <span class="badge badge-secondary" on-tap="highlightEntity" data-id$= "{{getID(news)}}" data-text$="{{getArticle(news)}}" style$="{{getBg(entity)}} cursor:pointer;">{{entity.schema:name}}</span>
                            
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
88
                          </template>
Daniel Suarez's avatar
Daniel Suarez committed
89
90
91
92
93
                          <template is="dom-repeat" items="{{news.taxonomies}}" as="category"> 
                            
                            <span class="badge badge-secondary" on-tap="highlightCategory" data-id$= "{{getID(news)}}" data-text$="{{getArticle(news)}}" style="background-color: #9b009b;margin-bottom:5px; cursor:pointer;">{{getCategory(category)}}</span>
                            
                          </template>
94
95
                        </div>                        

96
97
                        <div class="col-md-7" style="overflow-x: hidden; overflow-y: auto; height: 100%;">
                          <ul class="nav nav-tabs" role="tablist">
98
99
                            <li class="active" id="tabText"><a data-toggle="tab" href$="#propaganda[[index]]">Text</a></li>
                            <li id="tabArticle"><a data-toggle="tab" href$="#pdf[[index]]">Magazine</a></li>
100
101
                          </ul>
                          <div class="tab-content">
102
                            <div id$="propaganda[[index]]" class="tab-pane fade in active" style="background-color: #eeeeee96; float: right; border: 1px solid #B3BFC7; padding: 20px; overflow: auto; height: calc(100% - 45px); width: 100%;">
103
104
105
106
107
                              <div id$="{{getID(news)}}" style="text-align: justify; white-space: pre-line;">
                                {{getArticle(news)}}
                              </div>
                              
                            </div>
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
108
                            <div id$="pdf[[index]]" class="tab-pane fade" iframe-src$="{{setPDF(news, index)}}">
109
                              
110
111
                            </div>
                          </div>                   
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
112
                        </div>
113
                      </div>
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
114

115
116
                      <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
117
118
119
                      </div>
                    </div>
                  </div>
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
120
121
                </div>
              </div>        
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
122
            </div>
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
123

Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
124
125
              <!-- <iron-collapse ident$="subitem[[index]]">
                <div class="content" style="text-align:center; width: 94%; margin: 0 auto;">
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
126
                <br>
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
127
                <template is="dom-repeat" items="{{news.entities}}" as="entity"> 
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
128
              
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
129
                  <span class="badge badge-secondary" on-tap="filter" style$="{{getBg(entity)}}">{{entity.nif:anchorOf}}</span>
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
130
          
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
131
                </template>
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
132
133
              

Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
134
135
              </iron-collapse>
              </div> -->
136
            
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
        </template>
        <br>

    </paper-material>

  </template>

  <script>
    var filtered = false;

    Polymer({

      is: 'propaganda-chart',

      properties: {

        data: {
            type: Array,
            value: [] //default value for data array.
        },

        icon: {
          type: String,
          value: "face"
        },

        title: {
          type: String,
          value: "List of different tweets"
        },
        buttontext:{
          type: String,
          value : "See more"
        },

        brand: {
          type: String,
          value: "Undefined"
        },

        query: {
          type: String,
          observer: '_queryChanged'
        },

        index: {
          type: String
        },

        subindex: {
          type: String
        },

        fields: {
          type: Array,
          value: function() { return []; }
        },

        datos:{
          type: Object,
          observer: '_dataChanged'
        },

        field: {
          type: String
        },

        host: {
          type: String
        },

        param: {
          type: String,
          notify: true
        },

        filters: {
          type: Array,
          notify: true,
          value: function() { return []; }
        }
      },

      ready: function() {   

      },

      _dataChanged: function() {
        var that = this
        var hits = this.datos.hits.hits;
        var results = []
        //console.log(hits)
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
229

Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
230
        hits.forEach(function(entry) {
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
231
232
           //console.log(entry._source)
           if(entry._source['@type']=="schema:Article") results.push(entry._source);
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
233
        });
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
234
235
236
237
238
        if(results.length==0){
          this.$.nodata.style.visibility = 'visible';
        }else{
          this.$.nodata.style.visibility = 'hidden';
        }
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
239
        //console.log(results)
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
240
241
242
243
        that.data = results
      },
      getHeadline: function(news){
        //console.log(news['http://schema.org/headline'][0]['@value'])
244
        return news['schema:headline']
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
245
      },
246
247
      getArticle: function(news){
        //console.log(news['http://schema.org/headline'][0]['@value'])
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
        text = news['schema:articleBody']
        //console.log(text)
        //text=text.replace('↵','\n')
        text=text.replace(' \n','\n')
        text=text.replace('\n\f','\n')
        text=text.replace('', '') 
        text=text.replace(/\s{2,}/g, ' ')
        text=text.replace('', '')
        text=text.replace('', '')
        text=text.replace('', '')  
        text=text.replace('','')
        text=text.replace('', '')
        text=text.replace('', '')  
        text=text.replace('','')
        //console.log(text)
        return text
264
      },
265
      getID: function(news){
266
        //console.log(news['@id'])
267
268
        return news['@id']
      },
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
269
270
271
272
273
274
275
276
      getDate: function(date){
        var fecha = date.toString().split("-");
        //console.log(fecha)
        return fecha[2].split("T")[0]+"-"+fecha[1]+"-"+fecha[0]
      },
      getBg: function(entity){
        if(!("@type" in entity)){
          return "background-color: brown;margin-bottom:5px;"
277
        } else if(entity["@type"][0] == "schema:Place") {
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
278
          return "background-color: #f90;margin-bottom:5px;"
279
        } else if(entity["@type"][0] == "schema:Organization") {
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
280
          return "background-color: #dd4b39;margin-bottom:5px;"
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
281
        } else {
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
282
          return "background-color: #00c0ef;margin-bottom:5px;"
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
        }

      },
      checkSentiment: function(sentiment) {
        //console.log(sentiment[0]['marl:hasPolarity'])
        try{
          if (sentiment[0]['marl:hasPolarity'] == "marl:Positive")
            return "tweet positive-news"
          else if (sentiment[0]['marl:hasPolarity'] == "marl:Negative")
            return "tweet negative-news"
          else 
            return "tweet neutral-news"
        }
        catch (e){
          //console.log(e)
          return "tweet neutral-news"
        }
      },
      getUri: function(news){
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
302
        
303
        var uri="http://trivalent-resources.cluster.gsi.dit.upm.es/"+news['magazine'].toLowerCase()+"/"+news['magazine'].toLowerCase()+news['number']+".pdf#page="+news['schema:pageStart']+"&view=FitB";
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
304
305
306
307
308
        
        //console.log(uri)

        //var uri= "/magazines/"+news['magazine'].toLowerCase()+"/data/"+news['magazine'].toLowerCase()+news['number']+".pdf";
        //console.log(uri)
309
        //return uri;
310
        //return '/resources/'+(news['@id'].split('/')[4]);
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
      },
      getemotion: function(emotion) {
        //console.log(emotion)
        return emotion[0]['onyx:hasEmotion']['onyx:hasEmotionCategory'].split(':')[1]
      },
      checkSpanSentiment: function(sentiment) {
        //console.log(sentiment)
        if (sentiment == "marl:Positive")
          return "span positive"
        else if (sentiment == "marl:Negative")
          return "span negative"
        else 
          return "span neutral"
      },

      checkSource: function(source) {
        
        aaa = source['@id']
        //source = source.toLowerCase()
        if (aaa.indexOf("Dabiq") != -1){
          return "/images/dabiq_logo.png"
        } else {
          return "/images/rumiyah_logo.png"
        }
        
        
      },
      checkEmotion: function(source) {
        if(source[0]['onyx:hasEmotion']['onyx:hasEmotionCategory'].indexOf("anger") > -1)
          return "/images/emotion/anger.png"
        else if(source[0]['onyx:hasEmotion']['onyx:hasEmotionCategory'].indexOf("disgust") > -1)
          return "/images/emotion/disgust.png"
        else if(source[0]['onyx:hasEmotion']['onyx:hasEmotionCategory'].indexOf("fear") > -1)
          return "/images/emotion/fear.png"
        else if(source[0]['onyx:hasEmotion']['onyx:hasEmotionCategory'].indexOf("joy") > -1)
          return "/images/emotion/joy.png"
        else if(source[0]['onyx:hasEmotion']['onyx:hasEmotionCategory'].indexOf("neutral") > -1)
          return "/images/emotion/neutral.png"
        else if(source[0]['onyx:hasEmotion']['onyx:hasEmotionCategory'].indexOf("sadness") > -1)
          return "/images/emotion/sadness.png"
      },
352
      highlightEntity: function(e) {
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
353

354
355
356
        var id = e.target.dataset.id 
        var element = document.getElementById(id)
        var text = e.target.dataset.text
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
357
        var text_highlighted=""
358
        var beginIndex = e.model.__data__.entity["nif:beginIndex"]
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
359
        var endIndex = e.model.__data__.entity["nif:endIndex"] 
360
        var cnt=0
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
361
362

        if (beginIndex[0] >= 0) { 
363
364
365
366
367
368
369
370
371
372
373
374
375
          if(text[beginIndex[0]]===' ') cnt+=1
          text_highlighted = text.substring(0,beginIndex[0]+cnt) + "<span class='highlight' style='background-color:yellow'>" + text.substring(beginIndex[0]+cnt,endIndex[0]+1+cnt) + "</span>"
          for(var i =1 ; i<beginIndex.length;i++){
            if(text[beginIndex[i]+cnt]===' ') cnt+=1
            text_highlighted+=text.substring(endIndex[i-1]+1+cnt,beginIndex[i]+cnt)
            text_highlighted+="<span class='highlight' style='background-color:yellow'>"
            text_highlighted+=text.substring(beginIndex[i]+cnt,endIndex[i]+1+cnt)
            text_highlighted+="</span>"
          }
          text_highlighted+= text.substring(endIndex[endIndex.length-1]+1+cnt)
          element.innerHTML = text_highlighted
          //console.log(text_highlighted)
          //console.log(cnt)
376
377
378
379
380
381
382
383

          // Select the tab
          $('#tabText a').tab('show')
          //Dynamic Scroll
          setTimeout(function() {
            $('#'+element.parentElement.id).animate({scrollTop: $('#'+element.parentElement.id).scrollTop() + $('#'+element.parentElement.id + ' span').position().top - 200}, 400)  
          }, 200)
          
384
        }
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
385
      },
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
386
387
      setPDF: function(news, index) {
        function loadPDF() {
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
388
389
          /*var uri= "http://trivalent-resources.cluster.gsi.dit.upm.es/"+news['magazine'].toLowerCase()+"/"+news['magazine'].toLowerCase()+news['number']+".pdf#page="+news['schema:pageStart'];*/
          var uri= "http://localhost:8080/magazines/"+news['magazine'].toLowerCase()+"/data/"+news['magazine'].toLowerCase()+news['number']+".pdf#page="+news['schema:pageStart'];
390
          //console.log(uri)
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
391
          $('#article'+index).off();
392
393
394
395
396
          $('#article'+index+' li#tabArticle a').on('shown.bs.tab', function(event) {
            if ($('#article'+index+' iframe').length === 0) {
              var iframe = $('<iframe>').attr('src', uri).width('100%').height('91%');
              $('#article'+index+' #pdf'+index).html(iframe);
            }
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
397
398
          });
        }
399
        
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
400
401
402
403
404
405
406
407
408
        if (window.loaded) {
          loadPDF();
        } else {
          $('#article'+index).ready(function() {
            loadPDF();   
          });
        }
        return news['@id'];
      },
Daniel Suarez's avatar
Daniel Suarez committed
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
      highlightCategory: function(e) {
        console.log(e)
        var id = e.target.dataset.id 
        var element = document.getElementById(id)
        var text = e.target.dataset.text
        var text_highlighted= ""
        var beginIndex = e.model.__data__.category["nif:beginIndex"]
        var endIndex = e.model.__data__.category["nif:endIndex"]+1 
        var cnt=0
        
        console.log(beginIndex)
        console.log(endIndex)
        var subtext = text.substring(0,beginIndex)
 
        if (beginIndex >= 0) { 
         text = text.substring(0,beginIndex) + "<span class='highlight' style='background-color:yellow'>" + text.substring(beginIndex,endIndex) + "</span>" + text.substring(endIndex)
         element.innerHTML = text
         //console.log(text)

         //console.log($('#'+element.parentElement.id+' span').position().top)
         $('#'+element.parentElement.id).animate({scrollTop: $('#'+element.parentElement.id).scrollTop() + $('#'+element.parentElement.id + ' span').position().top - 200}, 400)
        }

        //var subtext = text.substring(0,beginIndex)
 
        /*if (beginIndex[0] >= 0) { 
         if(text[beginIndex[0]]===' ') cnt+=1
         text_highlighted = text.substring(0,beginIndex[0]+cnt) + "<span class='highlight' style='background-color:yellow'>" + text.substring(beginIndex[0]+cnt,endIndex[0]+1+cnt) + "</span>"
         for(var i =1 ; i<beginIndex.length;i++){
          if(text[beginIndex[i]+cnt]===' ') cnt+=1
          text_highlighted+=text.substring(endIndex[i-1]+1+cnt,beginIndex[i]+cnt)
          text_highlighted+="<span class='highlight' style='background-color:yellow'>"
          text_highlighted+=text.substring(beginIndex[i]+cnt,endIndex[i]+1+cnt)
          text_highlighted+="</span>"
         }
         text_highlighted+= text.substring(endIndex[endIndex.length-1]+1+cnt)
         element.innerHTML = text_highlighted*/
         //console.log(text_highlighted)
         //console.log(cnt)


         //console.log($('#'+element.parentElement.id+' span').position().top)
         $('#'+element.parentElement.id).animate({scrollTop: $('#'+element.parentElement.id).scrollTop() + $('#'+element.parentElement.id + ' span').position().top - 200}, 400)
        
      },
     
Daniel Suarez Souto's avatar
Daniel Suarez Souto committed
455
456
457
458
    });
  </script>

</dom-module>