in elgg jQuery datepicker options do not work

476 views Asked by At

My very first experiments with elgg. elgg_view('input/date' uses the datepicker function, but I need to add some options.

In my plugin (called "help") initialization function (start.php) I declared elgg_extend_view('js/elgg', 'help/js'); Then, in \mod\help\views\default\help\js.php I copied the elgg.ui.initDatePicker function from the elgg core and I added my options to it. Also I added

elgg.provide('elgg.help');

on the top and

elgg.register_hook_handler('init', 'system', elgg.ui.initDatePicker);

on the bottom, like this:

-- begin of \mod\help\views\default\help\js.php code

elgg.provide('elgg.help');

elgg.ui.initDatePicker = function() {
    var loadDatePicker = function() {
        $('.elgg-input-date').datepicker({

            // MY OPTIONS ADDED
            yearRange: "-100:+0",
            changeYear: true,
            changeMonth: true,

            // ISO-8601
            dateFormat: 'yy-mm-dd',
            onSelect: function(dateText) {
                if ($(this).is('.elgg-input-timestamp')) {
                    // convert to unix timestamp
                    var dateParts = dateText.split("-");
                    var timestamp = Date.UTC(dateParts[0], dateParts[1] - 1, dateParts[2]);
                    timestamp = timestamp / 1000;

                    var id = $(this).attr('id');
                    $('input[name="' + id + '"]').val(timestamp);
                }
            }
        });
    };

    if ($('.elgg-input-date').length && elgg.get_language() == 'en') {
        loadDatePicker();
    } else if ($('.elgg-input-date').length) {
        elgg.get({
            url: elgg.config.wwwroot + 'vendors/jquery/i18n/jquery.ui.datepicker-'+ elgg.get_language() +'.js',
            dataType: "script",
            cache: true,
            success: loadDatePicker,
            error: loadDatePicker // english language is already loaded.
        });
    }
}

elgg.register_hook_handler('init', 'system', elgg.ui.initDatePicker);

--- end of \mod\help\views\default\help\js.php code

It does indeed open a calendar, but it does not apply my yearRange,changeYear and changeMonth options. Though, if I just add the options in elgg\js\lib\ui.js it works fine. Since I don't want to mess with the core file I ask for hints here. Thank you

2

There are 2 answers

0
Mani On

Use

elgg_view('input/datepicker',array('name'=>'startDate'));

FOR elgg 1.8.16

0
Paweł Sroka On

Note that you overwrite core version of elgg.ui.initDatePicker this way.

You seem to register same function for the second time on init system (it's registered by core, you've just changed implementation)

You also don't need to run elgg.provide if you don't use provided (elgg.help) object.

Apart from this notes, I'd make sure the JS code is loaded on time. How do you load the code? elgg_load_js or some view extension?