ckan-liip /ckan/public/scripts/application.js

Language Javascript Lines 1892
MD5 Hash 18bc14b8d36833380c797b35347d235c
Repository https://bitbucket.org/philippkueng/ckan-liip.git View Raw File View Project SPDX
| Open JSFiddle
   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
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
var CKAN = CKAN || {};

CKAN.View = CKAN.View || {};
CKAN.Model = CKAN.Model || {};
CKAN.Utils = CKAN.Utils || {};

/* ================================= */
/* == Initialise CKAN Application == */
/* ================================= */
(function ($) {
  $(document).ready(function () {
    CKAN.Utils.relatedSetup($("#form-add-related"));
    CKAN.Utils.setupUserAutocomplete($('input.autocomplete-user'));
    CKAN.Utils.setupOrganizationUserAutocomplete($('input.autocomplete-organization-user'));
    CKAN.Utils.setupGroupAutocomplete($('input.autocomplete-group'));
    CKAN.Utils.setupAuthzGroupAutocomplete($('input.autocomplete-authzgroup'));
    CKAN.Utils.setupPackageAutocomplete($('input.autocomplete-dataset'));
    CKAN.Utils.setupTagAutocomplete($('input.autocomplete-tag'));
    $('input.autocomplete-format').live('keyup', function(){
      CKAN.Utils.setupFormatAutocomplete($(this));
    });
    CKAN.Utils.setupMarkdownEditor($('.markdown-editor'));
    // bootstrap collapse
    $('.collapse').collapse({toggle: false});

    // Buttons with href-action should navigate when clicked
    $('input.href-action').click(function(e) {
      e.preventDefault();
      window.location = ($(e.target).attr('action'));
    });

    var isGroupView = $('body.group.read').length > 0;
    if (isGroupView) {
      // Show extract of notes field
      CKAN.Utils.setupNotesExtract();
    }

    var isDatasetView = $('body.package.read').length > 0;
    if (isDatasetView) {
      // Show extract of notes field
      CKAN.Utils.setupNotesExtract();
    }

    var isResourceView = $('body.package.resource_read').length > 0;
    if (isResourceView) {
      CKAN.DataPreview.loadPreviewDialog(preload_resource);
    }

    var isEmbededDataviewer = $('body.package.resource_embedded_dataviewer').length > 0;
    if (isEmbededDataviewer) {
      CKAN.DataPreview.loadEmbeddedPreview(preload_resource, reclineState);
    }

    if ($(document.body).hasClass('search')) {
      // Calculate the optimal width for the search input regardless of the
      // width of the submit button (which can vary depending on translation).
      (function resizeSearchInput() {
        var form = $('#dataset-search'),
            input = form.find('[name=q]'),
            button = form.find('[type=submit]'),
            offset = parseFloat(button.css('margin-left'));

        // Grab the horizontal properties of the input that affect the width.
        $.each(['padding-left', 'padding-right', 'border-left-width', 'border-right-width'], function (i, prop) {
          offset += parseFloat(input.css(prop)) || 0;
        });

        input.width(form.outerWidth() - button.outerWidth() - offset);
      })();
    }

    var isDatasetNew = $('body.package.new').length > 0;
    if (isDatasetNew) {
      // Set up magic URL slug editor
      var urlEditor = new CKAN.View.UrlEditor({
        slugType: 'package'
      });
      $('#save').val(CKAN.Strings.addDataset);
      $("#title").focus();
    }
    var isGroupNew = $('body.group.new').length > 0;
    if (isGroupNew) {
      // Set up magic URL slug editor
      var urlEditor = new CKAN.View.UrlEditor({
        slugType: 'group'
      });
      $('#save').val(CKAN.Strings.addGroup);
      $("#title").focus();
    }

    var isDatasetEdit = $('body.package.edit').length > 0;
    if (isDatasetEdit) {
      CKAN.Utils.warnOnFormChanges($('form#dataset-edit'));
      var urlEditor = new CKAN.View.UrlEditor({
          slugType: 'package'
      });

      // Set up dataset delete button
      CKAN.Utils.setupDatasetDeleteButton();
    }
    var isDatasetResourceEdit = $('body.package.editresources').length > 0;
    if (isDatasetNew || isDatasetResourceEdit) {
      // Selectively enable the upload button
      var storageEnabled = $.inArray('storage',CKAN.plugins)>=0;
      if (storageEnabled) {
        $('li.js-upload-file').show();
      }
      // Backbone collection class
      var CollectionOfResources = Backbone.Collection.extend({model: CKAN.Model.Resource});
      // 'resources_json' was embedded into the page
      var view = new CKAN.View.ResourceEditor({
        collection: new CollectionOfResources(resources_json),
        el: $('form#dataset-edit')
      });
      view.render();

      $( ".drag-drop-list" ).sortable({
        distance: 10
      });
      $( ".drag-drop-list" ).disableSelection();
    }

    var isGroupEdit = $('body.group.edit').length > 0;
    if (isGroupEdit) {
      var urlEditor = new CKAN.View.UrlEditor({
          slugType: 'group'
      });
    }
	// OpenID hack
	// We need to remember the language we are using whilst logging in
	// we set this in the user session so we don't forget then
	// carry on as before.
	if (window.openid && openid.signin){
		openid._signin = openid.signin;
		openid.signin = function (arg) {
			$.get(CKAN.SITE_URL + '/user/set_lang/' + CKAN.LANG, function (){openid._signin(arg);})
		};
	}
	if ($('#login').length){
		$('#login').submit( function () {
			$.ajax(CKAN.SITE_URL + '/user/set_lang/' + CKAN.LANG, {async:false});
		});
	}
  });
}(jQuery));

/* =============================== */
/* jQuery Plugins                  */
/* =============================== */

jQuery.fn.truncate = function (max, suffix) {
  return this.each(function () {
    var element = jQuery(this),
        isTruncated = element.hasClass('truncated'),
        cached, length, text, expand;

    if (isTruncated) {
      element.html(element.data('truncate:' + (max === 'expand' ? 'original' : 'truncated')));
      return;
    }

    cached  = element.text();
    length  = max || element.data('truncate') || 30;
    text    = cached.slice(0, length);
    expand  = jQuery('<a href="#" />').text(suffix || 'Âť');

    // Bail early if there is nothing to truncate.
    if (cached.length < length) {
      return;
    }

    // Try to truncate to nearest full word.
    while ((/\S/).test(text[text.length - 1])) {
      text = text.slice(0, text.length - 1);
    }

    element.html(jQuery.trim(text));
    expand.appendTo(element.append(' '));
    expand.click(function (event) {
      event.preventDefault();
      element.html(cached);
    });

    element.addClass('truncated');
    element.data('truncate:original',  cached);
    element.data('truncate:truncated', element.html());
  });
};

/* =============================== */
/* Backbone Model: Resource object */
/* =============================== */
CKAN.Model.Resource = Backbone.Model.extend({
  constructor: function Resource() {
    Backbone.Model.prototype.constructor.apply(this, arguments);
  },
  toTemplateJSON: function() {
    var obj = Backbone.Model.prototype.toJSON.apply(this, arguments);
    obj.displaytitle = obj.description ? obj.description : 'No description ...';
    return obj;
  }
});



