Completed
Push — master ( b8f763...47a15d )
by Craig
14:22 queued 07:28
created

  B

Complexity

Conditions 2
Paths 2

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 43
rs 8.8571
c 0
b 0
f 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A ZikulaRoutesModule.EditFunctions.js ➔ ... ➔ $(?!?).bind(ꞌtypeahead:selectꞌ) 0 5 1
A ZikulaRoutesModule.EditFunctions.js ➔ ... ➔ $(?!?).typeahead.templates.suggestion 0 12 1
A ZikulaRoutesModule.EditFunctions.js ➔ ... ➔ $(?!?).typeahead.source 0 8 1
1
'use strict';
2
3
/**
4
 * Initialises a user field with auto completion.
5
 */
6
function zikulaRoutesInitUserField(fieldName, getterName)
7
{
8
    if (jQuery('#' + fieldName + 'LiveSearch').length < 1) {
9
        return;
10
    }
11
    jQuery('#' + fieldName + 'LiveSearch').removeClass('hidden');
12
13
    jQuery('#' + fieldName + 'Selector').typeahead({
14
        highlight: true,
15
        hint: true,
16
        minLength: 2
17
    }, {
18
        limit: 15,
19
        // The data source to query against. Receives the query value in the input field and the process callbacks.
20
        source: function (query, syncResults, asyncResults) {
21
            // Retrieve data from server using "query" parameter as it contains the search string entered by the user
22
            jQuery('#' + fieldName + 'Indicator').removeClass('hidden');
23
            jQuery.getJSON(Routing.generate('zikularoutesmodule_ajax_' + getterName.toLowerCase(), { fragment: query }), function( data ) {
0 ignored issues
show
Bug introduced by
The variable Routing seems to be never declared. If this is a global, consider adding a /** global: Routing */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
24
                jQuery('#' + fieldName + 'Indicator').addClass('hidden');
25
                asyncResults(data);
26
            });
27
        },
28
        templates: {
29
            empty: '<div class="empty-message">' + jQuery('#' + fieldName + 'NoResultsHint').text() + '</div>',
30
            suggestion: function(user) {
31
                var html;
32
33
                html = '<div class="typeahead">';
34
                html += '<div class="media"><a class="pull-left" href="javascript:void(0)">' + user.avatar + '</a>';
35
                html += '<div class="media-body">';
36
                html += '<p class="media-heading">' + user.uname + '</p>';
37
                html += '</div>';
38
                html += '</div>';
39
40
                return html;
41
            }
42
        }
43
    }).bind('typeahead:select', function(ev, user) {
44
        // Called after the user selects an item. Here we can do something with the selection.
45
        jQuery('#' + fieldName).val(user.uid);
46
        jQuery(this).typeahead('val', user.uname);
47
    });
48
}
49
50
51
var editedObjectType;
52
var editedEntityId;
53
var editForm;
54
var formButtons;
55
var triggerValidation = true;
56
57
function zikulaRoutesTriggerFormValidation()
58
{
59
    zikulaRoutesExecuteCustomValidationConstraints(editedObjectType, editedEntityId);
60
61
    if (!editForm.get(0).checkValidity()) {
62
        // This does not really submit the form,
63
        // but causes the browser to display the error message
64
        editForm.find(':submit').first().click();
65
    }
66
}
67
68
function zikulaRoutesHandleFormSubmit (event) {
69
    if (triggerValidation) {
70
        zikulaRoutesTriggerFormValidation();
71
        if (!editForm.get(0).checkValidity()) {
72
            event.preventDefault();
73
            return false;
74
        }
75
    }
76
77
    // hide form buttons to prevent double submits by accident
78
    formButtons.each(function (index) {
0 ignored issues
show
Unused Code introduced by
The parameter index is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
79
        jQuery(this).addClass('hidden');
80
    });
81
82
    return true;
83
}
84
85
/**
86
 * Initialises an entity edit form.
87
 */
88
function zikulaRoutesInitEditForm(mode, entityId)
89
{
90
    if (jQuery('.zikularoutes-edit-form').length < 1) {
91
        return;
92
    }
93
94
    editForm = jQuery('.zikularoutes-edit-form').first();
95
    editedObjectType = editForm.attr('id').replace('EditForm', '');
96
    editedEntityId = entityId;
97
98
    if (jQuery('#moderationFieldsSection').length > 0) {
99
        jQuery('#moderationFieldsContent').addClass('hidden');
100
        jQuery('#moderationFieldsSection legend').addClass('pointer').click(function (event) {
0 ignored issues
show
Unused Code introduced by
The parameter event is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
101
            if (jQuery('#moderationFieldsContent').hasClass('hidden')) {
102
                jQuery('#moderationFieldsContent').removeClass('hidden');
103
                jQuery(this).find('i').removeClass('fa-expand').addClass('fa-compress');
104
            } else {
105
                jQuery('#moderationFieldsContent').addClass('hidden');
106
                jQuery(this).find('i').removeClass('fa-compress').addClass('fa-expand');
107
            }
108
        });
109
    }
110
111
    var allFormFields = editForm.find('input, select, textarea');
112
    allFormFields.change(function (event) {
0 ignored issues
show
Unused Code introduced by
The parameter event is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
113
        zikulaRoutesExecuteCustomValidationConstraints(editedObjectType, editedEntityId);
114
    });
115
116
    formButtons = editForm.find('.form-buttons input');
117
    editForm.find('.btn-danger').first().bind('click keypress', function (event) {
118
        if (!window.confirm(Translator.__('Do you really want to delete this entry?'))) {
0 ignored issues
show
Bug introduced by
The variable Translator seems to be never declared. If this is a global, consider adding a /** global: Translator */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
119
            event.preventDefault();
120
        }
121
    });
122
    editForm.find('button[type=submit]').bind('click keypress', function (event) {
0 ignored issues
show
Unused Code introduced by
The parameter event is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
123
        triggerValidation = !jQuery(this).attr('formnovalidate');
124
    });
125
    editForm.submit(zikulaRoutesHandleFormSubmit);
126
127
    if (mode != 'create') {
128
        zikulaRoutesTriggerFormValidation();
129
    }
130
}
131
132