Skip to content

Instantly share code, notes, and snippets.

@zigotica
Forked from Serabe/components.close-button.js
Created October 4, 2016 10:09
Show Gist options
  • Select an option

  • Save zigotica/ebc9f115f2ce82ac91661aef4f717a6d to your computer and use it in GitHub Desktop.

Select an option

Save zigotica/ebc9f115f2ce82ac91661aef4f717a6d to your computer and use it in GitHub Desktop.

Revisions

  1. zigotica revised this gist Oct 4, 2016. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions templates.application.hbs
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,17 @@

    <!--
    <h2> A basic text component </h2>
    <div class="basic-text">
    {{text-component value=inputValue oninput=(action (mut inputValue)) onchange=(action (mut changeValue))}}<br>
    <p>
    InputValue: {{inputValue}}
    </p>
    <p>
    ChangeValue: {{changeValue}}
    </p>
    </div>
    -->
    <h2> A repeatable text field </h2>

    {{repeatable-field
  2. @Serabe Serabe revised this gist Oct 4, 2016. 1 changed file with 0 additions and 17 deletions.
    17 changes: 0 additions & 17 deletions templates.application.hbs
    Original file line number Diff line number Diff line change
    @@ -1,20 +1,3 @@
    <h1>Welcome to {{appName}}</h1>
    <br>
    <br>

    <!--
    <h2> A basic text component </h2>
    <div class="basic-text">
    {{text-component value=inputValue oninput=(action (mut inputValue)) onchange=(action (mut changeValue))}}<br>
    <p>
    InputValue: {{inputValue}}
    </p>
    <p>
    ChangeValue: {{changeValue}}
    </p>
    </div>
    -->
    <h2> A repeatable text field </h2>

    {{repeatable-field
  3. @Serabe Serabe revised this gist Oct 4, 2016. 5 changed files with 28 additions and 3 deletions.
    12 changes: 12 additions & 0 deletions components.close-button.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    import Ember from 'ember';

    export default Ember.Component.extend({
    tagName: 'button',
    click() {
    let action = this.get('action');

    if (typeof action === 'function') {
    action();
    }
    }
    });
    7 changes: 7 additions & 0 deletions helpers.arr.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    import Ember from 'ember';

    export function arr(params=[]/*, hash*/) {
    return params.slice(0);
    }

    export default Ember.Helper.helper(arr);
    1 change: 1 addition & 0 deletions templates.components.close-button.hbs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    X
    4 changes: 1 addition & 3 deletions templates.components.repeatable-field.hbs
    Original file line number Diff line number Diff line change
    @@ -4,10 +4,8 @@
    oninput=(action "onInput" idx)
    onchange=(action "onChange" idx)
    required=(is-main-field idx)
    afterComponents=(if (is-deletable comp.value idx) (arr (component "close-button" action=(action "removeRepetition" idx))) (arr))
    }} ({{comp.value}})
    {{#if (is-deletable comp.value idx)}}
    <button {{action "removeRepetition" idx}}>X</button>
    {{/if}}
    {{/each}}

    <h3> Values </h3>
    7 changes: 7 additions & 0 deletions templates.components.text-component.hbs
    Original file line number Diff line number Diff line change
    @@ -2,3 +2,10 @@
    oninput={{action "onInput" value="target.value"}}
    onchange={{action "onChange" value="target.value"}}
    required={{required}}>

    {{#if afterComponents}}
    |
    {{/if}}
    {{#each afterComponents as |comp|}}
    {{component comp}}
    {{/each}}
  4. @Serabe Serabe revised this gist Oct 3, 2016. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions components.repeatable-field.js
    Original file line number Diff line number Diff line change
    @@ -37,9 +37,6 @@ export default Ember.Component.extend({
    this.get('onchange')(this.get('components'), idx, value);

    this.addElementIfNeeded();
    if (idx === 0 && Ember.isEmpty(value)) {
    this.set('components', Ember.A([Ember.Object.create({value: '' })]));
    }
    }
    }
    });
  5. @Serabe Serabe revised this gist Oct 3, 2016. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions components.repeatable-field.js
    Original file line number Diff line number Diff line change
    @@ -37,6 +37,9 @@ export default Ember.Component.extend({
    this.get('onchange')(this.get('components'), idx, value);

    this.addElementIfNeeded();
    if (idx === 0 && Ember.isEmpty(value)) {
    this.set('components', Ember.A([Ember.Object.create({value: '' })]));
    }
    }
    }
    });
  6. @Serabe Serabe revised this gist Oct 3, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion templates.application.hbs
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@
    <br>
    <br>

    <!--
    <h2> A basic text component </h2>
    <div class="basic-text">
    {{text-component value=inputValue oninput=(action (mut inputValue)) onchange=(action (mut changeValue))}}<br>
    @@ -13,7 +14,7 @@ InputValue: {{inputValue}}
    ChangeValue: {{changeValue}}
    </p>
    </div>

    -->
    <h2> A repeatable text field </h2>

    {{repeatable-field
  7. @Serabe Serabe revised this gist Oct 3, 2016. 9 changed files with 52 additions and 19 deletions.
    15 changes: 10 additions & 5 deletions components.repeatable-field.js
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ export default Ember.Component.extend({
    init() {
    this._super(...arguments);

    this.set('components', Ember.A(['']));
    this.set('components', Ember.A([Ember.Object.create({value: ''})]));
    },

    didReceiveAttrs() {
    @@ -15,20 +15,25 @@ export default Ember.Component.extend({
    },

    addElementIfNeeded() {
    console.log(this.get('components.length'), this.get('maxRepeats'), this.get('components.lastObject'));
    console.log(this.get('components.length'), this.get('maxRepeats'), this.get('components.lastObject.value'));
    if (this.get('components.length') < this.get('maxRepeats') &&
    Ember.isPresent(this.get('components.lastObject'))) {
    this.get('components').pushObject('');
    Ember.isPresent(this.get('components.lastObject.value'))) {
    this.get('components').pushObject(Ember.Object.create({value:''}));
    }
    },

    actions: {
    removeRepetition(idx) {
    this.get('components').removeAt(idx, 1);
    this.addElementIfNeeded();
    },
    onInput(idx, value) {

    },
    onChange(idx, value) {
    this.get('components')
    .replace(idx, 1, [value]);
    .objectAt(idx)
    .set('value', value);
    this.get('onchange')(this.get('components'), idx, value);

    this.addElementIfNeeded();
    7 changes: 7 additions & 0 deletions helpers.is-deletable.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    import Ember from 'ember';

    export function isDeletable([value, idx]/*, hash*/) {
    return Ember.isPresent(value) && idx > 0;
    }

    export default Ember.Helper.helper(isDeletable);
    7 changes: 7 additions & 0 deletions helpers.is-extra-field.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    import Ember from 'ember';

    export function isExtraField([idx]/*, hash*/) {
    return idx > 0;
    }

    export default Ember.Helper.helper(isExtraField);
    7 changes: 7 additions & 0 deletions helpers.is-main-field.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    import Ember from 'ember';

    export function isMainField([idx]/*, hash*/) {
    return idx === 0;
    }

    export default Ember.Helper.helper(isMainField);
    9 changes: 9 additions & 0 deletions styles.app.css
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    body {
    margin: 12px 16px;
    font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
    font-size: 12pt;
    }

    input:required {
    border-color: red;
    }
    6 changes: 0 additions & 6 deletions templates.application.hbs
    Original file line number Diff line number Diff line change
    @@ -16,12 +16,6 @@ ChangeValue: {{changeValue}}

    <h2> A repeatable text field </h2>

    <h3> Config </h3>
    <p>
    Min repeats: {{text-component onchange=(action (mut minRepeats))}}<br>

    </p>

    {{repeatable-field
    field=(component "text-component")
    max-repeats=maxRepeats
    11 changes: 8 additions & 3 deletions templates.components.repeatable-field.hbs
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,19 @@
    <h3> Some fieldsets </h3>
    {{#each components as |comp idx|}}
    {{component field value=(readonly comp)
    {{component field value=(readonly comp.value)
    oninput=(action "onInput" idx)
    onchange=(action "onChange" idx)}} ({{comp}})
    onchange=(action "onChange" idx)
    required=(is-main-field idx)
    }} ({{comp.value}})
    {{#if (is-deletable comp.value idx)}}
    <button {{action "removeRepetition" idx}}>X</button>
    {{/if}}
    {{/each}}

    <h3> Values </h3>

    <ol>
    {{#each components as |comp idx|}}
    <li>{{comp}}</li>
    <li>{{comp.value}}</li>
    {{/each}}
    </ol>
    3 changes: 2 additions & 1 deletion templates.components.text-component.hbs
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    <input value={{value}}
    oninput={{action "onInput" value="target.value"}}
    onchange={{action "onChange" value="target.value"}}>
    onchange={{action "onChange" value="target.value"}}
    required={{required}}>
    6 changes: 2 additions & 4 deletions twiddle.json
    Original file line number Diff line number Diff line change
    @@ -9,10 +9,8 @@
    },
    "dependencies": {
    "jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
    "ember": "2.8.0",
    "ember-data": "2.8.0",
    "ember-template-compiler": "2.8.0",
    "ember-testing": "2.8.0"
    "ember": "2.9.0-beta.4",
    "ember-template-compiler": "2.9.0-beta.4"
    },
    "addons": {}
    }
  8. @Serabe Serabe created this gist Oct 3, 2016.
    37 changes: 37 additions & 0 deletions components.repeatable-field.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    import Ember from 'ember';

    export default Ember.Component.extend({
    init() {
    this._super(...arguments);

    this.set('components', Ember.A(['']));
    },

    didReceiveAttrs() {
    this._super(...arguments);
    this.set('maxRepeats', Math.max(1, parseInt(this.get('max-repeats'), 10)) || 5);

    this.set('minRepeats', Math.max(1, parseInt(this.get('min-repeats'), 10)) || 2);
    },

    addElementIfNeeded() {
    console.log(this.get('components.length'), this.get('maxRepeats'), this.get('components.lastObject'));
    if (this.get('components.length') < this.get('maxRepeats') &&
    Ember.isPresent(this.get('components.lastObject'))) {
    this.get('components').pushObject('');
    }
    },

    actions: {
    onInput(idx, value) {

    },
    onChange(idx, value) {
    this.get('components')
    .replace(idx, 1, [value]);
    this.get('onchange')(this.get('components'), idx, value);

    this.addElementIfNeeded();
    }
    }
    });
    16 changes: 16 additions & 0 deletions components.text-component.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    import Ember from 'ember';

    export default Ember.Component.extend({
    init() {
    this._super(...arguments);
    this.set('value', '');
    },
    actions: {
    onInput(value) {
    this.get('oninput')(value);
    },
    onChange(value) {
    this.get('onchange')(value);
    }
    }
    });
    8 changes: 8 additions & 0 deletions controllers.application.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    import Ember from 'ember';

    export default Ember.Controller.extend({
    appName: 'Ember Twiddle',
    actions: {
    repeatableFieldChanged() { }
    }
    });
    36 changes: 36 additions & 0 deletions templates.application.hbs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    <h1>Welcome to {{appName}}</h1>
    <br>
    <br>

    <h2> A basic text component </h2>
    <div class="basic-text">
    {{text-component value=inputValue oninput=(action (mut inputValue)) onchange=(action (mut changeValue))}}<br>

    <p>
    InputValue: {{inputValue}}
    </p>
    <p>
    ChangeValue: {{changeValue}}
    </p>
    </div>

    <h2> A repeatable text field </h2>

    <h3> Config </h3>
    <p>
    Min repeats: {{text-component onchange=(action (mut minRepeats))}}<br>

    </p>

    {{repeatable-field
    field=(component "text-component")
    max-repeats=maxRepeats
    min-repeats=minRepeats
    onchange=(action "repeatableFieldChanged")}}
    <div>

    </div>


    <br>
    <br>
    14 changes: 14 additions & 0 deletions templates.components.repeatable-field.hbs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    <h3> Some fieldsets </h3>
    {{#each components as |comp idx|}}
    {{component field value=(readonly comp)
    oninput=(action "onInput" idx)
    onchange=(action "onChange" idx)}} ({{comp}})
    {{/each}}

    <h3> Values </h3>

    <ol>
    {{#each components as |comp idx|}}
    <li>{{comp}}</li>
    {{/each}}
    </ol>
    3 changes: 3 additions & 0 deletions templates.components.text-component.hbs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    <input value={{value}}
    oninput={{action "onInput" value="target.value"}}
    onchange={{action "onChange" value="target.value"}}>
    18 changes: 18 additions & 0 deletions twiddle.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    {
    "version": "0.10.5",
    "EmberENV": {
    "FEATURES": {}
    },
    "options": {
    "use_pods": false,
    "enable-testing": false
    },
    "dependencies": {
    "jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
    "ember": "2.8.0",
    "ember-data": "2.8.0",
    "ember-template-compiler": "2.8.0",
    "ember-testing": "2.8.0"
    },
    "addons": {}
    }