/* ============================== */
/* == Backbone View: UrlEditor == */
/* ============================== */
CKAN.View.UrlEditor = Backbone.View.extend({
  initialize: function() {
    _.bindAll(this,'titleToSlug','titleChanged','urlChanged','checkSlugIsValid','apiCallback');

    // Initial state
    var self = this;
    this.updateTimer = null;
    this.titleInput = $('.js-title');
    this.urlInput = $('.js-url-input');
    this.validMsg = $('.js-url-is-valid');
    this.lengthMsg = $('.url-is-long');
    this.lastTitle = "";
    this.disableTitleChanged = false;

    // Settings
    this.regexToHyphen = [ new RegExp('[ .:/_]', 'g'),
                      new RegExp('[^a-zA-Z0-9-_]', 'g'),
                      new RegExp('-+', 'g')];
    this.regexToDelete = [ new RegExp('^-*', 'g'),
                      new RegExp('-*$', 'g')];

    // Default options
    if (!this.options.apiUrl) {
      this.options.apiUrl = CKAN.SITE_URL + '/api/2/util/is_slug_valid';
    }
    if (!this.options.MAX_SLUG_LENGTH) {
      this.options.MAX_SLUG_LENGTH = 90;
    }
    this.originalUrl = this.urlInput.val();

    // Hook title changes to the input box
    CKAN.Utils.bindInputChanges(this.titleInput, this.titleChanged);
    CKAN.Utils.bindInputChanges(this.urlInput, this.urlChanged);

    // If you've bothered typing a URL, I won't overwrite you
    function disable() {
      self.disableTitleChanged = true;
    };
    this.urlInput.keyup   (disable);
    this.urlInput.keydown (disable);
    this.urlInput.keypress(disable);

    // Set up the form
    this.urlChanged();
  },

  titleToSlug: function(title) {
    var slug = title;
    $.each(this.regexToHyphen, function(idx,regex) { slug = slug.replace(regex, '-'); });
    $.each(this.regexToDelete, function(idx,regex) { slug = slug.replace(regex, ''); });
    slug = slug.toLowerCase();

    if (slug.length<this.options.MAX_SLUG_LENGTH) {
        slug=slug.substring(0,this.options.MAX_SLUG_LENGTH);
    }
    return slug;
  },

  /* Called when the title changes */
  titleChanged:  function() {
    if (this.disableTitleChanged) { return; }
    var title = this.titleInput.val();
    if (title == this.lastTitle) { return; }
    this.lastTitle = title;

    slug = this.titleToSlug(title);
    this.urlInput.val(slug);
    this.urlInput.change();
  },

  /* Called when the url is changed */
  urlChanged: function() {
    var slug = this.urlInput.val();
    if (this.updateTimer) { clearTimeout(this.updateTimer); }
    if (slug.length<2) {
      this.validMsg.html('<span style="font-weight: bold; color: #444;">'+CKAN.Strings.urlIsTooShort+'</span>');
    }
    else if (slug==this.originalUrl) {
      this.validMsg.html('<span style="font-weight: bold; color: #000;">'+CKAN.Strings.urlIsUnchanged+'</span>');
    }
    else {
      this.validMsg.html('<span style="color: #777;">'+CKAN.Strings.checking+'</span>');
      var self = this;
      this.updateTimer = setTimeout(function () {
        self.checkSlugIsValid(slug);
      }, 200);
    }
    if (slug.length>20) {
      this.lengthMsg.show();
    }
    else {
      this.lengthMsg.hide();
    }
  },

  checkSlugIsValid: function(slug) {
    $.ajax({
      url: this.options.apiUrl,
      data: 'type='+this.options.slugType+'&slug=' + slug,
      dataType: 'jsonp',
      type: 'get',
      jsonpCallback: 'callback',
      success: this.apiCallback
    });
  },

  /* Called when the slug-validator gets back to us */
  apiCallback: function(data) {
    if (data.valid) {
      this.validMsg.html('<span style="font-weight: bold; color: #0c0">'+CKAN.Strings.urlIsAvailable+'</span>');
    } else {
      this.validMsg.html('<span style="font-weight: bold; color: #c00">'+CKAN.Strings.urlIsNotAvailable+'</span>');
    }
  }
});


/* =================================== */
/* == Backbone View: ResourceEditor == */
/* =================================== */
CKAN.View.ResourceEditor = Backbone.View.extend({
  initialize: function() {
    // Init bindings
    _.bindAll(this, 'resourceAdded', 'resourceRemoved', 'sortStop', 'openFirstPanel', 'closePanel', 'openAddPanel');
    this.collection.bind('add', this.resourceAdded);
    this.collection.bind('remove', this.resourceRemoved);
    this.collection.each(this.resourceAdded);
    this.el.find('.resource-list-edit').bind("sortstop", this.sortStop);

    // Delete the barebones editor. We will populate our own form.
    this.el.find('.js-resource-edit-barebones').remove();

    // Warn on form changes
    var flashWarning = CKAN.Utils.warnOnFormChanges(this.el);
    this.collection.bind('add', flashWarning);
    this.collection.bind('remove', flashWarning);

    // Trigger the Add Resource pane
    this.el.find('.js-resource-add').click(this.openAddPanel);

    // Tabs for adding resources
    new CKAN.View.ResourceAddUrl({
      collection: this.collection,
      el: this.el.find('.js-add-url-form'),
      mode: 'file'
    });
    new CKAN.View.ResourceAddUrl({
      collection: this.collection,
      el: this.el.find('.js-add-api-form'),
      mode: 'api'
    });
    new CKAN.View.ResourceAddUpload({
      collection: this.collection,
      el: this.el.find('.js-add-upload-form')
    });


    // Close details button
    this.el.find('.resource-panel-close').click(this.closePanel);

    // Did we embed some form errors?
    if (typeof global_form_errors == 'object') {
      if (global_form_errors.resources) {
        var openedOne = false;
        for (i in global_form_errors.resources) {
          var resource_errors = global_form_errors.resources[i];
          if (CKAN.Utils.countObject(resource_errors) > 0) {
            var resource = this.collection.at(i);
            resource.view.setErrors(resource_errors);
            if (!openedOne) {
              resource.view.openMyPanel();
              openedOne = true;
            }
          }
        }
      }
    }
    else {
      // Initial state
      this.openFirstPanel();
    }
  },
  /*
   * Called when the page loads or the current resource is deleted.
   * Reset page state to the first available edit panel.
   */
  openFirstPanel: function() {
    if (this.collection.length>0) {
      this.collection.at(0).view.openMyPanel();
    }
    else {
      this.openAddPanel();
    }
  },
  /*
   * Open the 'Add New Resource' special-case panel on the right.
   */
  openAddPanel: function(e) {
    if (e) { e.preventDefault(); }
    var panel = this.el.find('.resource-panel');
    var addLi = this.el.find('.resource-list-add > li');
    this.el.find('.resource-list > li').removeClass('active');
    $('.resource-details').hide();
    this.el.find('.resource-details.resource-add').show();
    addLi.addClass('active');
    panel.show();
  },
  /*
   * Close the panel on the right.
   */
  closePanel: function(e) {
    if (e) { e.preventDefault(); }
    this.el.find('.resource-list > li').removeClass('active');
    this.el.find('.resource-panel').hide();
  },
  /*
   * Update the resource__N__field names to match
   * new sort order.
  */
  sortStop: function(e,ui) {
    this.collection.each(function(resource) {
      // Ask the DOM for the new sort order
      var index = resource.view.li.index();
      resource.view.options.position = index;
      // Update the form element names
      var table = resource.view.table;
      $.each(table.find('input,textarea,select'), function(input_index, input) {
        var name = $(input).attr('name');
        if (name) {
          name = name.replace(/(resources__)\d+(.*)/, '$1'+index+'$2');
          $(input).attr('name',name);
        }
      });
    });
  },
  /*
   * Calculate id of the next resource to create
   */
  nextIndex: function() {
    var maxId=-1;
    var root = this.el.find('.resource-panel');
    root.find('input').each(function(idx,input) {
      var name = $(input).attr('name') || '';
      var splitName=name.split('__');
      if (splitName.length>1) {
        var myId = parseInt(splitName[1],10);
        maxId = Math.max(myId, maxId);
      }
    });
    return maxId+1;
  },
  /*
   * Create DOM elements for new resource.
   */
  resourceAdded: function(resource) {
    var self = this;
    resource.view = new CKAN.View.Resource({
      position: this.nextIndex(),
      model: resource,
      callback_deleteMe: function() { self.collection.remove(resource); }
    });
    this.el.find('.resource-list-edit').append(resource.view.li);
    this.el.find('.resource-panel').append(resource.view.table);
    if (resource.isNew()) {
      resource.view.openMyPanel();
    }
  },
  /*
   * Destroy DOM elements for deleted resource.
   */
  resourceRemoved: function(resource) {
    resource.view.removeFromDom();
    delete resource.view;
    this.openFirstPanel();
  }
});


