diff --git a/.jshintrc b/.jshintrc index 8352ac84..9fdf93e7 100644 --- a/.jshintrc +++ b/.jshintrc @@ -20,6 +20,7 @@ "jQuery": false, "$": false, "QUnit": false, - "sinon": false + "sinon": false, + "moment": false } } diff --git a/src/js/ckeditor-loader.js b/src/js/ckeditor-loader.js index 57630b6c..cee81bdc 100644 --- a/src/js/ckeditor-loader.js +++ b/src/js/ckeditor-loader.js @@ -9,9 +9,15 @@ // and thus it would make it impossible to test this part of the code. $(window).load(function () { $('[data-onload-ckeditor]').each(function () { + var language = $('html').attr('lang'); var confObj = {}; var $this = $(this); var confValue = $this.data('onload-ckeditor'); + + if (language) { + confObj.language = language; + } + if (confValue) { if (typeof confValue === 'object') { confObj = confValue; diff --git a/src/js/datetimepicker-loader.js b/src/js/datetimepicker-loader.js index 531b312b..532cacf0 100644 --- a/src/js/datetimepicker-loader.js +++ b/src/js/datetimepicker-loader.js @@ -1,27 +1,43 @@ -;(function ($, window) { +;(function ($, moment, window) { 'use strict'; - // CKEDITOR-LOADER DATA-API - // ======================== + var DatetimePickerLoader = function ($element) { + this.$element = $element; + }; - (function ($, window) { - // We have to use $(winodow).load() as $(document).ready() can not be triggered manually - // and thus it would make it impossible to test this part of the code. - $(window).load(function () { - var initComponentFn = function (confObj, confValue) { - if (confValue) { - $.extend(confObj, confValue); - } + DatetimePickerLoader.prototype.filterLocale = function (locale) { + return moment.locale(locale); + }; - $(this).datetimepicker(confObj); + DatetimePickerLoader.prototype.init = function (confObj) { + confObj.locale = this.filterLocale(confObj.locale); + this.$element.datetimepicker(confObj); + }; + + // We have to use $(winodow).load() as $(document).ready() can not be triggered manually + // and thus it would make it impossible to test this part of the code. + $(window).load(function () { + var initComponentFn = function (inlineConf) { + var datetimePickerLoader = new DatetimePickerLoader($(this)); + var conf = { + allowInputToggle: true, + sideBySide: true, + locale: $('html').attr('lang'), }; - $('[data-onload-datetimepicker]').each(function () { - initComponentFn.call(this, { allowInputToggle: true, sideBySide: true }, $(this).data( - 'onload-datetimepicker' - )); - }); + if (inlineConf) { + $.extend(conf, inlineConf); + } + + datetimePickerLoader.init(conf); + }; + + // CKEDITOR-LOADER DATA-API + // ======================== + + $('[data-onload-datetimepicker]').each(function () { + initComponentFn.call(this, $(this).data('onload-datetimepicker')); }); - }($, window)); + }); -}(jQuery, window)); +}(jQuery, moment, window)); diff --git a/src/js/tests/index.html b/src/js/tests/index.html index a5050901..70f29020 100644 --- a/src/js/tests/index.html +++ b/src/js/tests/index.html @@ -7,6 +7,9 @@ + + + diff --git a/src/js/tests/unit/ckeditor-loader.js b/src/js/tests/unit/ckeditor-loader.js index abf90185..8c2f7049 100644 --- a/src/js/tests/unit/ckeditor-loader.js +++ b/src/js/tests/unit/ckeditor-loader.js @@ -8,6 +8,7 @@ $(function () { teardown: function () { delete jQuery.fn.ckeditor; + $('html').attr('lang', null); }, }); @@ -16,53 +17,61 @@ $(function () { // Data-api tests // ====================== - QUnit.test('should call the ckeditor() method on the textarea on page load with no attribute', - function () { - var $textarea = $(''); - $('#qunit-fixture').append($textarea); - - sinon.spy(jQuery.fn, 'ckeditor'); - $(window).trigger('load'); - - QUnit.ok(jQuery.fn.ckeditor.calledOnce, 'Should init the ckeditor'); - QUnit.ok( - jQuery.fn.ckeditor.calledWithExactly({}), - 'Should init the ckeditor with no arguments' - ); - } - ); - - QUnit.test('should call the ckeditor() method on the textarea on page load with string attribute', - function () { - var $textarea = $(''); - $('#qunit-fixture').append($textarea); - - sinon.spy(jQuery.fn, 'ckeditor'); - $(window).trigger('load'); - - QUnit.ok(jQuery.fn.ckeditor.calledOnce, 'Should init the ckeditor'); - QUnit.ok( - jQuery.fn.ckeditor.calledWithExactly({ customConfig: '/path/to/config.js' }), - 'Should init the ckeditor with proper custom config file specified' - ); - } - ); - - QUnit.test('should call the ckeditor() method on the textarea on page load with json attribute', - function () { - var $textarea = $( - '' + - ''); - $('#qunit-fixture').append($textarea); - - sinon.spy(jQuery.fn, 'ckeditor'); - $(window).trigger('load'); - - QUnit.ok(jQuery.fn.ckeditor.calledOnce, 'Should init the ckeditor'); - QUnit.ok( - jQuery.fn.ckeditor.calledWithExactly({ option: 'value' }), - 'Should init the ckeditor with proper config object' - ); - } - ); + QUnit.test('should init ckeditor on page load with no attribute', function () { + var $textarea = $(''); + $('#qunit-fixture').append($textarea); + + sinon.spy(jQuery.fn, 'ckeditor'); + $(window).trigger('load'); + + QUnit.ok(jQuery.fn.ckeditor.calledOnce, 'Should init the ckeditor'); + QUnit.ok( + jQuery.fn.ckeditor.calledWithExactly({}), + 'Should init the ckeditor with no arguments' + ); + }); + + QUnit.test('should init ckeditor on page load with string attribute', function () { + var $textarea = $(''); + $('#qunit-fixture').append($textarea); + + sinon.spy(jQuery.fn, 'ckeditor'); + $(window).trigger('load'); + + QUnit.ok(jQuery.fn.ckeditor.calledOnce, 'Should init the ckeditor'); + QUnit.ok( + jQuery.fn.ckeditor.calledWithExactly({ customConfig: '/path/to/config.js' }), + 'Should init the ckeditor with proper custom config file specified' + ); + }); + + QUnit.test('should init ckeditor on on page load with json attribute', function () { + var $textarea = $(''); + $('#qunit-fixture').append($textarea); + + sinon.spy(jQuery.fn, 'ckeditor'); + $(window).trigger('load'); + + QUnit.ok(jQuery.fn.ckeditor.calledOnce, 'Should init the ckeditor'); + QUnit.ok( + jQuery.fn.ckeditor.calledWithExactly({ option: 'value' }), + 'Should init the ckeditor with proper config object' + ); + }); + + QUnit.test('should init ckeditor on page load with correct language settings', function () { + var $textarea = $(''); + $('#qunit-fixture').append($textarea); + + sinon.spy(jQuery.fn, 'ckeditor'); + $('html').attr('lang', 'cs'); + $(window).trigger('load'); + + QUnit.ok(jQuery.fn.ckeditor.calledOnce, 'Should init the ckeditor'); + QUnit.ok( + jQuery.fn.ckeditor.calledWithExactly({ language: 'cs' }), + 'Should init the ckeditor with proper config object' + ); + }); + }); diff --git a/src/js/tests/unit/datetimepicker-loader.js b/src/js/tests/unit/datetimepicker-loader.js index f7d4c9db..d239e34d 100644 --- a/src/js/tests/unit/datetimepicker-loader.js +++ b/src/js/tests/unit/datetimepicker-loader.js @@ -4,10 +4,17 @@ $(function () { QUnit.module('datetimepicker-loader', { setup: function () { jQuery.fn.datetimepicker = function () {}; + + moment.localeOrig = moment.locale; + moment.locale = function () {}; }, teardown: function () { + moment.locale = moment.localeOrig; + $('html').attr('lang', null); + delete moment.localeOrig; delete jQuery.fn.datetimepicker; + }, }); @@ -17,23 +24,28 @@ $(function () { // Data-api tests // ====================== QUnit.test( - 'should init datetimepicker with default config on the html element on page load', + 'should init datetimepicker with default config on page load', function () { var $input = $(''); $('#qunit-fixture').append($input); sinon.spy(jQuery.fn, 'datetimepicker'); + sinon.stub(moment, 'locale').withArgs().returns('fake-locale'); $(window).trigger('load'); QUnit.ok(jQuery.fn.datetimepicker.calledOnce, 'Should init the datetimepicker'); QUnit.ok( - jQuery.fn.datetimepicker.calledWith({ allowInputToggle: true, sideBySide: true }), + jQuery.fn.datetimepicker.calledWith({ + allowInputToggle: true, + sideBySide: true, + locale: 'fake-locale', + }), 'Should init the datetimepicker with default config' ); } ); - QUnit.test('should init datetimepicker with some config option on the html element on page load', + QUnit.test('should init datetimepicker with some config option on page load', function () { var $input = $( '' @@ -41,6 +53,7 @@ $(function () { $('#qunit-fixture').append($input); sinon.spy(jQuery.fn, 'datetimepicker'); + sinon.stub(moment, 'locale').withArgs().returns('fake-locale'); $(window).trigger('load'); QUnit.ok(jQuery.fn.datetimepicker.calledOnce, 'Should init the datetimepicker'); @@ -49,9 +62,31 @@ $(function () { option: 'optionValue', allowInputToggle: true, sideBySide: true, + locale: 'fake-locale', }), 'Should init the datetimepicker with specified config object' ); } ); + + QUnit.test('should init datetimepicker with locale', function () { + var $input = $(''); + + $('html').attr('lang', 'cs'); + $('#qunit-fixture').append($input); + sinon.spy(jQuery.fn, 'datetimepicker'); + sinon.stub(moment, 'locale').withArgs('cs').returns('fake-locale'); + $(window).trigger('load'); + + QUnit.ok(jQuery.fn.datetimepicker.calledOnce, 'Should init the datetimepicker'); + QUnit.ok( + jQuery.fn.datetimepicker.calledWith({ + locale: 'fake-locale', + allowInputToggle: true, + sideBySide: true, + }), + 'Should init the datetimepicker with locale set' + ); + }); + }); diff --git a/src/less/components/javascript/ckeditor-loader.less b/src/less/components/javascript/ckeditor-loader.less index 92a1cfdc..471b2115 100644 --- a/src/less/components/javascript/ckeditor-loader.less +++ b/src/less/components/javascript/ckeditor-loader.less @@ -14,8 +14,8 @@ options. ### Usage -This loader only calls the jQuery plugin defined in CKEditor jQuery adapter with appropriate arguments. Id does nothing -else. +This loader only calls the jQuery plugin defined in CKEditor jQuery adapter with appropriate arguments. +If the `lang` attribute is set on the `` element the CKEditor is localized accordingly. #### Data-API diff --git a/src/less/components/javascript/datetimepicker-loader.less b/src/less/components/javascript/datetimepicker-loader.less index 8fa7a3aa..2e2fde5e 100644 --- a/src/less/components/javascript/datetimepicker-loader.less +++ b/src/less/components/javascript/datetimepicker-loader.less @@ -8,13 +8,14 @@ Initializes [Bootstrap Datetimepicker](http://eonasdan.github.io/bootstrap-datet

JavaScript Required

- Scripts `datetimepicker-loader.js`, `moment.js` (this one before `bootstrap.js`) and `bootstrap-datetimepicker.js` must be included. + Scripts `datetimepicker-loader.js`, `moment.js` (optionaly with locales, before `bootstrap.js`) and `bootstrap-datetimepicker.js` must be included.
### Usage -This loader only calls the jQuery plugin defined in bootstrap-datetimepicker component. It does nothing else. +This loader only calls the jQuery plugin defined in bootstrap-datetimepicker component. +If the `lang` attribute is set on the `` element the datetimepicker is localized accordingly. #### Data-API diff --git a/src/less/components/javascript/select2-loader.less b/src/less/components/javascript/select2-loader.less index 8052b779..06caefb9 100644 --- a/src/less/components/javascript/select2-loader.less +++ b/src/less/components/javascript/select2-loader.less @@ -14,8 +14,8 @@ options. ### Usage -This loader only calls the jQuery plugin defined in the select2 library with appropriate arguments. It does nothing -else. +This loader only calls the jQuery plugin defined in the select2 library with appropriate arguments. +The select2 component natively uses the `lang` attribute of any parent element, so just be sure to include the correct locale file. #### Data-API