diff --git a/www/scripts/apps/login/home/common.js b/www/scripts/apps/login/home/common.js new file mode 100644 index 0000000000..948ab92078 --- /dev/null +++ b/www/scripts/apps/login/home/common.js @@ -0,0 +1,35 @@ +/* + * This file is part of Phraseanet + * + * (c) 2005-2013 Alchemy + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +define([ + "jquery", + "underscore", + "backbone", + "bootstrap", + "multiselect" +], function($, _, Backbone, bootstrap, multiselect) { + var initialize = function() { + // close alerts + $(document).on("click", ".alert .alert-block-close a", function(e){ + e.preventDefault(); + $(this).closest('.alert').alert('close'); + return false; + }); + + $("select[multiple='multiple']").multiselect({ + buttonWidth : "100%", + buttonClass: 'btn btn-inverse' + }); + }; + + return { + initialize: initialize, + languagePath: '/login/language.json' + }; +}); diff --git a/www/scripts/apps/login/home/config.js b/www/scripts/apps/login/home/config.js new file mode 100644 index 0000000000..3735f4fc29 --- /dev/null +++ b/www/scripts/apps/login/home/config.js @@ -0,0 +1,31 @@ +/* + * This file is part of Phraseanet + * + * (c) 2005-2013 Alchemy + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +// configure AMD loading +require.config({ + baseUrl: "/scripts", + paths: { + jquery: "../include/jslibs/jquery-1.7.1", + jqueryui: "../include/jslibs/jquery-ui-1.8.17/js/jquery-ui-1.8.17.custom.min", + underscore: "../assets/underscore-amd/underscore", + backbone: "../assets/backbone-amd/backbone", + i18n: "../assets/i18next/release/i18next.amd-1.6.2.min", + bootstrap: "../skins/html5/bootstrap/js/bootstrap.min", + multiselect: "../assets/bootstrap-multiselect/js/bootstrap-multiselect" + }, + shim: { + bootstrap : ["jquery"], + jqueryui: { + deps: ["jquery"] + }, + multiselect: { + deps: ["jquery", "bootstrap"] + } + } +}); \ No newline at end of file diff --git a/www/scripts/apps/login/home/forgotPassword.js b/www/scripts/apps/login/home/forgotPassword.js new file mode 100644 index 0000000000..e4602291e9 --- /dev/null +++ b/www/scripts/apps/login/home/forgotPassword.js @@ -0,0 +1,34 @@ +/* + * This file is part of Phraseanet + * + * (c) 2005-2013 Alchemy + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +require([ + "jquery", + "i18n", + "apps/login/home/common", + "apps/login/home/views/form" +], function($, i18n, Common, ForgotPassWordForm) { + Common.initialize(); + + i18n.init({ + resGetPath: Common.languagePath + }, function() { + new ForgotPassWordForm({ + el : $("form[name=forgottenPasswordForm]"), + rules: [{ + name: "email", + rules: "required", + message: i18n.t("validation_blank") + },{ + name: "email", + rules: "valid_email", + message: i18n.t("validation_email") + }] + }); + }); +}); diff --git a/www/scripts/apps/login/home/login.js b/www/scripts/apps/login/home/login.js new file mode 100644 index 0000000000..d80ab2a03a --- /dev/null +++ b/www/scripts/apps/login/home/login.js @@ -0,0 +1,34 @@ +/* + * This file is part of Phraseanet + * + * (c) 2005-2013 Alchemy + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +require([ + "jquery", + "i18n", + "apps/login/home/common", + "apps/login/home/views/form" +], function($, i18n, Common, LoginForm) { + Common.initialize(); + + i18n.init({ + resGetPath: Common.languagePath + }, function() { + new LoginForm({ + el : $("form[name=loginForm]"), + rules: [{ + name: "login", + rules: "required", + message: i18n.t("validation_blank") + },{ + name: "password", + rules: "required", + message: i18n.t("validation_blank") + }] + }); + }); +}); diff --git a/www/scripts/apps/login/home/register.js b/www/scripts/apps/login/home/register.js new file mode 100644 index 0000000000..9d2a8c72c3 --- /dev/null +++ b/www/scripts/apps/login/home/register.js @@ -0,0 +1,88 @@ +/* + * This file is part of Phraseanet + * + * (c) 2005-2013 Alchemy + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +// launch application +require([ + "jquery", + "i18n", + "apps/login/home/common", + "apps/login/home/views/form" +], function(Common, RegisterForm) { + Common.initialize(); + + var fieldsConfiguration = []; + + $.when.apply($, [ + $.ajax({ + url: '/login/registration-fields/', + success: function(config) { + fieldsConfiguration = config; + } + }) + ]).done(function(){ + $.i18n.init({ + resGetPath: Common.languagePath + }, function() { + var rules = [{ + name: "email", + rules: "required", + message: i18n.t("validation_blank") + },{ + name: "email", + rules: "valid_email", + message: i18n.t("validation_email") + },{ + name: "password", + rules: "required", + message: i18n.t("validation_blank") + },{ + name: "password", + rules: "min_length[5]", + message: i18n.t("validation_length_min", { + postProcess: "sprintf", + sprintf: ["5"] + }) + },{ + name: "passwordConfirm", + rules: "matches[password]", + message: i18n.t("password_match") + },{ + name: "accept-tou", + rules: "required", + message: i18n.t("accept_tou"), + type: "checkbox" + },{ + name: "collections[]", + rules: "min_length[1]", + message: i18n.t("validation_choice_min", { + postProcess: "sprintf", + sprintf: ["1"] + }), + type: "multiple" + }]; + + _.each(fieldsConfiguration, function(field) { + if (field.required) { + var rule = { + "name": field.name, + "rules": "required", + "message": i18n.t("validation_blank") + }; + + rules.push(rule); + } + }); + + new RegisterForm({ + el : $("form[name=registerForm]"), + rules: rules + }); + }); + }); +}); \ No newline at end of file diff --git a/www/scripts/apps/login/home/registerProvider.js b/www/scripts/apps/login/home/registerProvider.js new file mode 100644 index 0000000000..376367a464 --- /dev/null +++ b/www/scripts/apps/login/home/registerProvider.js @@ -0,0 +1,88 @@ +/* + * This file is part of Phraseanet + * + * (c) 2005-2013 Alchemy + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +// launch application +require([ + "jquery", + "i18n", + "apps/login/home/common", + "apps/login/home/views/form" +], function($, i18n, Common, RegisterForm) { + Common.initialize(); + + var fieldsConfiguration = []; + + $.when.apply($, [ + $.ajax({ + url: '/login/registration-fields/', + success: function(config) { + fieldsConfiguration = config; + } + }) + ]).done(function(){ + i18n.init({ + resGetPath: Common.languagePath + }, function() { + var rules = [{ + name: "email", + rules: "required", + message: i18n.t("validation_blank") + },{ + name: "email", + rules: "valid_email", + message: i18n.t("validation_email") + },{ + name: "password", + rules: "required", + message: i18n.t("validation_blank") + },{ + name: "password", + rules: "min_length[5]", + message: i18n.t("validation_length_min", { + postProcess: "sprintf", + sprintf: ["5"] + }) + },{ + name: "passwordConfirm", + rules: "matches[password]", + message: i18n.t("password_match") + },{ + name: "accept-tou", + rules: "required", + message: i18n.t("accept_tou"), + type: "checkbox" + },{ + name: "collections[]", + rules: "min_length[1]", + message: i18n.t("validation_choice_min", { + postProcess: "sprintf", + sprintf: ["1"] + }), + type: "multiple" + }]; + + _.each(fieldsConfiguration, function(field) { + if (field.required) { + var rule = { + "name": field.name, + "rules": "required", + "message": i18n.t("validation_blank") + }; + + rules.push(rule); + } + }); + + new RegisterForm({ + el : $("form[name=registerForm]"), + rules: rules + }); + }); + }); +}); \ No newline at end of file diff --git a/www/scripts/apps/login/home/renewPassword.js b/www/scripts/apps/login/home/renewPassword.js new file mode 100644 index 0000000000..8e3149d26f --- /dev/null +++ b/www/scripts/apps/login/home/renewPassword.js @@ -0,0 +1,41 @@ +/* + * This file is part of Phraseanet + * + * (c) 2005-2013 Alchemy + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +require([ + "jquery", + "i18n", + "apps/login/home/common", + "apps/login/home/views/form" +], function($, i18n, Common, RenewPassword) { + Common.initialize(); + + i18n.init({ + resGetPath: Common.languagePath + }, function() { + new RenewPassword({ + el : $("form[name=passwordRenewForm]"), + rules: [{ + name: "password", + rules: "required", + message: i18n.t("validation_blank") + },{ + name: "password", + rules: "min_length[5]", + message: i18n.t("validation_length_min", { + postProcess: "sprintf", + sprintf: ["5"] + }) + },{ + name: "passwordConfirm", + rules: "matches[password]", + message: i18n.t("password_match") + }] + }); + }); +}); diff --git a/www/scripts/apps/login/home/views/form.js b/www/scripts/apps/login/home/views/form.js new file mode 100644 index 0000000000..fd5334a416 --- /dev/null +++ b/www/scripts/apps/login/home/views/form.js @@ -0,0 +1,86 @@ +/* + * This file is part of Phraseanet + * + * (c) 2005-2013 Alchemy + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +define([ + "jquery", + "underscore", + "backbone", + "bootstrap", + "common/validator", + "apps/login/home/views/inputView" +], function($, _, Backbone, bootstrap, Validator, InputView) { + var RegisterForm = Backbone.View.extend({ + events: { + "submit": "onSubmit" + }, + initialize: function(options) { + var self = this; + var rules = []; + + if (options) { + rules = options.rules || []; + } + // get a new validator defined rules + this.validator = new Validator(rules); + + this.inputViews = {}; + + // creates input views for each input + _.each(this.$el.serializeArray(), function (input) { + self._addInputView(input); + }); + }, + onSubmit: function (event) { + var self = this; + + // reset previous errors in the view + this._resetInputErrors(); + + // validate new values + this.validator.validate(this.$el.serializeArray()); + + if (this.validator.hasErrors()) { + // cancel submit + event.preventDefault(); + // group errors by input + _.each(_.groupBy(this.validator.getErrors(), function(error){ + return error.name; + }), function (errors, name) { + // show new errors in the views + if (name in self.inputViews) { + self.inputViews[name].showErrors(errors); + } else { + // Because serialize array do not serialize non checked input + // We must create view for errored input name on the fly + var input = {"name": name}; + + self._addInputView(input); + self.inputViews[name].showErrors(errors); + } + }); + } + }, + _resetInputErrors: function() { + _.each(this.inputViews, function(view) { + view.resetErrors(); + }); + }, + _addInputView: function(input) { + var name = input.name; + this.inputViews[name] = new InputView({ + name: name, + el : $("input[name='"+name+"'], select[name='"+name+"'], textarea[name='"+name+"']", this.$el).first().closest("div") + }); + + return this; + } + }); + + return RegisterForm; +});