/* ============================== */
/* == Backbone View: Resource == */
/* ============================== */

CKAN.View.Resource = Backbone.View.extend({
  initialize: function() {
    this.el = $(this.el);
    _.bindAll(this,'updateName','updateIcon','name','askToDelete','openMyPanel','setErrors','setupDynamicExtras','addDynamicExtra' );
    this.render();
  },
  render: function() {
    this.raw_resource = this.model.toTemplateJSON();
    var resource_object = {
        resource: this.raw_resource,
        num: this.options.position,
        resource_icon: '/images/icons/page_white.png',
        resourceTypeOptions: [
          ['file', CKAN.Strings.dataFile],
          ['api', CKAN.Strings.api],
          ['visualization', CKAN.Strings.visualization],
          ['image', CKAN.Strings.image],
          ['metadata', CKAN.Strings.metadata],
          ['documentation', CKAN.Strings.documentation],
          ['code', CKAN.Strings.code],
          ['example', CKAN.Strings.example]
        ]
    };
    // Generate DOM elements
    this.li = $($.tmpl(CKAN.Templates.resourceEntry, resource_object));
    this.table = $($.tmpl(CKAN.Templates.resourceDetails, resource_object));

    // Hook to changes in name
    this.nameBox = this.table.find('input.js-resource-edit-name');
    this.descriptionBox = this.table.find('textarea.js-resource-edit-description');
    CKAN.Utils.bindInputChanges(this.nameBox,this.updateName);
    CKAN.Utils.bindInputChanges(this.descriptionBox,this.updateName);
    // Hook to changes in format
    this.formatBox = this.table.find('input.js-resource-edit-format');
    CKAN.Utils.bindInputChanges(this.formatBox,this.updateIcon);
    // Hook to open panel link
    this.li.find('.resource-open-my-panel').click(this.openMyPanel);
    this.table.find('.js-resource-edit-delete').click(this.askToDelete);
    // Hook to markdown editor
    CKAN.Utils.setupMarkdownEditor(this.table.find('.markdown-editor'));

    // Set initial state
    this.updateName();
    this.updateIcon();
    this.setupDynamicExtras();
    this.hasErrors = false;
  },
  /*
   * Process a JSON object of errors attached to this resource
   */
  setErrors: function(obj) {
    if (CKAN.Utils.countObject(obj) > 0) {
      this.hasErrors = true;
      this.errors = obj;
      this.li.addClass('hasErrors');
      var errorList = $('<dl/>').addClass('errorList');
      $.each(obj,function(k,v) {
        var errorText = '';
        var newLine = false;
        $.each(v,function(index,value) {
          if (newLine) { errorText += '<br/>'; }
          errorText += value;
          newLine = true;
        });
        errorList.append($('<dt/>').html(k));
        errorList.append($('<dd/>').html(errorText));
      });
      this.table.find('.resource-errors').append(errorList).show();
    }
  },
  /*
   * Work out what I should be called. Rough-match
   * of helpers.py:resource_display_name.
   */
  name: function() {
    var name = this.nameBox.val();
    if (!name) {
      name = this.descriptionBox.val();
      if (!name) {
        if (this.model.isNew()) {
          name = '<em>[new resource]</em>';
        }
        else {
          name = '[no name] ' + this.model.id;
        }
      }
    }
    if (name.length>45) {
      name = name.substring(0,45)+'...';
    }
    return name;
  },
  /*
   * Called when the user types to update the name in
   * my <li> to match the <input> values.
   */
  updateName: function() {
    // Need to structurally modify the DOM to force a re-render of text
    var $link = this.li.find('.js-resource-edit-name');
    $link.html('<span>'+this.name()+'</span>');
  },
  /*
   * Called when the user types to update the icon <img>
   * tags. Uses server API to select icon.
   */
  updateIcon: function() {
    var self = this;
    if (self.updateIconTimer) {
      clearTimeout(self.updateIconTimer);
    }
    self.updateIconTimer = setTimeout(function() {
        // AJAX to server API
        $.getJSON(CKAN.SITE_URL + '/api/2/util/resource/format_icon?format='+encodeURIComponent(self.formatBox.val()), function(data) {
          if (data && data.icon && data.format==self.formatBox.val()) {
            self.li.find('.js-resource-icon').attr('src',data.icon);
            self.table.find('.js-resource-icon').attr('src',data.icon);
          }
        });
        delete self.updateIconTimer;
      },
      100);
  },
  /*
   * Closes all other panels on the right and opens my editor panel.
   */
  openMyPanel: function(e) {
    if (e) { e.preventDefault(); }
    // Close all tables
    var panel = this.table.parents('.resource-panel');
    panel.find('.resource-details').hide();
    this.li.parents('fieldset#resources').find('.resource-list > li').removeClass('active');
    panel.show();
    this.table.show();
    this.table.find('.js-resource-edit-name').focus();
    this.li.addClass('active');
  },
  /*
   * Called when my delete button is clicked. Calls back to the parent
   * resource editor.
   */
  askToDelete: function(e) {
    e.preventDefault();
    var confirmMessage = CKAN.Strings.deleteThisResourceQuestion.replace('%name%', this.name());
    if (confirm(confirmMessage)) {
      this.options.callback_deleteMe();
    }
  },
  /*
   * Set up the dynamic-extras section of the table.
   */
  setupDynamicExtras: function() {
    var self = this;
    $.each(this.raw_resource, function(key,value) {
      // Skip the known keys
      if (self.reservedWord(key)) { return; }
      self.addDynamicExtra(key,value);
    });
    this.table.find('.add-resource-extra').click(function(e) {
      e.preventDefault();
      self.addDynamicExtra('','');
    });
  },
  addDynamicExtra: function(key,value) {
    // Create elements
    var dynamicExtra = $($.tmpl(CKAN.Templates.resourceExtra, {
      num: this.options.position,
      key: key,
      value: value}));
    this.table.find('.dynamic-extras').append(dynamicExtra);
    // Captured values
    var inputKey = dynamicExtra.find('.extra-key');
    var inputValue = dynamicExtra.find('.extra-value');
    // Callback function
    var self = this;
    var setExtraName = function() {
      var _key = inputKey.val();
      var key = $.trim(_key).replace(/\s+/g,'');
      // Don't allow you to create an extra called mimetype (etc)
      if (self.reservedWord(key)) { key=''; }
      // Set or unset the field's name
      if (key.length) {
        var newName = 'resources__'+self.options.position+'__'+key;
        inputValue.attr('name',newName);
        inputValue.removeClass('strikethrough');
      }
      else {
        inputValue.removeAttr('name');
        inputValue.addClass('strikethrough');
      }
    };
    // Callback function
    var clickRemove = function(e) {
      e.preventDefault();
      dynamicExtra.remove();
    };
    // Init with bindings
    CKAN.Utils.bindInputChanges(dynamicExtra.find('.extra-key'), setExtraName);
    dynamicExtra.find('.remove-resource-extra').click(clickRemove);
    setExtraName();
  },



  reservedWord: function(word) {
    return word=='cache_last_updated'   ||
          word=='cache_url'             ||
          word=='dataset'               ||
          word=='description'           ||
          word=='displaytitle'          ||
          word=='extras'                ||
          word=='format'                ||
          word=='hash'                  ||
          word=='id'                    ||
          word=='created'               ||
          word=='last_modified'         ||
          word=='mimetype'              ||
          word=='mimetype_inner'        ||
          word=='name'                  ||
          word=='package_id'            ||
          word=='position'              ||
          word=='resource_group_id'     ||
          word=='resource_type'         ||
          word=='revision_id'           ||
          word=='revision_timestamp'    ||
          word=='size'                  ||
          word=='size_extra'            ||
          word=='state'                 ||
          word=='url'                   ||
          word=='webstore_last_updated' ||
          word=='webstore_url';
  },
  /*
   * Called when my model is destroyed. Remove me from the page.
   */
  removeFromDom: function() {
    this.li.remove();
    this.table.remove();
  }
});


