aboutsummaryrefslogtreecommitdiff
path: root/extensions/Dashboard/web/js/dashboard.js
blob: 825dd98c6c6f0ec6a880f47e79a898ef91069b71 (plain)
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
/**
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * The Initial Developer of the Original Code is "Nokia Corporation"
 * Portions created by the Initial Developer are Copyright (C) 2011 the
 * Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *      David Wilson <ext-david.3.wilson@nokia.com>
 *      Jari Savolainen <ext-jari.a.savolainen@nokia.com>
 *      Pami Ketolainen <pami.ketolainen@jollamobile.com>
 *
 * @requires jQuery($), jQuery UI & sortable/draggable UI modules
 */


/**
 * Return a self-referring URL with the given string appended as anchor text.
 */
function makeSelfUrl(obj)
{
    var url = window.location.toString().split('#')[0];
    return url + '#?' + $.param(obj, true);
}

/**
 * Left-pad a string with a character until it is a certain length.
 * @param s
 *      The string.
 * @param c
 *      The character, defaults to '0'.
 * @param n
 *      The length, defaults to 2.
 */
function lpad(s, c, n)
{
    s = '' + s;
    c = c || '0';
    n = n || 2;

    while(s.length < n) {
        s = c + s;
    }

    return s;
}


/**
 * Format a timestamp to string YYYY-MM-DD HH:MM:SS in local time.
 * @param ts
 *      Integer seconds since UNIX epoch.
 */
function formatTime(ts)
{
    var dt = new Date(ts * 1000);
    var dat = [1900 + dt.getYear(),
               lpad(dt.getMonth()),
               lpad(dt.getDay())].join('-');
    var tim = [lpad(dt.getHours()),
               lpad(dt.getMinutes()),
               lpad(dt.getSeconds())].join(':');
    return dat + ' ' + tim;
}


/**
 * Compare two widgets (state hash) by position.
 *
 * Used to sort the widget array in order which can be easily pushed on overlay
 */
function widgetPosCmp(a, b)
{
    if (a.col == b.col) {
        return a.pos - b.pos;
    } else {
        return a.col - b.col;
    }
}


/**
 * Clone a template.
 *
 * @param sel
 *      jQuery selector referencing template DOM element.
 * @returns
 *      Cloned DOM element with id attribute removed.
 */
function cloneTemplate(sel)
{
    var cloned = $(sel).clone();
    cloned.removeAttr('id');
    return cloned;
}


/**
 * Return integer seconds since UNIX epoch GMT.
 */
function now()
{
    return Math.floor((new Date).getTime());
}








/**
 * Base widget. To keep things simple, currently combines state and visual
 * rendering. Manages constructing a widget's DOM, cloning any templates,
 * refresh timer, and rendering the widget's settings.
 *
 * The default render() prepopulates the widget's content element with a
 * "#widget-template-<type>" and adds "#widget-settings-template-<type>" to the
 * settings dialog. Both templates are expected to be defined in
 * dashboard.html.tmpl.
 *
 * Any field value in the settings dialog template with class 'custom-field'
 * and name attribute gets automatically transfered to and from
 * widget.state.data. If something else is require, subclass shoud override
 * _setCustomSetting() and _getCustomSettings() methods.
 *
 * In the simplest scenario the subclass only needs to implement reload() method
 * to display the desired content in the widget content element.
 *
 * When templates are rendered, the click event of any <button> element with
 * attribute 'name' is bound to onClick<Name>() method, where <Name> is the
 * value of name attribute with first letter capitalized.
 *
 *
 */
