Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to disable event #16

Open
wize-wiz opened this issue Oct 10, 2019 · 3 comments
Open

Add method to disable event #16

wize-wiz opened this issue Oct 10, 2019 · 3 comments

Comments

@wize-wiz
Copy link
Contributor

wize-wiz commented Oct 10, 2019

Like in #9, I too was searching for a solution to disable the event when editing a resource.

E.g. I have a news resource where the title generates a slug, but on edit this slug should not be changed. Now I could simply do an if/else if the request is an update or not, and just replace the Slug field with a disabled text field, but easier would be if I could just add a method like disableEvents($disable = true) with a default set to true.

I don't know what you (@drobee) think about this idea, but I did a quick edit and came with this solution.

src/Slug.php

// constructor
public function __construct(...) {
  ...   
  $this->disableEvents(false); // default to false
  ...
}

// method
public function disableEvents(bool $disable = true) {
  // I choose `meta` because somehow setOptions() sets a boolean to string "1" instead of `true`.
  return $this->withMeta(['disableEvents' => $disable]);
}

resources/js/components/Slug/FormField.vue

Changed the mounted method for a return if event should not be used.

    mounted() {
        // return if event should be disabled
        if(this.field.disableEvents) {
            return;
        }
        const eventType = this.field.options.event || 'keyup';
        Nova.$on('field-update-' + eventType + '-' + this.field.name, ({value}) => {
          this.generateSlug(value);
    },

I could make PR if you agree.


Another thing, I also added a delay for the generate slug request.

src/Slug.php

// added eventDelay to default options
protected $options = [
    'event' => 'keyup',
    'eventDelay' => 200
];

// method
public function eventDelay(int $delay) {
  return $this->setOption('eventDelay', $delay);
}

Mounted function with delay

// return if event disabled
if(this.field.disableEvents) {
    return;
}
this.eventDelayTimeout = null;
const eventType = this.field.options.event || 'keyup';
Nova.$on('field-update-' + eventType + '-' + this.field.name, ({value}) => {
    // clear timeout if any
    if(this.eventDelayTimeout !== null) { clearTimeout(this.eventDelayTimeout); }
    // delay generation of slug
    this.eventDelayTimeout = setTimeout((function() {
        this.generateSlug(value)
    }).bind(this), this.options.eventDelay);
})
@bernhardh
Copy link

In the meanwhile, you could do it the dirty way - see #9 (comment)

@drobee
Copy link
Owner

drobee commented Mar 26, 2020

@wize-wiz I'm sorry I've been neglecting this repo. I like your idea, it's simple yet effective. Would you still like to make a PR with this? I'd be grateful!

@wize-wiz
Copy link
Contributor Author

wize-wiz commented Apr 15, 2020

@wize-wiz I'm sorry I've been neglecting this repo. I like your idea, it's simple yet effective. Would you still like to make a PR with this? I'd be grateful!

@drobee I'll do 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants