<commit_before>/*
---
 
script: Submittable.js
 
description: Makes widget result in either submission or cancellation
 
license: Public domain (http://unlicense.org).

authors: <NAME>
 
requires:
  - LSD.Mixin
 
provides:
  - LSD.Mixin.Submittable
 
...
*/


LSD.Mixin.Submittable = new Class({
  options: {
    actions: {
      autosubmission: {
        enable: function() {
          if (this.attributes.autosubmit) this.submit();
        }
      }
    },
    has: {
      many: {
        submitters: {
          selector: ':submitter',
          as: 'submittable',
          scope: {
            'default': {
              filter: '[default]'
            }
          },
          options: {
            chain: {
              submission: function() {
                return {action: 'submit', target: this.submittable};
              }
            }
          },
          callbacks: {
            fill: function() {
              if (LSD.toLowerCase(this.element.tagName) == 'form')
                this.properties.watch('rendered', this.bind(LSD.Mixin.Submittable.watchNativeSubmission));
            },
            empty: function() {
              if (LSD.toLowerCase(this.element.tagName) == 'form')
                this.properties.unwatch('rendered', this.bind(LSD.Mixin.Submittable.watchNativeSubmission));
            }
          }
        }
      }
    },
    events: {
      _form: {
        attach: function(element) {
          if (LSD.toLowerCase(element.tagName) == 'form') element.addEvent('submit', this.bind('submit'));
        },
        detach: function(element) {
          if (LSD.toLowerCase(element.tagName) == 'form') element.removeEvent('submit', this.bind('submit'));
        }
      }
    }
  },
  
  submit: function(event) {
    if (event && event.type == 'submit' && event.target == this.element) event.preventDefault();
    if (this.submitting) return true;
    this.submitting = true;
    this.fireEvent('beforeSubmit', arguments);
    var submission = this.captureEvent('submit', arguments);
    if (submission) return submission;
    else if (submission !== false) this.callChain();
    this.fireEvent('afterSubmit', arguments);
    delete this.submitting;
    return this;
  },
  
  cancel: function() {
    var submission = this.captureEvent('cancel', arguments);
    if (submission) return submission;
    else if (submission !== false) {
      var target = this.getSubmissionTarget();
      if (target) target.uncallChain();
      this.uncallChain();
    };
    return this;
  }
});

/*
  Injects native submit button at top of the form that gets activated 
  when `enter` button is pressed in the form field. It stops the 
  native submission, and submits the widget form instead. 
  
  The first submitter widget in the form is considered activated and
  its value is used for submission data.
*/
LSD.Mixin.Submittable.watchNativeSubmission = function(state) {
  if (state) {
    this.allocate('submit').inject(this.element, 'top').addEvent('click', this.bind('submit'))
  } else {
    this.release('submit').dispose().removeEvent('click', this.bind('submit'));
  }
  /* 
    novalidate html attribute disables internal form validation 
    on form submission. Chrome and Safari will block form 
    submission without any visual clues otherwise.
  */
  this.element[state ? 'setAttribute' : 'removeAttribute']('novalidate', '');
};

LSD.Behavior.define(':submittable', 'submittable');<commit_msg>Make submission prevent default 'click' event when a clicked target is <input type=submit>. Thus, a native submission is never triggered<commit_after>/*
---
 
script: Submittable.js
 
description: Makes widget result in either submission or cancellation
 
license: Public domain (http://unlicense.org).

authors: <NAME>
 
requires:
  - LSD.Mixin
 
provides:
  - LSD.Mixin.Submittable
 
...
*/


LSD.Mixin.Submittable = new Class({
  options: {
    actions: {
      autosubmission: {
        enable: function() {
          if (this.attributes.autosubmit) this.submit();
        }
      }
    },
    has: {
      many: {
        submitters: {
          selector: ':submitter',
          as: 'submittable',
          scope: {
            'default': {
              filter: '[default]'
            }
          },
          options: {
            chain: {
              submission: function() {
                return {action: 'submit', target: this.submittable};
              }
            }
          },
          callbacks: {
            fill: function() {
              if (LSD.toLowerCase(this.element.tagName) == 'form')
                this.properties.watch('rendered', this.bind(LSD.Mixin.Submittable.watchNativeSubmission));
            },
            empty: function() {
              if (LSD.toLowerCase(this.element.tagName) == 'form')
                this.properties.unwatch('rendered', this.bind(LSD.Mixin.Submittable.watchNativeSubmission));
            }
          }
        }
      }
    },
    events: {
      _form: {
        attach: function(element) {
          if (LSD.toLowerCase(element.tagName) == 'form') element.addEvent('submit', this.bind('submit'));
        },
        detach: function(element) {
          if (LSD.toLowerCase(element.tagName) == 'form') element.removeEvent('submit', this.bind('submit'));
        }
      }
    }
  },
  
  submit: function(event) {
    this.fireEvent('beforeSubmit', arguments);
    if (event) {
      switch (event.type) {
        case "submit":
          if (event.target == this.element) event.preventDefault();
          break;
        case "click":
          if (LSD.toLowerCase(event.target.tagName) == 'input' && event.target.type == 'submit')
            event.preventDefault();
      }
    }
    var submission = this.captureEvent('submit', arguments);
    if (!submission && submission !== false) this.callChain();
    this.fireEvent('afterSubmit', arguments);
    return submission;
  },
  
  cancel: function() {
    var submission = this.captureEvent('cancel', arguments);
    if (!submission && submission !== false) {
      var target = this.getSubmissionTarget();
      if (target) target.uncallChain();
      this.uncallChain();
    };
    return submission;
  }
});

/*
  Injects native submit button at top of the form that gets activated 
  when `enter` button is pressed in the form field. It stops the 
  native submission, and submits the widget form instead. 
  
  The first submitter widget in the form is considered activated and
  its value is used for submission data.
*/
LSD.Mixin.Submittable.watchNativeSubmission = function(state) {
  if (state) {
    this.allocate('submit').inject(this.element, 'top').addEvent('click', this.bind('submit'))
  } else {
    this.release('submit').dispose().removeEvent('click', this.bind('submit'));
  }
  /* 
    novalidate html attribute disables internal form validation 
    on form submission. Chrome and Safari will block form 
    submission without any visual clues otherwise.
  */
  this.element[state ? 'setAttribute' : 'removeAttribute']('novalidate', '');
};

LSD.Behavior.define(':submittable', 'submittable');
{'fraction_non_alphanumeric': 0.1325811001410437, 'fraction_numerical': 0.0, 'mean_word_length': 2.368646080760095, 'pattern_counts': {'":': 2, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '91770', 'n_tokens_mistral': 2041, 'n_tokens_neox': 2012, 'n_words': 474}