var Widget = Base.extend({
    /**
     * Create an instance.
     *
     * @param dashboard
     *      Dashboard object.
     * @param state
     *      Initial widget state.
     */
    constructor: function(dashboard, state)
    {
        // Shorthand.
        this._dashboard = dashboard;

        // Fired when widget is removed
        this.onRemoveCb = new jQuery.Callbacks();
        // Fired when widget is maximized
        this.onMaximizeCb = new jQuery.Callbacks();
        // Fired when the widget state changes
        this.stateChangeCb = new jQuery.Callbacks();

        this.id = Widget.counter++;

        if(! this.TYPE) {
            this.TYPE = this.constructor.TYPE;
        }
        if(! this.TEMPLATE_TYPE) {
            this.TEMPLATE_TYPE = this.TYPE;
        }

        this.state = $.extend({
                color: 'none',
                minimized: false,
                height: 100,
                refresh: 0,
                data: {}
            }, state);
        this.render();
        this._applyState();
    },

    /**
     * Updates the widget.state and fires stateChangeCb if changes were
     * introduced.
     *
     * @param changes - New state values
     */
    updateState: function(changes)
    {
        var stateChanges = $.extend({}, changes);
        var dataChanges = stateChanges.data;

        // Remove unknown and unchanged values
        for (var key in stateChanges) {
            if (Widget.STATE_KEYS.indexOf(key) == -1 ||
                    this.state[key] == stateChanges[key]) {
                delete stateChanges[key];
            }
        }
        var changed = false;
        if (!$.isEmptyObject(stateChanges)) {
            $.extend(this.state, stateChanges);
            changed = true;
        }

        // Update widget type specific data
        changed = this._updateStateData(dataChanges) || changed;

        // Apply changes and fire the event
        if (changed) {
            this.stateChangeCb.fire(this);
            this._applyState();
        }
        return changed;
    },

    /**
     * Updates the widget type specific state.data
     *
     * @param changes - Object containin values to update in state.data
     * @returns True if something has changed in state.data
     *
     * Default implementation only compares state.data[key] == changes[key]
     * so this needs to be overriden in the subclass if widget stores more
     * complex values in the data object.
     */
    _updateStateData: function(changes)
    {
        var changes = $.extend({}, changes);
        for (var key in changes) {
            if (this.state.data[key] == changes[key]) {
                delete changes[key];
            }
        }
        if ($.isEmptyObject(changes)) {
            return false;
        } else {
            $.extend(this.state.data, changes);
            return true
        }
    },

    /**
     * Renders the widget ui from templates.
     */
    render: function()
    {
        // The top level container
        this.element = cloneTemplate('#widget-template');
        this.element.attr("id", "widget_" + this.id);
        this.element.addClass(this.TYPE + '-widget');
        // The header element, containing title and buttons
        this.headerElement = this._child(".widget-header");
        // The resizable container
        this.containerElement = this._child(".widget-container");
        // The actual widget content container
        this.contentElement = this._child(".widget-content");
        // Notification container
        this.statusElement = this._child(".widget-status");

        this._child(".widget-buttons [name='maximize']").button(
                {text:false, icons:{primary:"ui-icon-circle-plus"}});
        this._child(".widget-buttons [name='minimize']").button(
                {text:false, icons:{primary:"ui-icon-circle-minus"}});
        this._child(".widget-buttons [name='remove']").button(
                {text:false, icons:{primary:"ui-icon-circle-close"}});
        this._child(".widget-buttons [name='edit']").button(
                {text:false, icons:{primary:"ui-icon-wrench"}});
        this._child(".widget-buttons [name='refresh']").button(
                {text:false, icons:{primary:"ui-icon-refresh"}});

        // Populate content element with widget's template, if one exists.
        var sel = '#widget-template-' + this.TEMPLATE_TYPE;
        this.contentElement.append(cloneTemplate(sel));


        // Prepare settings dialog
        this.settingsDialog = this._child('.settings-dialog');
        this.settingsDialog.attr("id", "widget_settings_" + this.id);
        var colorSelect = $("[name='color']", this.settingsDialog);
        for(var bg in Widget.COLORS) {
            var fg = Widget.COLORS[bg];
            var item = $('<option />');
            item.html(bg);
            item.attr("value", bg);
            item.css({"background-color": bg, "color": fg});
            colorSelect.append(item);
        }

        // Append custom widget settings
        var sel = '#widget-settings-template-' + this.TEMPLATE_TYPE;
        var template = cloneTemplate(sel);
        this.settingsDialog.find("form").append(template);


        // Bind any buttons to automatic callbacks
        var widget = this;
        this._child(":button").each(function() {
            var name = $(this).attr("name");
            if (!name) return;
            var method = "onClick" + name[0].toUpperCase() + name.slice(1).toLowerCase();
            // this is the button element in this context
            $(this).click($.proxy(widget, method));
        });

        // Initialize the settings dialog
        this.settingsDialog.dialog({
            autoOpen: false,
            modal: true,
            width: 500,
            zIndex: 9999,
            buttons: {
                "Apply": $.proxy(this, "_onSettingsApply"),
                "Cancel": function(){ $(this).dialog("close") }
            }
        });

        // Make widget resizable
        this.containerElement.resizable({
            handles:"s",
            grid: [1, 16],
            helper: "widget-resize-helper",
            minHeight: Widget.MIN_HEIGHT,
            start: $.proxy(this, "_onResizeStart"),
            stop: $.proxy(this, "_onResizeStop")
        });
    },

    _onResizeStart: function()
    {
        // Iframes can eat mouse events, so we need to hide them
        $(".widget-content iframe").hide();
    },

    /**
     * Handle the resize event
     */
    _onResizeStop: function()
    {
        $(".widget-content iframe").show();
        // jquery ui resizable forces all dimensions, but we want width from
        // the parent overaly column.
        this.containerElement.css("width", "");
        this.updateState({height: this.containerElement.height()});
    },

    /**
     * Widget title bar maximize button click
     */
    onClickMaximize: function()
    {
        this.element.toggleClass("widget-max");
        this.headerElement.toggleClass("widget-header-maximized");
        this._child("button[name='remove']").toggle();
        this._child("button[name='minimize']").toggle();
        this._child("button[name='maximize'] .ui-button-icon-primary").toggleClass(
                "ui-icon-circle-plus ui-icon-arrowthick-1-sw");

        if (this.element.hasClass("widget-max")) {
            this.containerElement.css("height", "100%");
            if (this.state.minimized) {
                this.headerElement.removeClass("ui-corner-bottom");
                this.containerElement.slideDown("fast");
            }
            this.onMaximizeCb.fire(true);
        } else {
            this.containerElement.css("height", this.state.height);
            if (this.state.minimized) {
                this.headerElement.addClass("ui-corner-bottom");
                this.containerElement.slideUp("fast");
            }
            this.onMaximizeCb.fire(false);
        }

    },

    /**
     * Widget title bar minimize button click
     */
    onClickMinimize: function()
    {
        if (this.state.minimized) {
            this.headerElement.removeClass("ui-corner-bottom");
            this.containerElement.slideDown("fast");
            this.updateState({minimized: false});
        } else {
            this.headerElement.addClass("ui-corner-bottom");
            this.containerElement.slideUp("fast");
            this.updateState({minimized: true});
        }
    },

    /**
     * Widget title bar remove button click
     */
    onClickRemove: function()
    {
        if (confirm("Do you really want to remove this widget?")) {
            this.destroy();
            this.onRemoveCb.fire(this);
        }
    },

    /**
     * Widget title bar refresh button click
     */
    onClickRefresh: function()
    {
        this.reload();
    },

    /**
     * Widget title bar edit button click
     */
    onClickEdit: function()
    {

        this._setSettings();
        this.settingsDialog.dialog("open");
    },

    /**
     * Handler for settings dialog apply button click.
     */
    _onSettingsApply: function()
    {
        var state = this._getSettings();
        this.settingsDialog.dialog("close");
        if (this.updateState(state)) {
            this.reload();
        }
    },

    /**
     * Sets values in the widget settings dialog.
     */
    _setSettings: function()
    {
        var self = this;
        this.settingsDialog.find(".settings-field").each(function() {
            var key = $(this).attr("name");
            if(!key) return;
            $(this).val(self.state[key]);
        });
        this._setCustomSetting();
    },

    /**
     * Sets the widget specific setting values in the settigns dialog.
     *
     * Default implementation sets value to each form element with class
     * 'custom-field' in the settings dialog from this.state.data[<name>],
     * where <name> is the form element name attribute. Override in subclass,
     * if special processing is required.
     */
    _setCustomSetting: function()
    {
        var self = this;
        this.settingsDialog.find(".custom-field").each(function() {
            var key = $(this).attr("name");
            if(!key) return;
            $(this).val(self.state.data[key]);
        });
    },

    /**
     * Gets settings values from widget settings dialog.
     */
    _getSettings: function()
    {
        var state = {};
        this.settingsDialog.find(".settings-field").each(function() {
            var key = $(this).attr("name");
            if(!key) return;
            state[key] = $(this).val();
        });
        state.data = this._getCustomSettings();
        return state;
    },

    /**
     * Gets the widget type specific settings from the settings dialog
     *
     * Default implementation gets value (.val()) from each element with
     * class 'custom-field'. Override in subclass if special processing is
     * required.
     *
     * @returns Object presenting the state.data
     */
    _getCustomSettings: function()
    {
        var data = {};
        this.settingsDialog.find(".custom-field").each(function() {
            var key = $(this).attr("name");
            if(!key) return;
            data[key] = $(this).val();
        });
        return data;
    },

    /**
     * Aplies the state to widget UI.
     */
    _applyState: function()
    {
        window.clearInterval(this._refreshInterval);
        if(+this.state.refresh){
            this._refreshInterval = window.setInterval($.proxy(this, "reload"),
                    1000 * this.state.refresh)
        }

        var color = this.state.color == "none" ? "" : this.state.color;
        this.headerElement.css({
                "background": color,
                "color": Widget.COLORS[color]
        });
        this._child(".widget-header, .widget-container")
            .css("border-color", color);

        this._child(".widget-title").html(this.state.name);

        this.containerElement.css("height", this.state.height);

        if (this.state.minimized) {
            this.headerElement.addClass("ui-corner-bottom");
            this.containerElement.hide();
        }

        this._applyCustomState();
    },

    /**
     * Execute any actions required to apply widget specific settings
     */
    _applyCustomState: function()
    {
        // Implement in the subclass, if some actions are needed when state
        // changes.
    },

    /**
     * Reloads the widget content.
     */
    reload: function()
    {
        this.statusElement.empty();
    },

    /**
     * Removes the widget
     */

    destroy: function() {
        this.element.remove();
    },

    /**
     * Displays error text in widget status box
     */
    error: function(text)
    {
        if (text != undefined) {
            var clone = cloneTemplate('#widget-template-error');
            $('.error-text', clone).text(text);
            this.statusElement.html(clone);
        } else {
            this.statusElement.empty();
        }
    },
    /**
     * Display loader in widget status box
     */
    loader: function(on)
    {
        if (on) {
            var clone = cloneTemplate("#dash-template-loader");
            this.statusElement.html(clone);
        } else {
            this.statusElement.empty();
        }
    },

    /**
     * Return any matching child elements.
     */
    _child: function(sel)
    {
        return $(sel, this.element);
    }

}, /* class variables: */ {

    /** Mapping of type name of constructor. */
    _classes: {},

    /** Counter for widget instances to provide unique ID */
    counter: 0,

    /** Allowed keys in Widget.state */
    STATE_KEYS: ["id", "name", "overlay_id", "type", "color", "col", "pos",
        "height", "minimized", "refresh"],

    /** Minimum height for any widget. */
    MIN_HEIGHT: 100,

    /** Available colors, background -> foreground */
    COLORS: {
        'none':'',
        'gray':'white',
        'yellow': 'black',
        'red': 'black',
        'blue': 'white',
        'white': 'black',
        'orange': 'black',
        'green': 'black'},

    /**
     * Register a Widget subclass for use with Widget.createInstance().
     *
     * @param type
     *      String type name, e.g. "rss".
     * @param klass
     *      Constructor, i.e. the result of Widget.extend().
     */
    addClass: function(type, klass) {
        klass.TYPE = type;
        this._classes[type] = klass;
    },

    /**
     * Create a Widget given a state object.
     *
     * @param dashboard
     *      Dashboard the widget is associated with.
     * @param state
     *      Widget state object, including at least "type", which is the type
     *      of the widget to create.
     */
    createInstance: function(dashboard, state) {
        var klass = this._classes[state.type];
        return new klass(dashboard, state);
    }
});