/* ============================================= */
/* Backbone View: Add Resource by uploading file */
/* ============================================= */
CKAN.View.ResourceAddUpload = Backbone.View.extend({
  tagName: 'div',

  initialize: function(options) {
    this.el = $(this.el);
    _.bindAll(this, 'render', 'updateFormData', 'setMessage', 'uploadFile');
    $(CKAN.Templates.resourceUpload).appendTo(this.el);
    this.$messages = this.el.find('.alert');
    this.setupFileUpload();
  },

  events: {
    'click input[type="submit"]': 'uploadFile'
  },

  setupFileUpload: function() {
    var self = this;
    this.el.find('.fileupload').fileupload({
      // needed because we are posting to remote url
      forceIframeTransport: true,
      replaceFileInput: false,
      autoUpload: false,
      fail: function(e, data) {
        alert('Upload failed');
      },
      add: function(e,data) {
        self.fileData = data;
        self.fileUploadData = data;
        self.key = self.makeUploadKey(data.files[0].name);
        self.updateFormData(self.key);
      },
      send: function(e, data) {
        self.setMessage(CKAN.Strings.uploadingFile +' <img src="http://assets.okfn.org/images/icons/ajaxload-circle.gif" class="spinner" />');
      },
      done: function(e, data) {
        self.onUploadComplete(self.key);
      }
    })
  },

  ISODateString: function(d) {
    function pad(n) {return n<10 ? '0'+n : n};
    return d.getUTCFullYear()+'-'
       + pad(d.getUTCMonth()+1)+'-'
       + pad(d.getUTCDate())+'T'
       + pad(d.getUTCHours())+':'
       + pad(d.getUTCMinutes())+':'
       + pad(d.getUTCSeconds());
  },

  // Create an upload key/label for this file.
  //
  // Form: {current-date}/file-name. Do not just use the file name as this
  // would lead to collisions.
  // (Could add userid/username and/or a small random string to reduce
  // collisions but chances seem very low already)
  makeUploadKey: function(fileName) {
    // google storage replaces ' ' with '+' which breaks things
    // See http://trac.ckan.org/ticket/1518 for more.
    var corrected = fileName.replace(/ /g, '-');
    // note that we put hh mm ss as hhmmss rather hh:mm:ss (former is 'basic
    // format')
    var now = new Date();
    // replace ':' with nothing
    var str = this.ISODateString(now).replace(':', '').replace(':', '');
    return str  + '/' + corrected;
  },

  updateFormData: function(key) {
    var self = this;
    self.setMessage(CKAN.Strings.checkingUploadPermissions + ' <img src="http://assets.okfn.org/images/icons/ajaxload-circle.gif" class="spinner" />');
    self.el.find('.fileinfo').text(key);
    $.ajax({
      url: CKAN.SITE_URL + '/api/storage/auth/form/' + key,
      async: false,
      success: function(data) {
        self.el.find('form').attr('action', data.action);
        _tmpl = '<input type="hidden" name="${name}" value="${value}" />';
        var $hidden = $(self.el.find('form div.hidden-inputs')[0]);
        $.each(data.fields, function(idx, item) {
          $hidden.append($.tmpl(_tmpl, item));
        });
        self.hideMessage();
      },
      error: function(jqXHR, textStatus, errorThrown) {
        // TODO: more graceful error handling (e.g. of 409)
        self.setMessage(CKAN.Strings.failedToGetCredentialsForUpload, 'error');
        self.el.find('input[name="add-resource-upload"]').hide();
      }
    });
  },

  uploadFile: function(e) {
    e.preventDefault();
    if (!this.fileData) {
      alert('No file selected');
      return;
    }
    var jqXHR = this.fileData.submit();
  },

  onUploadComplete: function(key) {
    var self = this;
    $.ajax({
      url: CKAN.SITE_URL + '/api/storage/metadata/' + self.key,
      success: function(data) {
        var name = data._label;
        if (name && name.length > 0 && name[0] === '/') {
          name = name.slice(1);
        }
        var d = new Date(data._last_modified);
        var lastmod = self.ISODateString(d);
        var newResource = new CKAN.Model.Resource({});
        newResource.set({
            url: data._location
            , name: name
            , size: data._content_length
            , last_modified: lastmod
            , format: data._format
            , mimetype: data._format
            , resource_type: 'file.upload'
            , owner: data['uploaded-by']
            , hash: data._checksum
            , cache_url: data._location
            , cache_url_updated: lastmod
          }
          , {
            error: function(model, error) {
              var msg = 'Filed uploaded OK but error adding resource: ' + error + '.';
              msg += 'You may need to create a resource directly. Uploaded file at: ' + data._location;
              self.setMessage(msg, 'error');
            }
          }
        );
        self.collection.add(newResource);
        self.setMessage('File uploaded OK and resource added', 'success');
      }
    });
  },

  setMessage: function(msg, category) {
    var category = category || 'alert-info';
    this.$messages.removeClass('alert-info alert-success alert-error');
    this.$messages.addClass(category);
    this.$messages.show();
    this.$messages.html(msg);
  },

  hideMessage: function() {
    this.$messages.hide('slow');
  }
});

