~pkunal-parmar/ubuntu-calendar-app/EventBubble-Truncation

« back to all changes in this revision

Viewing changes to EventUtils.qml

Rewrote GetRecurrenceString function in Event Utils. Fixes: https://bugs.launchpad.net/bugs/1370431, https://bugs.launchpad.net/bugs/1372054.

Approved by Kunal Parmar, Ubuntu Phone Apps Jenkins Bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
import Ubuntu.Components 1.1
21
21
import QtOrganizer 5.0
22
22
import "Defines.js" as Defines
 
23
import "Recurrence.js" as Recurrence
23
24
 
24
25
 
25
26
QtObject{
26
27
    id:eventUtil
 
28
 
27
29
    function getWeekDaysIndex(daysOfWeek){
28
 
        var index = 0;
29
 
        if(compareArrays(daysOfWeek,[Qt.Monday,Qt.Tuesday,Qt.Wednesday,Qt.Thursday,Qt.Friday]))
30
 
            index = 2
31
 
        else if(compareArrays(daysOfWeek,[Qt.Monday,Qt.Wednesday,Qt.Friday]))
32
 
            index = 3
33
 
        else if(compareArrays(daysOfWeek,[Qt.Tuesday,Qt.Thursday]))
34
 
            index = 4
35
 
        else
36
 
            index = 5
37
 
        return index;
 
30
        for (var index = Recurrence.Weekdays; index < Recurrence.OnDiffDays; index++) {
 
31
            if (compareArrays(daysOfWeek, Recurrence.weeklyDays[index])) {
 
32
                return index;
 
33
            }
 
34
        }
 
35
        return Recurrence.OnDiffDays;
38
36
    }
39
37
 
40
 
    function compareArrays(daysOfWeek, actualArray){
 
38
    function compareArrays(daysOfWeek, actualArray) {
41
39
        if (daysOfWeek.length !== actualArray.length) return false;
42
40
        for (var i = 0; i < actualArray.length; i++) {
43
41
            if (daysOfWeek[i] !== actualArray[i]) return false;
44
42
        }
45
43
        return true;
46
44
    }
47
 
    function getDaysOfWeek(index,weekDays){
 
45
 
 
46
    function getDaysOfWeek(index, weekDays) {
48
47
        var daysOfWeek = [];
49
 
        switch(index){
50
 
        case 2:
51
 
            daysOfWeek = [Qt.Monday,Qt.Tuesday,Qt.Wednesday,Qt.Thursday,Qt.Friday];
52
 
            break;
53
 
        case 3:
54
 
            daysOfWeek = [Qt.Monday,Qt.Wednesday,Qt.Friday];
55
 
            break;
56
 
        case 4:
57
 
            daysOfWeek = [Qt.Tuesday,Qt.Thursday];
58
 
            break;
59
 
        case 5:
 
48
        if (index !== Recurrence.OnDiffDays) {
 
49
            daysOfWeek = Recurrence.weeklyDays[index];
 
50
        } else {
60
51
            daysOfWeek = weekDays.length === 0 ? [date.getDay()] : weekDays;
61
 
            break;
62
52
        }
63
53
        return daysOfWeek;
64
54
    }
 
55
 
65
56
    //Function to get Weeknames in narrow Format
66
57
    function getDays(daysOfWeek) {
67
58
        var days = []
68
 
        for(var j = 0;j<daysOfWeek.length;++j){
 
59
        for (var j = 0; j < daysOfWeek.length; ++j) {
69
60
            //push all days
70
 
            days.push(Qt.locale().dayName(daysOfWeek[j],Locale.NarrowFormat))
 
61
            days.push(Qt.locale().dayName(daysOfWeek[j], Locale.NarrowFormat))
71
62
        }
72
63
        days = days.join(', ');
73
64
        return days;
74
65
    }
75
66
 
76
 
    function getRecurrenceString(rule){
77
 
 
78
 
        var index;
79
 
        var reccurence = "";
80
 
        var limit,str = "";
 
67
    function getString(rule,recurrence){
81
68
        var dateFormat = Qt.locale().dateFormat(Locale.LongFormat);
82
 
        index = rule.frequency;
83
 
        if(index === RecurrenceRule.Weekly){
84
 
            index = getWeekDaysIndex(rule.daysOfWeek.sort() )
85
 
            reccurence = "Weekly "
86
 
            if(index === 5){
87
 
                reccurence +=  "on " + getDays(rule.daysOfWeek.sort())
88
 
            }
 
69
        var str = "";
 
70
        if (rule.limit === undefined) {
 
71
            str = i18n.tr(recurrence)
 
72
        } else if (rule.limit !== undefined && parseInt(rule.limit)) {
 
73
            // TRANSLATORS: the argument refers to multiple recurrence of event with count .
 
74
            // E.g. "Daily; 5 times."
 
75
            str = i18n.tr("%1; %2 time", "%1; %2 times", rule.limit).arg(recurrence).arg(rule.limit)
 
76
        } else {
 
77
            // TRANSLATORS: the argument refers to recurrence until user selected date.
 
78
            // E.g. "Daily; until 12/12/2014."
 
79
            str = i18n.tr("%1; until %2").arg(recurrence).arg(rule.limit.toLocaleString(Qt.locale(), dateFormat))
89
80
        }
90
 
        else if(index === RecurrenceRule.Monthly)
91
 
            index = 6
92
 
        else if(index === RecurrenceRule.Yearly)
93
 
            index = 7
94
 
        if(index !==5)
95
 
            reccurence += Defines.recurrenceLabel[index]
 
81
        return str;
 
82
    }
96
83
 
97
 
        str = (rule.limit === undefined) ? i18n.tr(reccurence) :
98
 
                                           (rule.limit !== undefined && parseInt(rule.limit)) ?
99
 
                                               i18n.tr("%1 ; %2 times ").arg(reccurence).arg(rule.limit) :
100
 
                                               i18n.tr("%1 ;  until %2").arg(reccurence).arg(rule.limit.toLocaleString(Qt.locale(), dateFormat))
 
84
    function getRecurrenceString(rule) {
 
85
        var index = rule.frequency;
 
86
        var recurrence = "";
 
87
        var str = "";
 
88
        //Check if reccurence is weekly or not
 
89
        if (index === RecurrenceRule.Weekly) {
 
90
            index = getWeekDaysIndex(rule.daysOfWeek.sort())
 
91
            // We are using a custom index
 
92
            // because we have more options than the Qt RecurrenceRule enum.
 
93
        } else if (index === RecurrenceRule.Monthly) {
 
94
            index = Recurrence.Monthly // If reccurence is Monthly
 
95
        } else if (index === RecurrenceRule.Yearly) {
 
96
            index = Recurrence.Yearly // If reccurence is Yearly
 
97
        }
 
98
        // if reccurrence is on different days.
 
99
        if (index === Recurrence.OnDiffDays) {
 
100
            // TRANSLATORS: the argument refers to several different days of the week.
 
101
            // E.g. "Weekly on Mondays, Tuesdays"
 
102
            recurrence += i18n.tr("Weekly on %1").arg(getDays(rule.daysOfWeek.sort()))
 
103
        } else {
 
104
            recurrence += Defines.recurrenceLabel[index]
 
105
        }
 
106
        str = getString(rule,recurrence);
101
107
        return str;
102
108
    }
103
109
}