Code Duplication    Length = 48-50 lines in 2 locations

src/Form/Field/Select.php 1 location

@@ 279-328 (lines=50) @@
276
     *
277
     * @return $this
278
     */
279
    public function ajax($url, $idField = 'id', $textField = 'text')
280
    {
281
        $configs = array_merge([
282
            'allowClear'         => true,
283
            'placeholder'        => $this->label,
284
            'minimumInputLength' => 1,
285
        ], $this->config);
286
287
        $configs = json_encode($configs);
288
        $configs = substr($configs, 1, strlen($configs) - 2);
289
290
        $this->script = <<<EOT
291
292
$("{$this->getElementClassSelector()}").select2({
293
  ajax: {
294
    url: "$url",
295
    dataType: 'json',
296
    delay: 250,
297
    data: function (params) {
298
      return {
299
        q: params.term,
300
        page: params.page
301
      };
302
    },
303
    processResults: function (data, params) {
304
      params.page = params.page || 1;
305
306
      return {
307
        results: $.map(data.data, function (d) {
308
                   d.id = d.$idField;
309
                   d.text = d.$textField;
310
                   return d;
311
                }),
312
        pagination: {
313
          more: data.next_page_url
314
        }
315
      };
316
    },
317
    cache: true
318
  },
319
  $configs,
320
  escapeMarkup: function (markup) {
321
      return markup;
322
  }
323
});
324
325
EOT;
326
327
        return $this;
328
    }
329
330
    /**
331
     * Set config for select2.

src/Grid/Filter/Presenter/Select.php 1 location

@@ 164-211 (lines=48) @@
161
     * @param $idField
162
     * @param $textField
163
     */
164
    public function ajax($resourceUrl, $idField = 'id', $textField = 'text')
165
    {
166
        $configs = array_merge([
167
            'allowClear'         => true,
168
            'placeholder'        => trans('admin.choose'),
169
            'minimumInputLength' => 1,
170
        ], $this->config);
171
172
        $configs = json_encode($configs);
173
        $configs = substr($configs, 1, strlen($configs) - 2);
174
175
        $this->script = <<<EOT
176
177
$(".{$this->getElementClass()}").select2({
178
  ajax: {
179
    url: "$resourceUrl",
180
    dataType: 'json',
181
    delay: 250,
182
    data: function (params) {
183
      return {
184
        q: params.term,
185
        page: params.page
186
      };
187
    },
188
    processResults: function (data, params) {
189
      params.page = params.page || 1;
190
191
      return {
192
        results: $.map(data.data, function (d) {
193
                   d.id = d.$idField;
194
                   d.text = d.$textField;
195
                   return d;
196
                }),
197
        pagination: {
198
          more: data.next_page_url
199
        }
200
      };
201
    },
202
    cache: true
203
  },
204
  $configs,
205
  escapeMarkup: function (markup) {
206
      return markup;
207
  }
208
});
209
210
EOT;
211
    }
212
213
    /**
214
     * @return array