/* ======================================== */
/* == Backbone View: Add resource by URL == */
/* ======================================== */
CKAN.View.ResourceAddUrl = Backbone.View.extend({
  initialize: function(options) {
    _.bindAll(this, 'clickSubmit');
  },

  clickSubmit: function(e) {
     e.preventDefault();

     var self = this;
     var newResource = new CKAN.Model.Resource({});

     this.el.find('input[name="add-resource-save"]').addClass("disabled");
     var urlVal = this.el.find('input[name="add-resource-url"]').val();
     var qaEnabled = $.inArray('qa',CKAN.plugins)>=0;

     if(qaEnabled && this.options.mode=='file') {
       $.ajax({
         url: CKAN.SITE_URL + '/qa/link_checker',
         context: newResource,
         data: {url: urlVal},
         dataType: 'json',
         error: function(){
           newResource.set({url: urlVal, resource_type: 'file'});
           self.collection.add(newResource);
           self.resetForm();
         },
         success: function(data){
           data = data[0];
           newResource.set({
             url: urlVal,
             resource_type: 'file',
             format: data.format,
             size: data.size,
             mimetype: data.mimetype,
             last_modified: data.last_modified,
             url_error: (data.url_errors || [""])[0]
           });
           self.collection.add(newResource);
           self.resetForm();
         }
       });
     }
     else {
       newResource.set({url: urlVal, resource_type: this.options.mode});
       this.collection.add(newResource);
       this.resetForm();
     }
  },

  resetForm: function() {
     this.el.find('input[name="add-resource-save"]').removeClass("disabled");
     this.el.find('input[name="add-resource-url"]').val('');
  },

  events: {
    'click .btn': 'clickSubmit'
  }
});