/**
 * Overlay class which handles the dashboard columnt layout.
 *
 * Provides two callbacks
 *
 * columnChangeCb - fired when columns change
 * widgetsMovedCb - fired when widget order has been changed
 *
 */
var Overlay = Base.extend({
    constructor: function(dashboard, columns)
    {
        this.columnChangeCb = new jQuery.Callbacks();
        this.widgetsMovedCb = new jQuery.Callbacks();
        this.dashboard = dashboard;
        this.element = $("table#overlay");
        this.setColumns(columns);
    },

    /**
     * Resets the overlay container table to original condition
     */
    destroy: function()
    {
        this.element.colResizable({disable:true});
        this._disableSortable();
        this._child("#overlay-top").empty();
        this._child("#overlay-columns").empty();
        this._child("#overlay-header").empty();
    },

    setColumns: function(columns)
    {
        // Reset
        this.destroy();

        // Re initialize
        this.columns = $.extend([], Overlay.DEFAULT_COLUMNS, columns);

        for (var i = 0; i < this.columns.length; i++) {
            this._createColumn(this.columns[i]);
        }
        this._enableSortable();
        this._resetResizable();
    },

    /**
     * Adds new column at the end
     */
    addColumn: function()
    {
        if (this.columns.length >= Overlay.MAX_COLUMNS) {
            alert("Maximum of " + Overlay.MAX_COLUMNS + " columns reached");
            return;
        }
        var width = Math.floor(100 / (this.columns.length + 1));
        var shrink = Math.floor(width / this.columns.length);
        var total = 0;
        this._child("#overlay-header th").each(function(){
            total += $(this).width();
        });
        this._child("#overlay-header th").each(function(){
            $(this).css("width", "-=" + Math.ceil(total * (shrink / 100)));
        });
        this._createColumn(width);
        this._updateColumnState();
        this._resetResizable();
        this._resetSortable();
    },

    /**
     * Removes the last column and moves the widgets to previous column
     */
    removeColumn:function()
    {
        if (this.columns.length <= 1) {
            alert("Cant remove the last column");
            return;
        }
        var toBeRemoved = this._child("#overlay-columns > td").last();
        var lastColumn = toBeRemoved.prev(".overlay-column");
        if (toBeRemoved.children().size()) {
            lastColumn.append(toBeRemoved.children());
            this.widgetsMovedCb.fire(this.columns.length - 1,
                    lastColumn.sortable("toArray"));
        }
        toBeRemoved.sortable("destroy").remove();
        this._child("#overlay-header > th").last().remove();
        var count = this._child("#overlay-columns > td").size();
        this._child("#overlay-top").attr("colspan", count);
        this._updateColumnState();
        this._resetResizable();
        this._resetSortable();
    },

    /**
     * Reset column widths to be even.
     */
    resetColumns: function()
    {
        var width = Math.floor(100 / this.columns.length);
        this._child("#overlay-header > th").css("width", width + "%");
        this._updateColumnState();
        this._resetResizable();
    },

    /**
     * Adds new widget in the overlay.
     * Inspects the widget.state.col/pos attributes and places it accordingly,
     * or as the first columns last, if col/pos does not match the overlay.
     */
    insertWidget: function(widget)
    {
        var col = widget.state.col;
        var colElement = this._child(".overlay-column").eq(col);
        if (!colElement.size()) {
            colElement = this._child("#overlay-columns > td").first();
            col = 1;
        }
        var posElement = colElement.find(".widget").eq(widget.state.pos);
        if (posElement.size()) {
            posElement.before(widget.element);
        } else {
            colElement.append(widget.element);
        }
        this.widgetsMovedCb.fire(col, colElement.sortable("toArray"));
        widget.reload();
        widget.onMaximizeCb.add($.proxy(this, "_onWidgetMaximize"));
        this._resetSortable();
    },

    /**
     * Disables and enables column resizing
     */
    _resetResizable: function()
    {
        this.element.colResizable({disable:true});
        this.element.colResizable({
            minWidth: Overlay.MIN_WIDTH,
            onResize: $.proxy(this, "_updateColumnState")
        });
    },

    /**
     * Disables and enables drag and drop sorting
     */
    _resetSortable: function()
    {
        this._disableSortable();
        this._enableSortable();
    },

    /**
     * Enables drag and drop sorting
     */
    _enableSortable: function()
    {
        this._child("td.overlay-column").sortable({
            connectWith: "td.overlay-column",
            handle: ".widget-header",
            opacity: 0.7,
            tolerance: "pointer",
            update: $.proxy(this, "_onSortUpdate"),
            start: $.proxy(this, "_onSortStart"),
            stop: $.proxy(this, "_onSortStop")
        });
    },

    /**
     * Disables drag and drop sorting
     */
    _disableSortable: function()
    {
        this._child("td.overlay-column").sortable("destroy");
    },

    /**
     * Creates a new column.
     * Separated from addColumn() so that the initial columns can be created
     * with single sortable/resizable reset
     */
    _createColumn: function(width)
    {
        var total = this.element.width();
        var index = this._child(".overlay-column").size()
        var newcolumn = $("<td id='column_" + index + "' />");
        newcolumn.addClass("overlay-column");
        this._child("#overlay-columns").append(newcolumn);
        var count = this._child("#overlay-columns > td").size();
        this._child("#overlay-top").attr("colspan", count);
        var header = $("<th><div class='resize-guide'/></th>");
        header.css("width", width + "%");
        this._child("#overlay-header").append(header);
    },

    /**
     * Calculates the column widths and stores them in state
     */
    _updateColumnState: function()
    {
        var total = 0;
        this._child("#overlay-columns > td").each(function(){
            total += $(this).width();
        });
        var columns = [];
        this._child("#overlay-columns > td").each(function(){
            columns.push(Math.floor($(this).width() / total * 100));
        });
        this.columns = columns;
        this.columnChangeCb.fire(this.columns);
    },

    /**
     * Event handlers for drag and drop sort
     */
    _onSortUpdate: function(event, ui) {
        var column = $(event.target)
        // TODO change the top column id to 'column_0'
        var col = Number(column.attr("id").split("_")[1] || 0);
        this.widgetsMovedCb.fire(col, column.sortable("toArray"));
    },
    _onSortStart: function() {
        this._child("td.overlay-column").addClass("overlay-column-hint");
    },
    _onSortStop: function() {
        this._child("td.overlay-column").removeClass("overlay-column-hint");
    },


    /**
     * Disables drag and drop sort when widget is maximized
     */
    _onWidgetMaximize: function(maximized)
    {
        if (maximized) {
            this._disableSortable();
        } else {
            this._enableSortable();
        }
    },

    /**
     * Return any matching child elements.
     */
    _child: function(sel)
    {
        return $(sel, this.element);
    }

}, /* Class variables */ {
    DEFAULT_COLUMNS: [100],
    MAX_COLUMNS: 4,
    MIN_WIDTH: 100
});


