From 18316325469891d7044d4f88c5ff1ee2024788ff Mon Sep 17 00:00:00 2001 From: Lars Strojny Date: Sat, 21 Apr 2012 11:18:28 +0200 Subject: [PATCH] Removing command experiment. Now lives in experiments/services-as-commands --- .../Resources/public/js/backbone-forms.js | 324 +++++++++--------- .../Command/AddMemberCommand.php | 21 -- .../Command/CommandChain.php | 49 --- .../Command/CommandInterface.php | 9 - .../Command/CreateOrganisationCommand.php | 21 -- .../OrganisationBundle/Command/Parameters.php | 59 ---- .../Command/ParametersInterface.php | 11 - .../Command/CreateOrganisationCommandTest.php | 38 -- 8 files changed, 162 insertions(+), 370 deletions(-) delete mode 100644 src/LogTweets/Bundle/OrganisationBundle/Command/AddMemberCommand.php delete mode 100644 src/LogTweets/Bundle/OrganisationBundle/Command/CommandChain.php delete mode 100644 src/LogTweets/Bundle/OrganisationBundle/Command/CommandInterface.php delete mode 100644 src/LogTweets/Bundle/OrganisationBundle/Command/CreateOrganisationCommand.php delete mode 100644 src/LogTweets/Bundle/OrganisationBundle/Command/Parameters.php delete mode 100644 src/LogTweets/Bundle/OrganisationBundle/Command/ParametersInterface.php delete mode 100644 src/LogTweets/Bundle/OrganisationBundle/Tests/Command/CreateOrganisationCommandTest.php diff --git a/src/LogTweets/Bundle/BackboneJsBundle/Resources/public/js/backbone-forms.js b/src/LogTweets/Bundle/BackboneJsBundle/Resources/public/js/backbone-forms.js index b2dbb36..72c603b 100644 --- a/src/LogTweets/Bundle/BackboneJsBundle/Resources/public/js/backbone-forms.js +++ b/src/LogTweets/Bundle/BackboneJsBundle/Resources/public/js/backbone-forms.js @@ -6,24 +6,24 @@ * License and more information at: * http://github.com/powmedia/backbone-forms */ -;(function($) { - +;(function($) { + //================================================================================================== //TEMPLATES //================================================================================================== - + var templates = { form: '\
{{fieldsets}}
\ ', - + fieldset: '\
\ {{legend}}\ \
\ ', - + field: '\
  • \ \ @@ -32,17 +32,17 @@
  • \ ' }; - + var classNames = { error: 'bbf-error' }; - - - + + + //================================================================================================== //HELPERS //================================================================================================== - + //Support paths for nested attributes e.g. 'user.name' function getNested(obj, path) { var fields = path.split("."); @@ -52,16 +52,16 @@ } return result; } - + var helpers = {}; - + /** * This function is used to transform the key from a schema into the title used in a label. * (If a specific title is provided it will be used instead). - * + * * By default this converts a camelCase string into words, i.e. Camel Case * If you have a different naming convention for schema keys, replace this function. - * + * * @param {String} Key * @return {String} Title */ @@ -83,7 +83,7 @@ * @return {Template} Compiled template */ helpers.createTemplate = function(str, context) { - //Store user's template options + //Store user's template options var _interpolateBackup = _.templateSettings.interpolate; //Set custom template settings @@ -93,50 +93,50 @@ //Reset to users' template settings _.templateSettings.interpolate = _interpolateBackup; - + if (!context) { return template; } else { return template(context); } }; - - + + /** * Sets the templates to be used. - * + * * If the templates passed in are strings, they will be compiled, expecting Mustache style tags, * i.e.
    {{varName}}
    * * You can also pass in previously compiled Underscore templates, in which case you can use any style * tags. - * + * * @param {Object} templates * @param {Object} classNames */ helpers.setTemplates = function(templates, classNames) { var createTemplate = helpers.createTemplate; - + Form.templates = Form.templates || {}; Form.classNames = Form.classNames || {}; - + //Set templates, compiling them if necessary _.each(templates, function(template, key, index) { if (_.isString(template)) template = createTemplate(template); - + Form.templates[key] = template; }); - + //Set class names _.extend(Form.classNames, classNames); }; - - + + /** * Return the editor constructor for a given schema 'type'. * Accepts strings for the default editors, or the reference to the constructor function * for custom editors - * + * * @param {String|Function} The schema type e.g. 'Text', 'Select', or the editor constructor e.g. editors.Date * @param {Object} Options to pass to editor, including required 'key', 'schema' * @return {Mixed} An instance of the mapped editor @@ -151,35 +151,35 @@ return new constructorFn(options); }; - + /** * Triggers an event that can be cancelled. Requires the user to invoke a callback. If false * is passed to the callback, the action does not run. * * NOTE: This helper uses private Backbone apis so can break when Backbone is upgraded - * + * * @param {Mixed} Instance of Backbone model, view, collection to trigger event on * @param {String} Event name * @param {Array} Arguments to pass to the event handlers * @param {Function} Callback to run after the event handler has run. * If any of them passed false or error, this callback won't run - */ - helpers.triggerCancellableEvent = function(subject, event, args, callback) { + */ + helpers.triggerCancellableEvent = function(subject, event, args, callback) { //Return if there are no event listeners if (!subject._callbacks || !subject._callbacks[event]) return callback(); - + var next = subject._callbacks[event].next; if (!next) return callback(); - + var fn = next.callback, context = next.context || this; - + //Add the callback that will be used when done args.push(callback); - + fn.apply(context, args); } - + /** * Returns a validation function based on the type defined in the schema * @@ -191,11 +191,11 @@ if (_.isRegExp(validator)) { return validators.regexp({ regexp: validator }); } - + //Use a built-in validator if given a string if (_.isString(validator)) { if (!validators[validator]) throw new Error('Validator "'+validator+'" not found'); - + return validators[validator](); } @@ -205,22 +205,22 @@ //Use a customised built-in validator if given an object if (_.isObject(validator) && validator.type) { var config = validator; - + return validators[config.type](config); } - + //Unkown validator type throw new Error('Invalid validator: ' + validator); }; - + //================================================================================================== //VALIDATORS //================================================================================================== - + var validators = {}; - + validators.errMessages = { required: 'Required', regexp: 'Invalid', @@ -228,100 +228,100 @@ url: 'Invalid URL', match: 'Must match field "{{field}}"' } - - validators.required = function(options) { + + validators.required = function(options) { options = _.extend({ type: 'required', message: this.errMessages.required }, options); - + return function required(value) { options.value = value; - + var err = { type: options.type, message: helpers.createTemplate(options.message, options) }; - + if (value === null || value === undefined || value === '') return err; }; }; - + validators.regexp = function(options) { if (!options.regexp) throw new Error('Missing required "regexp" option for "regexp" validator'); - + options = _.extend({ type: 'regexp', message: this.errMessages.regexp }, options); - + return function regexp(value) { options.value = value; - + var err = { type: options.type, message: helpers.createTemplate(options.message, options) }; - + //Don't check empty values (add a 'required' validator for this) if (value === null || value === undefined || value === '') return; if (!options.regexp.test(value)) return err; }; }; - + validators.email = function(options) { options = _.extend({ type: 'email', message: this.errMessages.email, regexp: /^[\w\-]{1,}([\w\-.]{1,1}[\w\-]{1,}){0,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/ }, options); - + return validators.regexp(options); }; - + validators.url = function(options) { options = _.extend({ type: 'url', message: this.errMessages.url, regexp: /^(http|https):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i }, options); - + return validators.regexp(options); }; - + validators.match = function(options) { if (!options.field) throw new Error('Missing required "field" options for "match" validator'); - + options = _.extend({ type: 'match', message: this.errMessages.match }, options); - + return function match(value, attrs) { options.value = value; - + var err = { type: options.type, message: helpers.createTemplate(options.message, options) }; - + //Don't check empty values (add a 'required' validator for this) if (value === null || value === undefined || value === '') return; - + if (value != attrs[options.field]) return err; } }; - + //================================================================================================== //FORM //================================================================================================== - + var Form = Backbone.View.extend({ - + //Field views fields: null, @@ -337,26 +337,26 @@ * idPrefix {String} : Prefix for editor IDs. If undefined, the model's CID is used. * template {String} : Template to use. Default to 'form'. */ - initialize: function(options) { + initialize: function(options) { //Get the schema this.schema = (function() { if (options.schema) return options.schema; - + var model = options.model; if (!model) throw new Error('Could not find schema'); - + if (_.isFunction(model.schema)) return model.schema(); - + return model.schema; })(); - + //Handle other options this.model = options.model; this.data = options.data; this.fieldsToRender = options.fields || _.keys(this.schema); this.fieldsets = options.fieldsets; this.templateName = options.template || 'form'; - + //Stores all Field views this.fields = {}; }, @@ -369,12 +369,12 @@ fieldsToRender = this.fieldsToRender, fieldsets = this.fieldsets, templates = Form.templates; - + //Create el from template var $form = $(templates[this.templateName]({ fieldsets: '
    ' })); - + //Get a reference to where fieldsets should go and remove the placeholder var $fieldsetContainer = $('.bbf-placeholder', $form).parent(); $fieldsetContainer.html(''); @@ -385,18 +385,18 @@ if (_(fs).isArray()) { fs = {'fields': fs}; } - + //Concatenating HTML as strings won't work so we need to insert field elements into a placeholder var $fieldset = $(templates.fieldset({ legend: (fs.legend) ? '' + fs.legend + '' : '', fields: '
    ' })); - + var $fieldsContainer = $('.bbf-placeholder', $fieldset).parent(); $fieldsContainer.html(''); - + self.renderFields(fs.fields, $fieldsContainer); - + $fieldsetContainer.append($fieldset); }); } else { @@ -405,12 +405,12 @@ legend: '', fields: '
    ' })); - + var $fieldsContainer = $('.bbf-placeholder', $fieldset).parent(); $fieldsContainer.html(''); - + this.renderFields(fieldsToRender, $fieldsContainer); - + $fieldsetContainer.append($fieldset); } @@ -430,7 +430,7 @@ model = this.model, data = this.data, fields = this.fields; - + //Create form fields _.each(fieldsToRender, function(key) { //Get nested schema @@ -491,16 +491,16 @@ //Get errors from default Backbone model validator if (model && model.validate) { var modelErrors = model.validate(this.getValue()); - + if (modelErrors) { var isDictionary = _.isObject(modelErrors) && !_.isArray(modelErrors); - + //If errors are not in object form then just store on the error object if (!isDictionary) { errors._others = errors._others || []; errors._others.push(modelErrors); } - + //Merge programmatic errors (requires model.validate() to return an object e.g. { fieldKey: 'error' }) if (isDictionary) { _.each(modelErrors, function(val, key) { @@ -508,7 +508,7 @@ if (self.fields[key] && !errors[key]) { self.fields[key].setError(val); } - + else { //Otherwise add to '_others' key errors._others = errors._others || []; @@ -541,21 +541,21 @@ modelError = e; } }); - + if (modelError) return modelError; }, /** * Get all the field values as an object. * Use this method when passing data instead of objects - * + * * @param {String} To get a specific field value pass the key name */ getValue: function(key) { //Return only given key if specified if (key) return this.fields[key].getValue(); - - //Otherwise return entire form + + //Otherwise return entire form var values = {}; _.each(this.fields, function(field) { values[field.key] = field.getValue(); @@ -563,7 +563,7 @@ return values; }, - + /** * Update field values, referenced by key * @param {Object} New values to set @@ -579,7 +579,7 @@ */ remove: function() { var fields = this.fields; - + for (var key in fields) { fields[key].remove(); } @@ -593,7 +593,7 @@ //================================================================================================== //FIELD //================================================================================================== - + var Field = Backbone.View.extend({ /** @@ -619,7 +619,7 @@ return options.schema || {}; })(); - + //Set schema defaults if (!schema.type) schema.type = 'Text'; if (!schema.title) schema.title = helpers.keyToTitle(this.key); @@ -647,7 +647,7 @@ //Decide on the editor to use var editor = this.editor = helpers.createEditor(schema.type, options); - + //Create the element var $field = $(templates[schema.template]({ key: this.key, @@ -657,23 +657,23 @@ editor: '', help: '' })); - + //Render editor var $editorContainer = $('.bbf-placeholder-editor', $field).parent(); $editorContainer.empty(); $editorContainer.append(editor.render().el); - + //Set help text this.$help = $('.bbf-placeholder-help', $field).parent(); this.$help.empty(); if (this.schema.help) this.$help.html(this.schema.help); - + //Add custom CSS class names if (this.schema.fieldClass) $field.addClass(this.schema.fieldClass); - + //Add custom attributes if (this.schema.fieldAttrs) $field.attr(this.schema.fieldAttrs); - + this.setElement($field); return this; @@ -696,7 +696,7 @@ return id; }, - + /** * Check the validity of the field * @return {String} @@ -712,7 +712,7 @@ return error; }, - + /** * Set the field into an error state, adding the error class and setting the error message * @@ -721,26 +721,26 @@ setError: function(errMsg) { //Object and NestedModel types set their own errors internally if (this.editor.hasNestedForm) return; - + var errClass = Form.classNames.error; this.$el.addClass(errClass); - + if (this.$help) this.$help.html(errMsg); }, - + /** * Clear the error state and reset the help message */ clearError: function() { var errClass = Form.classNames.error; - + this.$el.removeClass(errClass); - + // some fields (e.g., Hidden), may not have a help el if (this.$help) { this.$help.empty(); - + //Reset help text if available var helpMsg = this.schema.help; if (helpMsg) this.$help.html(helpMsg); @@ -761,7 +761,7 @@ getValue: function() { return this.editor.getValue(); }, - + /** * Set/change the value of the editor */ @@ -771,7 +771,7 @@ logValue: function() { if (!console || !console.log) return; - + console.log(this.getValue()); }, @@ -819,18 +819,18 @@ else if (options.value) { this.value = options.value; } - + if (this.value === undefined) this.value = this.defaultValue; this.form = options.form; this.schema = options.schema || {}; this.validators = options.validators || this.schema.validators; - + if (this.key) this.$el.attr('name', this.key); - + //Add custom CSS class names if (this.schema.editorClass) this.$el.addClass(this.schema.editorClass); - + //Add custom attributes if (this.schema.editorAttrs) this.$el.attr(this.schema.editorAttrs); }, @@ -838,11 +838,11 @@ getValue: function() { throw 'Not implemented. Extend and override this method.'; }, - + setValue: function() { throw 'Not implemented. Extend and override this method.'; }, - + /** * Update the model with the current value * NOTE: The method is defined on the editors so that they can be used independently of fields @@ -852,20 +852,20 @@ commit: function() { var error = this.validate(); if (error) return error; - + this.model.set(this.key, this.getValue(), { error: function(model, e) { error = e; } }); - + if (error) return error; }, - + /** * Check validity * NOTE: The method is defined on the editors so that they can be used independently of fields - * + * * @return {String} */ validate: function() { @@ -892,17 +892,17 @@ editors.Text = editors.Base.extend({ tagName: 'input', - + defaultValue: '', - + initialize: function(options) { editors.Base.prototype.initialize.call(this, options); - + var schema = this.schema; - + //Allow customising text type (email, phone etc.) for HTML5 browsers var type = 'text'; - + if (schema && schema.editorAttrs && schema.editorAttrs.type) type = schema.editorAttrs.type; if (schema && schema.dataType) type = schema.dataType; @@ -925,12 +925,12 @@ getValue: function() { return this.$el.val(); }, - + /** * Sets the value of the form element * @param {String} */ - setValue: function(value) { + setValue: function(value) { this.$el.val(value); } @@ -961,7 +961,7 @@ onKeyPress: function(event) { //Allow backspace if (event.charCode == 0) return; - + //Get the whole new value so that we can prevent things like double decimals points etc. var newVal = this.$el.val() + String.fromCharCode(event.charCode); @@ -970,15 +970,15 @@ if (!numeric) event.preventDefault(); }, - getValue: function() { + getValue: function() { var value = this.$el.val(); - + return value === "" ? null : parseFloat(value, 10); }, - + setValue: function(value) { value = value === null ? null : parseFloat(value, 10); - + editors.Text.prototype.setValue.call(this, value); } @@ -1003,18 +1003,18 @@ tagName: 'textarea' }); - - + + //CHECKBOX editors.Checkbox = editors.Base.extend({ - + defaultValue: false, - + tagName: 'input', - + initialize: function(options) { editors.Base.prototype.initialize.call(this, options); - + this.$el.attr('type', 'checkbox'); }, @@ -1026,23 +1026,23 @@ return this; }, - + getValue: function() { return this.$el.attr('checked') ? true : false; }, - + setValue: function(value) { if (value) { this.$el.attr('checked', true); } } - + }); - - + + //HIDDEN editors.Hidden = editors.Base.extend({ - + defaultValue: '', initialize: function(options) { @@ -1050,11 +1050,11 @@ this.$el.attr('type', 'hidden'); }, - + getValue: function() { return this.value; }, - + setValue: function(value) { this.value = value; } @@ -1064,7 +1064,7 @@ /** * SELECT - * + * * Renders a