/* ================ */
/* == CKAN.Utils == */
/* ================ */
CKAN.Utils = function($, my) {
  // Animate the appearance of an element by expanding its height
  my.animateHeight = function(element, animTime) {
    if (!animTime) { animTime = 350; }
    element.show();
    var finalHeight = element.height();
    element.height(0);
    element.animate({height:finalHeight}, animTime);
  };

  my.bindInputChanges = function(input, callback) {
    input.keyup(callback);
    input.keydown(callback);
    input.keypress(callback);
    input.change(callback);
  };

  my.setupDatasetDeleteButton = function() {
    var select = $('select.dataset-delete');
    select.attr('disabled','disabled');
    select.css({opacity: 0.3});
    $('button.dataset-delete').click(function(e) {
      select.removeAttr('disabled');
      select.fadeTo('fast',1.0);
      $(e.target).css({opacity:0});
      $(e.target).attr('disabled','disabled');
      return false;
    });
  };

  // Attach dataset autocompletion to provided elements
  //
  // Requires: jquery-ui autocomplete
  my.setupPackageAutocomplete = function(elements) {
    elements.autocomplete({
      minLength: 0,
      source: function(request, callback) {
        var url = '/dataset/autocomplete?q=' + request.term;
        $.ajax({
          url: url,
          success: function(data) {
            // atm is a string with items broken by \n and item = title (name)|name
            var out = [];
            var items = data.split('\n');
            $.each(items, function(idx, value) {
              var _tmp = value.split('|');
              var _newItem = {
                label: _tmp[0],
                value: _tmp[1]
              };
              out.push(_newItem);
            });
            callback(out);
          }
        });
      }
      , select: function(event, ui) {
        var input_box = $(this);
        input_box.val('');
        var old_name = input_box.attr('name');
        var field_name_regex = /^(\S+)__(\d+)__(\S+)$/;
        var split = old_name.match(field_name_regex);

        var new_name = split[1] + '__' + (parseInt(split[2],10) + 1) + '__' + split[3];

        input_box.attr('name', new_name);
        input_box.attr('id', new_name);

        var $new = $('<div class="ckan-dataset-to-add"><p></p></div>');
        $new.append($('<input type="hidden" />').attr('name', old_name).val(ui.item.value));
        $new.append('<i class="icon-plus-sign"></i> ');
        $new.append(ui.item.label);
        input_box.after($new);

        // prevent setting value in autocomplete box
        return false;
      }
    });
  };

  // Attach tag autocompletion to provided elements
  //
  // Requires: jquery-ui autocomplete
  my.setupTagAutocomplete = function(elements) {
    // don't navigate away from the field on tab when selecting an item
    elements.bind( "keydown",
      function( event ) {
        if ( event.keyCode === $.ui.keyCode.TAB && $( this ).data( "autocomplete" ).menu.active ) {
          event.preventDefault();
        }
      }
    ).autocomplete({
        minLength: 1,
        source: function(request, callback) {
          // here request.term is whole list of tags so need to get last
          var _realTerm = $.trim(request.term.split(',').pop());
          var url = CKAN.SITE_URL + '/api/2/util/tag/autocomplete?incomplete=' + _realTerm;
          $.getJSON(url, function(data) {
            // data = { ResultSet: { Result: [ {Name: tag} ] } } (Why oh why?)
            var tags = $.map(data.ResultSet.Result, function(value, idx) {
              return value.Name;
            });
            callback( $.ui.autocomplete.filter(tags, _realTerm) );
          });
        },
        focus: function() {
          // prevent value inserted on focus
          return false;
        },
        select: function( event, ui ) {
          var terms = this.value.split(',');
          // remove the current input
          terms.pop();
          // add the selected item
          terms.push( " "+ui.item.value );
          // add placeholder to get the comma-and-space at the end
          terms.push( " " );
          this.value = terms.join( "," );
          return false;
        }
    });
  };

  // Attach tag autocompletion to provided elements
  //
  // Requires: jquery-ui autocomplete
  my.setupFormatAutocomplete = function(elements) {
    elements.autocomplete({
      minLength: 1,
      source: function(request, callback) {
        var url = CKAN.SITE_URL + '/api/2/util/resource/format_autocomplete?incomplete=' + request.term;
        $.getJSON(url, function(data) {
          // data = { ResultSet: { Result: [ {Name: tag} ] } } (Why oh why?)
          var formats = $.map(data.ResultSet.Result, function(value, idx) {
            return value.Format;
          });
          callback(formats);
        });
      }
    });
  };

  my.setupOrganizationUserAutocomplete = function(elements) {
    elements.autocomplete({
      minLength: 2,
      source: function(request, callback) {
        var url = '/api/2/util/user/autocomplete?q=' + request.term;
        $.getJSON(url, function(data) {
          $.each(data, function(idx, userobj) {
            var label = userobj.name;
            if (userobj.fullname) {
              label += ' [' + userobj.fullname + ']';
            }
            userobj.label = label;
            userobj.value = userobj.name;
          });
          callback(data);
        });
      },
      select: function(event, ui) {
        var input_box = $(this);
        input_box.val('');
        var parent_dd = input_box.parent('dd');
        var old_name = input_box.attr('name');
        var field_name_regex = /^(\S+)__(\d+)__(\S+)$/;
        var split = old_name.match(field_name_regex);

        var new_name = split[1] + '__' + (parseInt(split[2],10) + 1) + '__' + split[3];
        input_box.attr('name', new_name);
        input_box.attr('id', new_name);

        parent_dd.before(
          '<input type="hidden" name="' + old_name + '" value="' + ui.item.value + '">' +
          '<input type="hidden" name="' + old_name.replace('__name','__capacity') + '" value="editor">' +
          '<dd>' + ui.item.label + '</dd>'
        );

        return false; // to cancel the event ;)
      }
    });
  };


  // Attach user autocompletion to provided elements
  //
  // Requires: jquery-ui autocomplete
  my.setupUserAutocomplete = function(elements) {
    elements.autocomplete({
      minLength: 2,
      source: function(request, callback) {
        var url = CKAN.SITE_URL + '/api/2/util/user/autocomplete?q=' + request.term;
        $.getJSON(url, function(data) {
          $.each(data, function(idx, userobj) {
            var label = userobj.name;
            if (userobj.fullname) {
              label += ' [' + userobj.fullname + ']';
            }
            userobj.label = label;
            userobj.value = userobj.name;
          });
          callback(data);
        });
      }
    });
  };


  my.relatedSetup = function(form) {
    $('[rel=popover]').popover();

    function addAlert(msg) {
      $('<div class="alert alert-error" />').html(msg).hide().prependTo(form).fadeIn();
    }

    function relatedRequest(action, method, data) {
      return $.ajax({
        type: method,
        dataType: 'json',
        contentType: 'application/json',
        url: CKAN.SITE_URL + '/api/3/action/related_' + action,
        data: data ? JSON.stringify(data) : undefined,
        error: function(err, txt, w) {
          // This needs to be far more informative.
          addAlert('<strong>Error:</strong> Unable to ' + action + ' related item');
        }
      });
    }

    // Center thumbnails vertically.
    var relatedItems = $('.related-items');
    relatedItems.find('li').each(function () {
      var item = $(this), description = item.find('.description');

      function vertiallyAlign() {
        var img = $(this),
            height = img.height(),
            parent = img.parent().height(),
            top = (height - parent) / 2;

        if (parent < height) {
          img.css('margin-top', -top);
        }
      }

      item.find('img').load(vertiallyAlign);
      description.data('height', description.height()).truncate();
    });

    relatedItems.on('mouseenter mouseleave', '.description.truncated', function (event) {
      var isEnter = event.type === 'mouseenter'
          description = $(this)
          timer = description.data('hover-intent');

      function update() {
        var parent = description.parents('li:first'),
            difference = description.data('height') - description.height();

        description.truncate(isEnter ? 'expand' : 'collapse');
        parent.toggleClass('expanded-description', isEnter);

        // Adjust the bottom margin of the item relative to it's current value
        // to allow the description to expand without breaking the grid.
        parent.css('margin-bottom', isEnter ? '-=' + difference + 'px' : '');
        description.removeData('hover-intent');
      }

      if (!isEnter && timer) {
        // User has moused out in the time set so cancel the action.
        description.removeData('hover-intent');
        return clearTimeout(timer);
      } else if (!isEnter && !timer) {
        update();
      } else {
        // Delay the hover action slightly to wait to see if the user mouses
        // out again. This prevents unwanted actions.
        description.data('hover-intent', setTimeout(update, 200));
      }
    });

    // Add a handler for the delete buttons.
    relatedItems.on('click', '[data-action=delete]', function (event) {
      var id = $(this).data('relatedId');
      relatedRequest('delete', 'POST', {id: id}).done(function () {
        $('#related-item-' + id).remove();
      });
      event.preventDefault();
    });

    $(form).submit(function (event) {
      event.preventDefault();

      // Validate the form
      var form = $(this), data = {};
      jQuery.each(form.serializeArray(), function () {
        data[this.name] = this.value;
      });

      form.find('.alert').remove();
      form.find('.error').removeClass('error');
      if (!data.title) {
        addAlert('<strong>Missing field:</strong> A title is required');
        $('[name=title]').parent().addClass('error');
        return;
      }
      if (!data.url) {
        addAlert('<strong>Missing field:</strong> A url is required');
        $('[name=url]').parent().addClass('error');
        return;
      }

      relatedRequest('create', this.method, data).done(function () {
        // TODO: Insert item dynamically.
        window.location.reload();
      });
    });
  };

  // Attach authz group autocompletion to provided elements
  //
  // Requires: jquery-ui autocomplete
  my.setupAuthzGroupAutocomplete = function(elements) {
    elements.autocomplete({
      minLength: 2,
      source: function(request, callback) {
        var url = CKAN.SITE_URL + '/api/2/util/authorizationgroup/autocomplete?q=' + request.term;
        $.getJSON(url, function(data) {
          $.each(data, function(idx, userobj) {
            var label = userobj.name;
            userobj.label = label;
            userobj.value = userobj.name;
          });
          callback(data);
        });
      }
    });
  };

  my.setupGroupAutocomplete = function(elements) {
    elements.autocomplete({
      minLength: 2,
      source: function(request, callback) {
        var url = CKAN.SITE_URL + '/api/2/util/group/autocomplete?q=' + request.term;
        $.getJSON(url, function(data) {
          $.each(data, function(idx, userobj) {
            var label = userobj.name;
            userobj.label = label;
            userobj.value = userobj.name;
          });
          callback(data);
        });
      }
    });
  };

  my.setupMarkdownEditor = function(markdownEditor) {
    // Markdown editor hooks
    markdownEditor.find('button, div.markdown-preview').live('click', function(e) {
      e.preventDefault();
      var $target = $(e.target);
      // Extract neighbouring elements
      var markdownEditor=$target.closest('.markdown-editor');
      markdownEditor.find('button').removeClass('depressed');
      var textarea = markdownEditor.find('.markdown-input');
      var preview = markdownEditor.find('.markdown-preview');
      // Toggle the preview
      if ($target.is('.js-markdown-preview')) {
        $target.addClass('depressed');
        raw_markdown=textarea.val();
        preview.html("<em>"+CKAN.Strings.loading+"<em>");
        $.post(CKAN.SITE_URL + "/api/util/markdown", { q: raw_markdown },
          function(data) { preview.html(data); }
        );
        preview.width(textarea.width());
        preview.height(textarea.height());
        textarea.hide();
        preview.show();
      } else {
        markdownEditor.find('.js-markdown-edit').addClass('depressed');
        textarea.show();
        preview.hide();
        textarea.focus();
      }
      return false;
    });
  };

  // If notes field is more than 1 paragraph, just show the
  // first paragraph with a 'Read more' link that will expand
  // the div if clicked
  my.setupNotesExtract = function() {
    var notes = $('#content div.notes');
    var paragraphs = notes.find('#notes-extract > *');
    if (paragraphs.length===0) {
      notes.hide();
    }
    else if (paragraphs.length > 1) {
      var remainder = notes.find('#notes-remainder');
      $.each(paragraphs,function(i,para) {
        if (i > 0) { remainder.append($(para).remove()); }
      });
      var finalHeight = remainder.height();
      remainder.height(0);
      notes.find('#notes-toggle').show();
      notes.find('#notes-toggle button').click(
        function(event){
          notes.find('#notes-toggle button').toggle();
          if ($(event.target).hasClass('more')) {
            remainder.animate({'height':finalHeight});
          }
          else {
            remainder.animate({'height':0});
          }
          return false;
        }
      );
    }
  };

  my.warnOnFormChanges = function() {
    var boundToUnload = false;
    return function($form) {
      var flashWarning = function() {
        if (boundToUnload) { return; }
        boundToUnload = true;
        // Bind to the window departure event
        window.onbeforeunload = function () {
          return CKAN.Strings.youHaveUnsavedChanges;
        };
      };
      // Hook form modifications to flashWarning
      $form.find('input,select').live('change', function(e) {
        $target = $(e.target);
        // Entering text in the 'add' box does not represent a change
        if ($target.closest('.resource-add').length===0) {
          flashWarning();
        }
      });
      // Don't stop us leaving
      $form.submit(function() {
        window.onbeforeunload = null;
      });
      // Calling functions might hook to flashWarning
      return flashWarning;
    };
  }();

  my.countObject = function(obj) {
    var count=0;
    $.each(obj, function() {
      count++;
    });
    return count;
  };

  function followButtonClicked(event) {
    var button = event.currentTarget;
    if (button.id === 'user_follow_button') {
        var object_type = 'user';
    } else if (button.id === 'dataset_follow_button') {
        var object_type = 'dataset';
    }
    else {
        // This shouldn't happen.
        return;
    }
	var object_id = button.getAttribute('data-obj-id');
    if (button.getAttribute('data-state') === "follow") {
        if (object_type == 'user') {
            var url = '/api/action/follow_user';
        } else if (object_type == 'dataset') {
            var url = '/api/action/follow_dataset';
        } else {
            // This shouldn't happen.
            return;
        }
        var data = JSON.stringify({
          id: object_id
        });
        var nextState = 'unfollow';
        var nextString = CKAN.Strings.unfollow;
    } else if (button.getAttribute('data-state') === "unfollow") {
        if (object_type == 'user') {
            var url = '/api/action/unfollow_user';
        } else if (object_type == 'dataset') {
            var url = '/api/action/unfollow_dataset';
        } else {
            // This shouldn't happen.
            return;
        }
        var data = JSON.stringify({
          id: object_id
        });
        var nextState = 'follow';
        var nextString = CKAN.Strings.follow;
    }
    else {
        // This shouldn't happen.
        return;
    }
    $.ajax({
      contentType: 'application/json',
      url: url,
      data: data,
      dataType: 'json',
      processData: false,
      type: 'POST',
      success: function(data) {
        button.setAttribute('data-state', nextState);
        button.innerHTML = nextString;
      }
    });
  };

  // This only needs to happen on dataset pages, but it doesn't seem to do
  // any harm to call it anyway.
  $('#user_follow_button').on('click', followButtonClicked);
  $('#dataset_follow_button').on('click', followButtonClicked);

  return my;
}(jQuery, CKAN.Utils || {});