/**
 * Dashboard 'model': this maintains the front end's notion of the workspace
 * state, which includes column widths, widget instances, and other overlay
 * settings.
 *
 */
var Dashboard = Base.extend({
    /**
     * Create an instance.
     *
     * @param config
     *      Dashboard configuration object passed in via JSON object in
     *      dashboard.html.
     */
    constructor: function(config)
    {
        this.overlay = {};
        this._oldOverlay = null;
        this._unsavedChanges = false;
        this.widgets = {};
        this.buttons = {};
        this.config = config || {};

        this.initUI();
        if (this.config.overlay_id) {
            this.loadOverlay(config.overlay_id);
        } else {
            this.welcomeOverlay();
        }
        window.onbeforeunload = $.proxy(this, "_onBeforeUnload");
    },

    initUI: function()
    {
        var dashboard = this;
        $("#buttons button").each(function(){
            var $elem = $(this);
            var name = $elem.attr("name");
            dashboard.buttons[name] = $elem;
            $elem.button({
                text: false,
                icons: {primary: "icon-" + name.toLowerCase()}
            });
            var callback = "onClick" + name[0].toUpperCase() + name.slice(1);
            $elem.click($.proxy(dashboard, callback));
        });

        this.overlayInfo = $("#overlay-info");
        this.overlayInfo.find(".workspace,.unsaved,.shared,.pending").hide();
        this.widgetSelect = $("#buttons [name='widgettype']");
        this.overlaySettings = $("#overlay-settings");
        this.overlayList = $("#overlay-open");
        this.notifyBox = $("#dashboard_notify");
    },

    /******************
     * Button handlers.
     */
    onClickNewoverlay: function()
    {
        if (!this._confirmUnsaved()) return;
        this.setOverlay(this._makeDefaultOverlay());
        this._setUnsaved(false);
    },

    onClickAddwidget: function()
    {
        var type = this.widgetSelect.val();

        var widget = this._createWidget({
            name: 'Unnamed widget',
            type: type
        });
        widget.onClickEdit();
    },

    onClickSaveoverlay: function()
    {
        if (!this.overlay.id) {
            this._openOverlaySettings(true);
        } else {
            this.saveOverlay(false);
        }
    },

    onClickSaveoverlayas: function()
    {
        if (this.overlay.id) {
            this._oldOverlay = $.extend({}, this.overlay);
            if (this.overlay.id) {
                delete this.overlay.id;
                this.overlay.name = "Copy of " + this._oldOverlay.name;
            } else {
                this.overlay.name = "";
            }
            this.overlay.description = "";
            this.overlay.shared = false;
        }
        this._openOverlaySettings(true);
    },

    onClickOpenoverlay: function()
    {
        if (!this._confirmUnsaved()) return;
        this.rpc("overlay_list").done($.proxy(this, "_openOverlayList"));
    },

    onClickDeleteoverlay: function()
    {
        if(!confirm("Do you really want to delete this overlay")) return;
        var rpc = this.rpc('overlay_delete', {
            id: this.overlay.id
        });
        rpc.done($.proxy(this, "_onDeleteOverlayDone"));
    },
    _onDeleteOverlayDone: function()
    {
        this.notify("Overlay deleted");
        this._setUnsaved(false);
        this.onClickNewoverlay();
    },

    onClickPublishoverlay: function()
    {
        if (!this.overlay.user_can_publish) {
            alert("You are not allowed to publish this overlay!");
            return;
        }
        var rpc = this.rpc('overlay_publish', {
            id: this.overlay.id, withhold: 0
        });
        rpc.done($.proxy(this, "_onPublishOverlayDone"));

    },
    onClickWithholdoverlay: function()
    {
        if (!this.overlay.user_can_publish) {
            alert("You are not allowed to withhold this overlay!");
            return;
        }
        var rpc = this.rpc('overlay_publish', {
            id: this.overlay.id, withhold: 1
        });
        rpc.done($.proxy(this, "_onPublishOverlayDone"));

    },
    _onPublishOverlayDone: function(pending)
    {
        this.overlay.pending = pending;
        this.buttons.publishoverlay.toggle(pending);
        this.buttons.withholdoverlay.toggle(!pending);
        $(".pending", this.overlayInfo).toggle(this.overlay.shared && pending);
        if (pending) {
            this.notify("Overlay withheld");
        } else {
            this.notify("Overlay published");
        }
    },

    // TODO Overlay UI object should bind directly to these buttons
    onClickAddcolumn: function()
    {
        this.overlayUI.addColumn();
    },
    onClickRemovecolumn: function()
    {
        this.overlayUI.removeColumn();
    },
    onClickResetcolumns: function()
    {
        this.overlayUI.resetColumns();
    },

    onClickOverlaysettings: function()
    {
        this._openOverlaySettings(false);
    },

    /***********************************
     * Overlay settings dialog handling.
     */
    _openOverlaySettings: function(save)
    {
        var overlay = this.overlay;
        this.overlaySettings.find(".settings-field").each(function(){
            var field = $(this);
            var name = field.attr("name");
            if (field.attr("type") == "checkbox") {
                field.prop("checked", Boolean(overlay[name]));
            } else {
                field.val(overlay[name]);
            }
        });
        var buttons = {
            "Cancel": $.proxy(this, "_cancelOverlaySettings")
        };
        if (save) {
            buttons["Save"] = $.proxy(this, "_saveOverlaySettings");
        } else {
            buttons["Apply"] = $.proxy(this, "_applyOverlaySettings");
        }

        this.overlaySettings.dialog({
            modal: true,
            width: 500,
            zIndex: 9999,
            buttons: buttons
        });
    },
    _cancelOverlaySettings: function()
    {
        if (this._oldOverlay != null) this.overlay = this._oldOverlay;
        this.overlaySettings.dialog("close");
    },
    _applyOverlaySettings: function()
    {
        var overlay = this.overlay;
        var changed = false;
        this.overlaySettings.find(".settings-field").each(function(){
            var field = $(this);
            var name = field.attr("name");
            var value = null;
            if (field.attr("type") == "checkbox") {
                value = field.prop("checked");
            } else {
                value = field.val();
            }
            if (overlay[name] != value) {
                overlay[name] = value;
                changed = true;
            }
        });
        this.overlaySettings.dialog("close");
        if (changed) this._setUnsaved(true);
    },
    _saveOverlaySettings: function()
    {
        this._applyOverlaySettings();
        this.saveOverlay(true);
    },

    /**
     * Create initial empty overlay with welcome message
     */
    welcomeOverlay: function()
    {
        this.setOverlay($.extend(this._makeDefaultOverlay(),
        {
            columns: [25, 50, 25],
            widgets: [{
                col: 0,
                pos: 0,
                type: 'text',
                name: 'Welcome to Dashboard',
                height: 500,
                data :{text: $('#dash-template-welcome-text').html()}
            }]
        }));
        this._setUnsaved(false);
    },

    /**
     * Create an empty overlay definition
     */
    _makeDefaultOverlay: function()
    {
        return {
            name: 'New Overlay',
            description: '',
            workspace: false,
            shared: false,
            pending: true,
            user_can_edit: true,
            user_can_publish: false,
            columns: this._makeColumns(3),
            widgets: []
        };
    },

    /**
     * Saves the current overlay state
     */
    saveOverlay: function(asnew)
    {
        var rpc = this.rpc("overlay_save", this._makeSaveParams(asnew));
        rpc.done($.proxy(this, "_saveDone"));
        rpc.fail($.proxy(this, "_saveFail"));
    },

    _saveDone: function(result)
    {
        this.notify("Overlay saved");
        this._oldOverlay = null;
        this.setOverlay(result.overlay);
        this._setUnsaved(false);
    },
    _saveFail: function(error)
    {
        if(this._oldOverlay != null) this.overlay = this._oldOverlay;
    },

    _openOverlayList: function(overlays)
    {
        this.overlayList.find("ul").empty();
        this.overlayList.find(".pending-list").toggle(
            DASHBOARD_CONFIG.can_publish);
        this.overlayList.dialog({
            modal: true,
            width: 500,
            zIndex: 9999,
            position: {my: 'center top', at: 'center top'}
        });
        var list = this.overlayList.find("ul.shared");
        for (var i = 0; i < overlays.length; i++) {
            if (!overlays[i].shared || overlays[i].pending) continue;
            list.append(this._createOverlayEntry(overlays[i]));
        }
        var list = this.overlayList.find("ul.owner");
        list.empty();
        for (var i = 0; i < overlays.length; i++) {
            if (overlays[i].owner.id != this.config.user_id) continue;
            list.append(this._createOverlayEntry(overlays[i]));
        }
        if (DASHBOARD_CONFIG.can_publish) {
            list = this.overlayList.find("ul.pending");
            for (var i = 0; i < overlays.length; i++) {
                if (!overlays[i].pending) continue;
                list.append(this._createOverlayEntry(overlays[i]));
            }
        }
    },

    /**
     * Renders single overlay entry for the open dialog
     */
    _createOverlayEntry: function(overlay)
    {
        var elem = cloneTemplate("#template-overlay-entry");
        var openButton = $(".name", elem);
        openButton.html(overlay.name || "<i>-no name-</i>");
        openButton.button({
            icons:{primary:"ui-icon-folder-open"}
        }).click({id: overlay.id}, $.proxy(this, "_onClickLoad"));

        openButton.next().button({
            text: false,
            icons:{primary:"ui-icon-triangle-1-s"}
        }).click(function(){
            $(this).parent().next().slideToggle("fast");
        });

        openButton.parent().buttonset();

        // jQuery.show() does not seem to work for some reason...
        if (overlay.workspace) $("span.workspace", elem).css("display", "inline");
        if (overlay.shared) {
            $("span.shared", elem).css("display", "inline");
            if (overlay.pending) $("span.pending", elem).css("display", "inline");
        }
        $(".owner", elem).text(overlay.owner.name);
        $(".description", elem).text(overlay.description);
        $(".modified", elem).text(overlay.modified);
        return elem;
    },

    /**
     * Overlay clicked in the open dialog.
     */
    _onClickLoad: function(event, ui) {
        this.overlayList.dialog("close");
        this.loadOverlay(event.data.id);
    },

    /**
     * Loads overlay with given ID
     */
    loadOverlay: function(id)
    {
        this.rpc("overlay_get", {id: id}).done(
                $.proxy(this, "_loadDone"));
    },
    _loadDone: function(overlay) 
    {
        this.notify("Overlay loaded");
        this.setOverlay(overlay);
        this._setUnsaved(false);
    },

    /**
     * Reset front-end state to match the overlay described by the given
     * JSON object.
     *
     * @param workspace
     *      Overlay JSON, as represented by overlay_get RPC
     */
    setOverlay: function(overlay)
    {
        this.overlay = overlay;

        // Destroy old widgets
        for(var id in this.widgets) {
            this.widgets[id].destroy();
            delete this.widgets[id];
        }

        // Create overlay UI
        this.overlayUI = new Overlay(this, overlay.columns);
        this.overlayUI.columnChangeCb.add($.proxy(this, "_onColumnChange"));
        // Create new widgets
        overlay.widgets.sort(widgetPosCmp);
        while(overlay.widgets.length) {
            this._createWidget(overlay.widgets.shift());
        }
        this.overlayUI.widgetsMovedCb.add($.proxy(this, "_onWidgetsMoved"));

        // Set overlay info
        this.overlayInfo.attr("href",
                "page.cgi?id=dashboard.html&overlay_id=" + (overlay.id || ""))
        $(".name", this.overlayInfo).text(overlay.name);
        $(".workspace", this.overlayInfo).toggle(overlay.workspace);
        $(".shared", this.overlayInfo).toggle(overlay.shared);
        $(".pending", this.overlayInfo).toggle(overlay.shared && overlay.pending);

        // Set buttons
        this.buttons.saveoverlay.toggle(overlay.user_can_edit);
        this.buttons.deleteoverlay.toggle(overlay.id && overlay.user_can_edit);
        this.buttons.publishoverlay.toggle(overlay.user_can_publish && overlay.pending);
        this.buttons.withholdoverlay.toggle(overlay.user_can_publish && !overlay.pending);
    },

    /**
     * Creates a widget and inserts it in the overlay ui.
     */
    _createWidget: function(state)
    {
        var widget = Widget.createInstance(this, state);
        widget.onRemoveCb.add($.proxy(this, "_onWidgetRemove"));
        widget.stateChangeCb.add($.proxy(this, "_onWidgetStateChange"));
        this.widgets[widget.id] = widget;
        this.overlayUI.insertWidget(widget);
        return widget;
    },

    /*******************************************
     * Column and widget postion change handling.
     */
    _onWidgetsMoved: function(col, widget_ids)
    {
        for (var i = 0; i < widget_ids.length; i++) {
            var id = widget_ids[i].split("_")[1];
            this.widgets[id].updateState({col: col, pos: i});
        }
    },
    _onWidgetRemove: function(widget)
    {
        this._setUnsaved(true)
        delete this.widgets[widget.id]
    },
    _onColumnChange: function(columns)
    {
        this._setUnsaved(true);
        this.overlay.columns = columns;
    },
    _onWidgetStateChange: function(widget)
    {
        this._setUnsaved(true);
    },

    _setUnsaved: function(unsaved)
    {
        // If user can't edit the overlay, there won't be any changes that
        // could be saved
        if (!this.overlay.user_can_edit) unsaved = false;
        $(".unsaved", this.overlayInfo).toggle(unsaved);
        this._unsavedChanges = unsaved;

    },
    
    _confirmUnsaved: function()
    {
        if (this._unsavedChanges && this.overlay.user_can_edit) {
            return confirm("There are unsaved changes. Continue?");
        }
        return true;
    },

    _onBeforeUnload: function()
    {
        if (this._unsavedChanges) {
            return "There are unsaved changes, which would be lost.";
        }
    },

    /********************************************
     * Start an RPC to the Dashboard web service.
     *
     * @param method
     *      Method name.
     * @param params
     *      Object containing key/value pairs to send to method.
     * @returns
     *      RPC object.
     */
    rpc: function(method, params, cb)
    {
        var rpc = new Rpc('Dashboard', method, params);
        rpc.fail(function(e) { alert(e.message ? e.message : e); });
        return rpc;
    },

    /**
     * Displays a notification in dashboard status box
     */
    notify: function(message)
    {
        this.notifyBox.text(message);
        this.notifyBox.stop(true, true).show()
            .delay(5000).fadeOut("slow");
    },

    /**
     * Return an array of equally sized column objects.
     *
     * @param count
     *      Number of columns.
     */
    _makeColumns: function(count)
    {
        var cols = [];
        var width = Math.floor(100 / count);
        for(var i = 0; i < count; i++) {
            cols.push(width);
        }
        return cols;
    },

    /**
     * Return structure that can be saved through rpc
     *
     * @param asnew
     *      If true, removes the id values so that overlay gets saved as new one
     */
    _makeSaveParams: function(asnew)
    {
        var overlay = $.extend({}, this.overlay, {
            widgets: this._getWidgetStates(asnew)
        });
        if (asnew) delete overlay.id;
        return overlay;
    },
    /**
     * Return an array describing state of widgets in the workspace, as
     * understood by the save rpc methods
     */
    _getWidgetStates: function(asnew)
    {
        var states = [];
        for (var id in this.widgets) {
            var state = $.extend({}, this.widgets[id].state);
            if (asnew) delete state.id;
            states.push(state);
        }
        return states;
    }
});


/**
 * Display a warning if the user's browser doesn't match the configured regex.
 * Block loading entirely if the browser is far too old.
 */
function checkBrowserQuality()
{
    var warn = DASHBOARD_CONFIG.browsers_warn;
    var block = DASHBOARD_CONFIG.browsers_block;

    if(warn && navigator.userAgent.match(RegExp(warn))) {
        $('#dashboard_notify').html($('#browser_warning_template'));
    } else if(block && navigator.userAgent.match(RegExp(block))) {
        $('#dashboard').html($('#browser_block_template'));
        throw ''; // Prevent further execution.
    }
}


/**
 * Main program implementation ('document.ready').
 *
 * Expects DASHBOARD_CONFIG global to be initialized by dashboard.html.tmpl
 * (which itself is populated by Extension.pm). This global contains various
 * configurables, and the initial workspace state (to avoid a redundant web
 * service roundtrip).
 */
function main()
{
    checkBrowserQuality();
    dashboard = new Dashboard(DASHBOARD_CONFIG);
}

$(document).ready(main);