-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
599 lines (573 loc) · 18.1 KB
/
index.js
File metadata and controls
599 lines (573 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
var cpReporting = {
report_name: 'corepress',
Reports: {
categories: { label: 'Categories', heads: [ 'Name', 'Slug', 'Sidebar', 'Video', 'Feed', 'Sponsorship', 'Image', 'Content Widgets' ] },
menus: { label: 'Menus', heads: [ 'Menu', 'Items' ] },
sidebars: { label: 'Sidebars', heads: [ 'Title', 'Widgets', 'Categories' ] },
skedtool: { label: 'Skedtool', heads: [ 'Name', 'Color' ] },
tags: { label: 'Tags', heads: [ 'Name', 'Slug' ] },
feeds: { label: 'Feeds', heads: [ 'ID', 'Pages', 'Exists', 'Posts', 'Age' ] },
videos: { label: 'Videos', heads: [ 'ID', 'Terms', 'Widgets', 'Exists' ] },
widgets: { label: 'Widgets', heads: [ 'Widget', 'Settings', 'Sponsorship', 'Image' ] },
zones: { label: 'Zones', heads: [ 'Name', 'Slug' ] },
dfp_adparam: { label: 'DFP Params', heads: [ 'Key', 'Default', 'Global', 'Value' ] },
dfp_adsites: { label: 'DFP Sites', heads: [ 'Label', 'Site', 'Criteria' ] },
dfp_adsizes: { label: 'DFP Sizes', heads: [ 'Height', 'Width' ] },
dfp_settings: { label: 'DFP Settings', heads: [ 'Element', 'Value' ] },
dfp_adunits: { label: 'DFP Units', heads: [ 'Name', 'Slug', 'Sizes', 'Parameters' ] },
all_feeds: { label: 'Broken Feeds', heads: [ 'ID', 'Markets' ] },
},
Newsrooms: {
ch: { label: 'Calgary Herald', domain: 'postmediacalgaryherald2.wordpress.com' },
ej: { label: 'Edmonton Journal', domain: 'postmediaedmontonjournal2.wordpress.com' },
lp: { label: 'Regina LeaderPost', domain: 'postmedialeaderpost2.wordpress.com' },
mg: { label: 'Montreal Gazette', domain: 'postmediamontrealgazette2.wordpress.com' },
oc: { label: 'Ottawa Citizen', domain: 'postmediaottawacitizen2.wordpress.com' },
sp: { label: 'Saskatoon StarPhoenix', domain: 'postmediathestarphoenix2.wordpress.com' },
vp: { label: 'Vancouver Province', domain: 'theprovince.wpdqa3.canada.com' },
vs: { label: 'Vancouver Sun', domain: 'postmediavancouversun2.wordpress.com' },
},
mode: '',
market: '',
check_sponsored: true,
check_feed: false,
getTitleCase: function( _str ) {
if ( 'undefined' === typeof _str ) {
return '';
}
return _str.replace( /\w\S*/g, function( _txt ){ return _txt.charAt(0).toUpperCase() + _txt.substr(1).toLowerCase(); } );
},
getFeedUrl: function( _url, _domain ) {
var _label = _url;
if ( 0 < 1 * _url ) {
// SouthPARC
_label = 'SP:' + _url;
_url = 'http://app.canada.com/SouthPARC/service.svc/Content?callingSite=' + _domain + '&contentId=' + _url + '&format=atom&AllLinks=false&maxdocs=40';
}
return { url: _url, label: _label };
},
getNewsroomDomain: function() {
return cpReporting.Newsrooms[ cpReporting.market ].domain;
},
getTermEditUrl: function( _cid, _typ, _data, _label ) {
//console.log(_data );
var _url = 'https://' + cpReporting.getNewsroomDomain() + '/wp-admin/';
switch ( _typ ) {
case 'h':
_url += 'index.php?page=home_layout';
_label = ( '' !== _label ) ? _label : 'Home';
break;
case 'c':
_url += 'edit-tags.php?action=edit&taxonomy=category&post_type=post&tag_ID=' + _cid;
_label = ( '' !== _label ) ? _label : _data.categories[ _cid ].name;
break;
case 't':
_url += 'edit-tags.php?action=edit&taxonomy=post_tag&post_type=post&tag_ID=' + _cid;
_label = ( '' !== _label ) ? _label : _data.tags[ _cid ].name;
break;
default:
break;
}
return { label: _label, url: _url };
},
getNewsroomName: function( _mkt ) {
return ( '' === _mkt ) ? 'All Markets' : cpReporting.Newsrooms[ _mkt ].label;
},
confirmOnPageExit: function ( e ) {
if ( false === document.getElementById( 'pn_cp_offline_mode' ).checked ) {
e.preventDefault();
return;
}
e = e || window.event;
var message = 'Sure?';
// For IE6-8 and Firefox prior to version 4
if ( e ) {
e.returnValue = message;
}
// For Chrome, Safari, IE8+ and Opera 12+
return message;
},
setCookie: function( _cname, _cval, _cdays ) {
var d = new Date();
d.setTime( d.getTime() + ( 365 * 24 * 60 * 60 * 1000 ) );
var _expires = 'expires=' + d.toUTCString();
document.cookie = _cname + '=' + _cval + ';' + _expires;
},
getCookie: function( _cname ) {
var name = _cname + '=';
var ca = document.cookie.split( ';' );
for ( var i = 0; i < ca.length; i++ ) {
var c = ca[ i ];
while ( c.charAt(0) == ' ' ) c = c.substring( 1 );
if ( c.indexOf( name ) == 0 ) return c.substring( name.length, c.length );
}
return '';
},
ObjectToArray: function( _obj ) {
return Object.keys( _obj ).map( function(key) { return _obj[ key ] } );
},
SortObject: function( _obj, _field ) {
// accepts an object, returns a sorted array
var _ary_sorted = [];
if ( 'pn_key' === _field ) {
Object.keys( _obj ).sort().forEach( function( _elem, _idx ) {
_ary_sorted[ _idx ] = { pn_key: _elem, pn_val: _obj[ _elem ] };
} );
//console.log( _ary_sorted ) ;
} else {
cpReporting.ObjectToArray( _obj ).sort( function( a, b ) {
if ( a[ _field ].toLowerCase() > b[ _field ].toLowerCase() ) {
return 1;
}
if ( a[ _field ].toLowerCase() < b[ _field ].toLowerCase() ) {
return -1;
}
return 0;
} ).forEach( function( _elem, _idx ) {
_ary_sorted[ _idx ] = _elem;
} );
}
return _ary_sorted;
},
onChange: function () {
cpReporting.report_name = cpReporting.market + '_' + cpReporting.mode;
ReactDOM.render(
<ReportWrapper mode={ cpReporting.mode } mkt={ cpReporting.market } />,
document.getElementById('pn_cp_report_wrapper')
);
}
};
//cpReporting.market = Object.keys( cpReporting.Newsrooms )[0];
cpReporting.mode = Object.keys( cpReporting.Reports )[0];
window.onbeforeunload = cpReporting.confirmOnPageExit;
//console.log(cpReporting.getCookie( 'offline' ) );
/*
Change this so it loads the JSON first, then renders a list of report links
clicking a report link renders the table using the JSON
*/
var MenuModeRow = React.createClass( {
handleClick: function() {
// this.props.id is the type of report to show menu, skedtool, etc
cpReporting.mode = this.props.id;
cpReporting.onChange();
},
render: function() {
return (
<li ref={ this.props.id } onClick={ this.handleClick } className={ ( ( cpReporting.mode === this.props.id ) ? 'chosen' : '' ) }>{ this.props.label }</li>
);
}
} );
var MenuModes = React.createClass( {
render: function() {
var _rows = [];
Object.keys( cpReporting.Reports ).forEach( function( _elem, _idx ) {
_rows.push( <MenuModeRow key={ _idx } id={ _elem } label={ cpReporting.Reports[ _elem ].label } /> );
} );
return (
<ul id="pn_cp_menu_modes">
{ _rows }
</ul>
);
}
} );
var MarketModeRow = React.createClass( {
handleClick: function() {
// this.props.id is the type of report to show menu, skedtool, etc
cpReporting.market = this.props.id;
//console.log(this.props.id);
cpReporting.onChange();
},
render: function() {
return (
<li ref={ this.props.id } onClick={ this.handleClick } className={ ( ( this.props.id === cpReporting.market ) ? 'chosen' : '' ) }>{ this.props.label }</li>
);
}
} );
var MarketModes = React.createClass( {
render: function() {
var _rows = [];
var _ary = Object.keys( cpReporting.Newsrooms );
_ary.forEach( function (_elem, _idx ) {
_rows.push( <MarketModeRow key={ _idx } id={ _elem } label={ cpReporting.Newsrooms[ _elem ].label } /> );
} );
_rows.push( <MarketModeRow key="-1" id="" label="All Markets" /> );
return (
<ul id="pn_cp_market_modes">{ _rows }</ul>
);
}
} );
var CheckboxSponsored = React.createClass( {
handleClick: function() {
cpReporting.check_sponsored = ! cpReporting.check_sponsored;
cpReporting.onChange();
},
render: function() {
return (
<span><input type="checkbox" onChange={ this.handleClick } checked={ cpReporting.check_sponsored } />Sponsored only</span>
);
}
} );
var CheckboxFeed = React.createClass( {
handleClick: function() {
cpReporting.check_feed = ! cpReporting.check_feed;
cpReporting.onChange();
},
render: function() {
return (
<span><input type="checkbox" onChange={ this.handleClick } checked={ cpReporting.check_feed } />Feed only</span>
);
}
} );
var ReportFilters = React.createClass( {
render: function() {
return (
<div>
<CheckboxSponsored />
<CheckboxFeed />
</div>
);
}
} );
var ReportCellList = React.createClass( {
render: function() {
var _data = this.props.data;
if ( 'object' === typeof _data ) {
return (
<td>{
Object.keys( _data ).map( function( l, _idx ) {
return <span className="pn_cp_list_item">
<a href={ _data[l].url } className={ '' === _data[l].url ? 'pn_cp_table_link_hide' : '' } key={ _idx }>{ _data[l].label }</a>
</span>
} )
}</td>
);
} else {
// nothing passed so return an empty cell
return ( <td></td> );
}
}
} );
var ReportCellImage = React.createClass( {
render: function() {
var _aclass = 'pn_cp_table_link_hide';
var _data = this.props.data;
var _url = this.props.data.url;
var _img = this.props.data.img;
//console.log(this.props.data);
if ( 'object' === typeof _data ) {
if ( '' !== _url ) {
return (
<td><a href={ _url } className={ _aclass }>
<img src={ _img } /></a>
</td>
);
} else {
return (
<td><img src={ _img } /></td>
);
}
} else {
// nothing passed so return an empty cell
return ( <td></td> );
}
}
} );
var ReportCellLabel = React.createClass( {
getLabel: function() {
var _label = this.props.data.label;
switch ( typeof _label ) {
case 'boolean':
_label = _label ? 'T' : 'F';
break;
case 'string':
case 'number':
default:
break;
}
return _label;
},
getCellClass: function() {
var _class = 'pn_cp_table_td_str';
if ( 'string' === typeof this.props.data.clss ) {
_class = 'pn_cp_message';
} else if ( 'number' === typeof this.props.data.cols ) {
_class = 'pn_cp_subhead';
} else {
switch ( typeof this.props.data.label ) {
case 'string':
_class = 'pn_cp_table_td_str';
break;
case 'number':
_class = 'pn_cp_table_td_num';
break;
case 'boolean':
_class = 'pn_cp_table_td_bool';
break;
default:
break;
}
}
return _class;
},
getCellContents: function() {
if ( '' !== this.props.data.url ) {
return <a href={ this.props.data.url } target="_blank">{ this.getLabel() }</a>
} else {
return this.getLabel()
}
},
render: function() {
if ( 'object' === typeof this.props.data ) {
return (
<td
className={ this.getCellClass() }
colSpan={ ( ( 'number' === typeof this.props.data.cols ) ? this.props.data.cols : 1 ) }
>{ this.getCellContents() }</td>
);
} else {
// nothing passed so return an empty cell
return ( <td></td> );
}
}
} );
var ReportRow = React.createClass( {
render: function() {
var cells = [];
var _data = {};
this.props.data.forEach( function( _elem, _idx ) {
if ( 'object' === typeof _elem ) {
switch ( _elem.mode ) {
case 'list':
cells.push( <ReportCellList key={ _idx } data={ _elem.data } /> );
break;
case 'image':
cells.push( <ReportCellImage key={ _idx } data={ _elem.data } /> );
break;
case 'label':
default:
cells.push( <ReportCellLabel key={ _idx } data={ _elem.data } /> );
break;
}
} else {
// just a string
//console.log( _idx ); // on feeds duplicates a child key prop ??
cells.push( <ReportCellLabel key={ _idx } data={ { label: _elem } } /> );
}
} );
return (
<tr>{ cells }</tr>
);
}
} );
var ReportHead = React.createClass( {
render: function() {
return (
<td>{ this.props.data }</td>
);
}
} );
var ReportTable = React.createClass( {
displayRows: function( _mkt, _multi ) {
var _rows = [];
if ( true === _multi ) {
_rows.push( <ReportRow key={ _mkt } data={ [ { mode: 'label', data: { label: cpReporting.Newsrooms[ _mkt ].label, cols: 8 } } ] } /> );
}
Object.keys( this.props.data.cats ).forEach( function( _elem, _idx ) {
//console.log(_elem + '....' + _mkt);
//if ( 'all' === _mkt ) {}
if ( 'undefined' !== typeof this.props.data.cats[ _elem ][ _mkt ] ) {
var _stype = '';
var _scomp = '';
var _surl = '';
var _simg = '';
var _feed = { mode: 'label', data: cpReporting.getFeedUrl( this.props.data.cats[ _elem ][ _mkt ].feed, cpReporting.Newsrooms[ _mkt ].domain ) };
if ( '' !== this.props.data.cats[ _elem ][ _mkt ].spons ) {
//console.log('SPONSORED');
//console.log(this.props.data.cats[ _elem ][ _mkt ].spons);
_stype = cpReporting.getTitleCase( this.props.data.cats[ _elem ][ _mkt ].spons.type.replace( '_', ' ' ) );
_scomp = {
mode: 'label',
data: {
label: this.props.data.cats[ _elem ][ _mkt ].spons.comp,
url: this.props.data.cats[ _elem ][ _mkt ].spons.url,
}
};
_simg = {
mode: 'image',
data: {
img: this.props.data.cats[ _elem ][ _mkt ].spons.img,
url: this.props.data.cats[ _elem ][ _mkt ].spons.url,
}
};
}
if (
( ( ! cpReporting.check_sponsored ) || ( '' !== _stype ) ) &&
( ( ! cpReporting.check_feed ) || ( '' !== this.props.data.cats[ _elem ][ _mkt ].feed ) )
) {
_rows.push( <ReportRow key={ _mkt+_idx } data={ [
_elem,
this.props.data.cats[ _elem ][ _mkt ].name,
_feed,
this.props.data.cats[ _elem ][ _mkt ].video,
this.props.data.cats[ _elem ][ _mkt ].outfits,
_stype,
_scomp,
_simg,
] } /> );
}
}
}.bind( this ) );
if ( ( ( true === _multi ) && ( 1 >= _rows.length ) ) || ( 0 === _rows.length ) ) {
// just the newsroom header so no rows
_rows.push( <ReportRow key={ _mkt+'1' } data={ [ { mode: 'label', data: { label: '- No matches', cols: 8, clss: 'pn_cp_message' } } ] } /> );
}
return ( _rows );
},
render: function() {
var _heads = [];
var _rows = [];
//var _mode = this.props.mode;
//console.log('ReportTable: ' + cpReporting.market);
if ( ! jQuery.isEmptyObject( this.props.data ) ) {
if ( ! jQuery.isEmptyObject( this.props.data.cats ) ) {
var _mode_ary = [];
_mode_ary = [ 'Slug', 'Name', 'Feed', 'Video', 'Outfits', 'Sponsored', 'Company', 'Logo' ];
var _mkt = cpReporting.market;
//cpReporting.market.forEach() {}
if ( '' === _mkt ) {
Object.keys( cpReporting.Newsrooms ).forEach( function( _elem, _idx ) {
_rows.push( this.displayRows( _elem, true ) );
}.bind( this ) );
} else {
_rows.push( this.displayRows( _mkt, false ) );
}
_mode_ary.forEach( function( _ttl ) {
_heads.push( <ReportHead key={ _ttl } data={ _ttl } /> );
}.bind( this ) );
}
}
return (
<div id="pn_cp_report_wrapper">
<div id="pn_cp_report_tools">
</div>
<div id="pn_cp_report_div">
<table id="pn_cp_report_table">
<thead>
<tr>{ _heads }</tr>
</thead>
<tbody>{ _rows }</tbody>
</table>
</div>
</div>
);
}
} );
var ButtonToExcel = React.createClass( {
sendTableToExcel: (
function () {
var _uri = 'data:application/vnd.ms-excel;base64,';
var _template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>';
var _base64 = function ( s ) {
return window.btoa( unescape( encodeURIComponent( s ) ) );
};
var _format = function ( s, c ) {
return s.replace( /{(\w+)}/g, function (m, p) { return c[p]; } );
};
return function() {
var _ctx = {
worksheet: cpReporting.report_name,
table: document.getElementById( 'pn_cp_report_div' ).innerHTML
}
document.getElementById( 'pn_cp_download_link' ).href = _uri + _base64( _format( _template, _ctx ) );
document.getElementById( 'pn_cp_download_link' ).download = 'corepress_' + cpReporting.report_name + '.xls';
document.getElementById( 'pn_cp_download_link' ).click();
};
}
)(),
render: function() {
return (
<input type="button" value="Export to Excel" className="pn_cp_button" onClick={ this.sendTableToExcel } />
);
}
} );
var ReportBox = React.createClass( {
render: function() {
return (
<div className="ReportBox">
<ReportTable data={ this.props.data } />
</div>
);
}
} );
var ReportWrapper = React.createClass( {
getInitialState: function() {
return {
data: {},
};
},
handleUserInput: function( mode ) {
this.setState( {
data: { data },
} );
},
componentDidMount: function() {
var _data = {};
var _count = 0;
var _url = 'all_cats.json';
this.serverRequest = $.get( _url, function( result ) {
_data = result;
_count ++;
this.setState( {
data: _data
} );
console.log( _data );
}.bind( this ) );
},
render: function() {
//var mode_ = this.props.mode;
var title_ = '';
Object.keys( cpReporting.Reports ).forEach( function( _elem ) {
if ( _elem === cpReporting.mode ) {
title_ = cpReporting.Reports[ _elem ].label;
}
} );
return (
<div className="PageWrapper">
<div className="pn_cp_wrapper">
<MarketModes markets={ this.props.markets } />
</div>
<div className="pn_cp_wrapper">
<ReportFilters />
</div>
<div className="pn_cp_wrapper">
<div className="pn_cp_report_title_wrapper">
<div className="pn_cp_report_title">ReactJS Sandbox: { title_ } – { cpReporting.getNewsroomName( this.props.mkt ) }</div>
<ButtonToExcel />
</div>
<ReportBox data={ this.state.data } />
</div>
</div>
);
}
} );
var CheckboxOffline = React.createClass( {
getInitialState: function() {
//console.log( cpReporting.getCookie( 'offline' ) );
return {
offline_mode: ( 'true' === cpReporting.getCookie( 'offline' ) ? true : false ),
};
},
handleClick: function() {
this.setState( { offline_mode: ! this.state.offline_mode } );
cpReporting.setCookie( 'offline', ( ! this.state.offline_mode ), 365 );
},
render: function() {
return (
<input type="checkbox" id="pn_cp_offline_mode" onClick={ this.handleClick } checked={ this.state.offline_mode } />
);
}
} );
/*** set up page once loaded ***/
jQuery( document ).ready( function() {
cpReporting.onChange();
} );