/* ==================== */
/* == Data Previewer == */
/* ==================== */
CKAN.DataPreview = function ($, my) {
  my.jsonpdataproxyUrl = 'http://jsonpdataproxy.appspot.com/';
  my.dialogId = 'ckanext-datapreview';
  my.$dialog = $('#' + my.dialogId);

  // **Public: Loads a data previewer for an embedded page**
  //
  // Uses the provided reclineState to restore the Dataset.  Creates a single
  // view for the Dataset (the one defined by reclineState.currentView).  And
  // then passes the constructed Dataset, the constructed View, and the
  // reclineState into the DataExplorer constructor.
  my.loadEmbeddedPreview = function(resourceData, reclineState) {
    my.$dialog.html('<h4>Loading ... <img src="http://assets.okfn.org/images/icons/ajaxload-circle.gif" class="loading-spinner" /></h4>');

    // Restore the Dataset from the given reclineState.
    var datasetInfo = _.extend({
        url: reclineState.url,
        backend: reclineState.backend
      },
      reclineState.dataset
    );
    var dataset = new recline.Model.Dataset(datasetInfo);

    // Only create the view defined in reclineState.currentView.
    // TODO: tidy this up.
    var views = null;
    if (reclineState.currentView === 'grid') {
      views = [ {
        id: 'grid',
        label: 'Grid',
        view: new recline.View.SlickGrid({
          model: dataset,
          state: reclineState['view-grid']
        })
      }];
    } else if (reclineState.currentView === 'graph') {
      views = [ {
        id: 'graph',
        label: 'Graph',
        view: new recline.View.Graph({
          model: dataset,
          state: reclineState['view-graph']
        })
      }];
    } else if (reclineState.currentView === 'map') {
      views = [ {
        id: 'map',
        label: 'Map',
        view: new recline.View.Map({
          model: dataset,
          state: reclineState['view-map']
        })
      }];
    }

    // Finally, construct the DataExplorer.  Again, passing in the reclineState.
    var dataExplorer = new recline.View.MultiView({
      el: my.$dialog,
      model: dataset,
      state: reclineState,
      views: views
    });

  };

  // **Public: Creates a link to the embeddable page.
  //
  // For a given DataExplorer state, this function constructs and returns the
  // url to the embeddable view of the current dataexplorer state.
  my.makeEmbedLink = function(explorerState) {
    var state = explorerState.toJSON();
    state.state_version = 1;

    var queryString = '?';
    var items = [];
    $.each(state, function(key, value) {
      if (typeof(value) === 'object') {
        value = JSON.stringify(value);
      }
      items.push(key + '=' + escape(value));
    });
    queryString += items.join('&');
    return embedPath + queryString;
  };

  // **Public: Loads a data preview**
  //
  // Fetches the preview data object from the link provided and loads the
  // parsed data from the webstore displaying it in the most appropriate
  // manner.
  //
  // link - Preview button.
  //
  // Returns nothing.
  my.loadPreviewDialog = function(resourceData) {
    my.$dialog.html('<h4>Loading ... <img src="http://assets.okfn.org/images/icons/ajaxload-circle.gif" class="loading-spinner" /></h4>');

    function showError(msg){
      msg = msg || CKAN.Strings.errorLoadingPreview;
      return $('#ckanext-datapreview')
        .append('<div></div>')
        .addClass('alert alert-error fade in')
        .html(msg);
    }

    function initializeDataExplorer(dataset) {
      var views = [
        {
          id: 'grid',
          label: 'Grid',
          view: new recline.View.SlickGrid({
            model: dataset
          })
        },
        {
          id: 'graph',
          label: 'Graph',
          view: new recline.View.Graph({
            model: dataset
          })
        },
        {
          id: 'map',
          label: 'Map',
          view: new recline.View.Map({
            model: dataset
          })
        }
      ];

      var dataExplorer = new recline.View.MultiView({
        el: my.$dialog,
        model: dataset,
        views: views,
        config: {
          readOnly: true
        }
      });

      // Hide the fields control by default
      // (This should be done in recline!)
      $('.menu-right a[data-action="fields"]').click();

      // -----------------------------
      // Setup the Embed modal dialog.
      // -----------------------------

      // embedLink holds the url to the embeddable view of the current DataExplorer state.
      var embedLink = $('.embedLink');

      // embedIframeText contains the '<iframe>' construction, which sources
      // the above link.
      var embedIframeText = $('.embedIframeText');

      // iframeWidth and iframeHeight control the width and height parameters
      // used to construct the iframe, and are also used in the link.
      var iframeWidth = $('.iframe-width');
      var iframeHeight = $('.iframe-height');

      // Update the embedLink and embedIframeText to contain the updated link
      // and update width and height parameters.
      function updateLink() {
        var link = my.makeEmbedLink(dataExplorer.state);
        var width = iframeWidth.val();
        var height = iframeHeight.val();
        link += '&width='+width+'&height='+height;

        // Escape '"' characters in {{link}} in order not to prematurely close
        // the src attribute value.
        embedIframeText.val($.mustache('<iframe frameBorder="0" width="{{width}}" height="{{height}}" src="{{link}}"></iframe>',
                                       {
                                         link: link.replace(/"/g, '&quot;'),
                                         width: width,
                                         height: height
                                       }));
        embedLink.attr('href', link);
      }

      // Bind changes to the DataExplorer, or the two width and height inputs
      // to re-calculate the url.
      dataExplorer.state.bind('change', updateLink);
      for (var i=0; i<dataExplorer.pageViews.length; i++) {
        dataExplorer.pageViews[i].view.state.bind('change', updateLink);
      }

      iframeWidth.change(updateLink);
      iframeHeight.change(updateLink);

      // Initial population of embedLink and embedIframeText
      updateLink();

      // Finally, since we have a DataExplorer, we can show the embed button.
      $('.preview-header .btn').show();

    }

    // 4 situations
    // a) something was posted to the datastore - need to check for this
    // b) csv or xls (but not datastore)
    // c) can be treated as plain text
    // d) none of the above but worth iframing (assumption is
    // that if we got here (i.e. preview shown) worth doing
    // something ...)
    resourceData.formatNormalized = my.normalizeFormat(resourceData.format);
    resourceData.url  = my.normalizeUrl(resourceData.url);
    if (resourceData.formatNormalized === '') {
      var tmp = resourceData.url.split('/');
      tmp = tmp[tmp.length - 1];
      tmp = tmp.split('?'); // query strings
      tmp = tmp[0];
      var ext = tmp.split('.');
      if (ext.length > 1) {
        resourceData.formatNormalized = ext[ext.length-1];
      }
    }

    // Set recline CKAN backend API endpoint to right location (so it can locate
    // CKAN DataStore)
    recline.Backend.Ckan.API_ENDPOINT = CKAN.SITE_URL + '/api';

    if (resourceData.datastore_active) {
      resourceData.backend =  'ckan';
      var dataset = new recline.Model.Dataset(resourceData);
      var errorMsg = CKAN.Strings.errorLoadingPreview + ': ' + CKAN.Strings.errorDataStore;
      dataset.fetch()
        .done(function(dataset){
            initializeDataExplorer(dataset);
        })
        .fail(function(error){
          if (error.message) errorMsg += ' (' + error.message + ')';
          showError(errorMsg);
        });

    }
    else if (resourceData.formatNormalized in {'csv': '', 'xls': '', 'tsv':''}) {
      // set format as this is used by Recline in setting format for DataProxy
      resourceData.format = resourceData.formatNormalized;
      resourceData.backend = 'dataproxy';
      var dataset = new recline.Model.Dataset(resourceData);
      var errorMsg = CKAN.Strings.errorLoadingPreview + ': ' +CKAN.Strings.errorDataProxy;
      dataset.fetch()
        .done(function(dataset){

          dataset.bind('query:fail', function(error) {
            $('#ckanext-datapreview .data-view-container').hide();
            $('#ckanext-datapreview .header').hide();
            $('.preview-header .btn').hide();
          });

          initializeDataExplorer(dataset);
          $('.recline-query-editor .text-query').hide();
        })
        .fail(function(error){
          if (error.message) errorMsg += ' (' + error.message + ')';
          showError(errorMsg);
        });
    }
    else if (resourceData.formatNormalized in {
        'rdf+xml': '',
        'owl+xml': '',
        'xml': '',
        'n3': '',
        'n-triples': '',
        'turtle': '',
        'plain': '',
        'atom': '',
        'tsv': '',
        'rss': '',
        'txt': ''
        }) {
      // HACK: treat as plain text / csv
      // pass url to jsonpdataproxy so we can load remote data (and tell dataproxy to treat as csv!)
      var _url = my.jsonpdataproxyUrl + '?type=csv&url=' + resourceData.url;
      my.getResourceDataDirect(_url, function(data) {
        my.showPlainTextData(data);
      });
    }
    else if (resourceData.formatNormalized in {'html':'', 'htm':''}
        ||  resourceData.url.substring(0,23)=='http://docs.google.com/') {
      // we displays a fullscreen dialog with the url in an iframe.
      my.$dialog.empty();
      var el = $('<iframe></iframe>');
      el.attr('src', resourceData.url);
      el.attr('width', '100%');
      el.attr('height', '100%');
      my.$dialog.append(el);
    }
    // images
    else if (resourceData.formatNormalized in {'png':'', 'jpg':'', 'gif':''}
        ||  resourceData.resource_type=='image') {
      // we displays a fullscreen dialog with the url in an iframe.
      my.$dialog.empty();
      var el = $('<img />');
      el.attr('src', resourceData.url);
      el.css('max-width', '100%');
      el.css('border', 'solid 4px black');
      my.$dialog.append(el);
    }
    else {
      // Cannot reliably preview this item - with no mimetype/format information,
      // can't guarantee it's not a remote binary file such as an executable.
      my.showError({
        title: CKAN.Strings.previewNotAvailableForDataType + resourceData.formatNormalized,
        message: ''
      });
    }
  };

  // Public: Requests the formatted resource data from the webstore and
  // passes the data into the callback provided.
  //
  // preview - A preview object containing resource metadata.
  // callback - A Function to call with the data when loaded.
  //
  // Returns nothing.
  my.getResourceDataDirect = function(url, callback) {
    // $.ajax() does not call the "error" callback for JSONP requests so we
    // set a timeout to provide the callback with an error after x seconds.
    var timeout = 5000;
    var timer = setTimeout(function error() {
      callback({
        error: {
          title: 'Request Error',
          message: 'Dataproxy server did not respond after ' + (timeout / 1000) + ' seconds'
        }
      });
    }, timeout);

    // have to set jsonp because webstore requires _callback but that breaks jsonpdataproxy
    var jsonp = '_callback';
    if (url.indexOf('jsonpdataproxy') != -1) {
      jsonp = 'callback';
    }

    // We need to provide the `cache: true` parameter to prevent jQuery appending
    // a cache busting `={timestamp}` parameter to the query as the webstore
    // currently cannot handle custom parameters.
    $.ajax({
      url: url,
      cache: true,
      dataType: 'jsonp',
      jsonp: jsonp,
      success: function(data) {
        clearTimeout(timer);
        callback(data);
      }
    });
  };

  // Public: Displays a String of data in a fullscreen dialog.
  //
  // data    - An object of parsed CSV data returned by the webstore.
  //
  // Returns nothing.
  my.showPlainTextData = function(data) {
    if(data.error) {
      my.showError(data.error);
    } else {
      var content = $('<pre></pre>');
      for (var i=0; i<data.data.length; i++) {
        var row = data.data[i].join(',') + '\n';
        content.append(my.escapeHTML(row));
      }
      my.$dialog.html(content);
    }
  };

  my.showError = function (error) {
    var _html = _.template(
        '<div class="alert alert-error"><strong><%= title %></strong><br /><%= message %></div>',
        error
    );
    my.$dialog.html(_html);
  };

  my.normalizeFormat = function(format) {
    var out = format.toLowerCase();
    out = out.split('/');
    out = out[out.length-1];
    return out;
  };

  my.normalizeUrl = function(url) {
    if (url.indexOf('https') === 0) {
      return 'http' + url.slice(5);
    } else {
      return url;
    }
  }

  // Public: Escapes HTML entities to prevent broken layout and XSS attacks
  // when inserting user generated or external content.
  //
  // string - A String of HTML.
  //
  // Returns a String with HTML special characters converted to entities.
  my.escapeHTML = function (string) {
    return string.replace(/&(?!\w+;|#\d+;|#x[\da-f]+;)/gi, '&amp;')
                 .replace(/</g, '&lt;').replace(/>/g, '&gt;')
                 .replace(/"/g, '&quot;')
                 .replace(/'/g, '&#x27')
                 .replace(/\//g,'&#x2F;');
  };


  // Export the CKANEXT object onto the window.
  $.extend(true, window, {CKANEXT: {}});
  CKANEXT.DATAPREVIEW = my;
  return my;
}(jQuery, CKAN.DataPreview || {});
Back to Top