Permalink
Browse files

Add prefill to new time entry form

Prefilled values are based on the most recent prior entry and
populated via plain JS function.
  • Loading branch information...
lovett committed Aug 3, 2016
1 parent 00b5ab6 commit 51db7eb802fe23a4d9e90f7a711b8ef4292924df
Showing with 70 additions and 11 deletions.
  1. +8 −10 .dir-locals.el
  2. +2 −1 .eslintrc.js
  3. +9 −0 app/Http/Controllers/TimeController.php
  4. +38 −0 public/js/prefill.js
  5. +13 −0 resources/views/time/form.blade.php
View
@@ -1,7 +1,7 @@
;;; Directory Local Variables
;;; For more information see (info "(emacs) Directory Variables")
-((php-mode
+((nil
(eval . (progn
(set (make-local-variable 'project-root)
(file-name-directory
@@ -10,12 +10,10 @@
(customize-set-variable 'flycheck-php-phpcs-executable (expand-file-name "vendor/bin/phpcs" project-root))
(customize-set-variable 'flycheck-phpcs-standard (expand-file-name "phpcs.xml" project-root))
(customize-set-variable 'flycheck-php-phpmd-executable (expand-file-name "vendor/bin/phpmd" project-root))
- (customize-set-variable 'flycheck-phpmd-rulesets (expand-file-name "phpmd.xml" project-root))))))
-
-((js-mode
- (eval . (progn
- (set (make-local-variable 'project-root)
- (file-name-directory
- (let ((d (dir-locals-find-file ".")))
- (if (stringp d) d (car d)))))
- (customize-set-variable 'flycheck-javascript-eslint-executable (expand-file-name "node_modules/.bin/eslint" project-root))))))
+ (customize-set-variable 'flycheck-phpmd-rulesets (expand-file-name "phpmd.xml" project-root))
+ (customize-set-variable 'flycheck-javascript-eslint-executable (expand-file-name "node_modules/.bin/eslint" project-root))
+ (add-to-list 'flycheck-disabled-checkers 'javascript-jshint)
+ (add-to-list 'flycheck-disabled-checkers 'javascript-gjslint)
+ (add-to-list 'flycheck-disabled-checkers 'javascript-jscs)
+ (add-to-list 'flycheck-disabled-checkers 'javascript-standard)
+ ))))
View
@@ -8,6 +8,7 @@ module.exports = {
"rules": {
"indent": ["error", 4],
"semi": ["error", "always"],
- "no-new": "off"
+ "no-new": "off",
+ "no-unused-vars": [2, {"vars": "local", "args": "after-used"}]
}
};
@@ -127,9 +127,18 @@ public function create(Request $request)
]
);
+ $previousModel = $request->user()->time();
+ if ($projectId !== null) {
+ $previousModel->where('project_id', $projectId);
+ }
+
+ $previousModel->orderBy('start', 'DESC');
+ $previousModel->limit(1);
+
$viewVars = [
'page_title' => 'Add time',
'model' => $model,
+ 'previousModel' => $previousModel->first(),
'submission_route' => 'time.store',
'submission_method' => 'POST',
'projects' => $projects,
View
@@ -0,0 +1,38 @@
+/* eslint-disable no-unused-vars */
+function prefill (e) {
+ 'use strict';
+
+ var i, changeEvent, id, trigger, target;
+
+ trigger = e.trigger || e.srcElement;
+
+ if (trigger.hasAttributes() === false) {
+ return;
+ }
+
+ for (i = 0; i < trigger.attributes.length; i++) {
+ if (trigger.attributes[i].name.indexOf('data-') !== 0) {
+ continue;
+ }
+
+ id = trigger.attributes[i].name.replace('data-', '');
+
+ target = document.getElementById(id);
+
+ if (!target) {
+ continue;
+ }
+
+ if (target.nodeName === 'TEXTAREA') {
+ target.innerHTML = trigger.attributes[i].value;
+ continue;
+ }
+
+ target.value = trigger.attributes[i].value;
+
+ if (target.nodeName === 'SELECT') {
+ changeEvent = new window.Event('change');
+ target.dispatchEvent(changeEvent);
+ }
+ }
+}
@@ -6,6 +6,18 @@
{!! Form::model($model, ['route' => $submission_route, 'method' => $submission_method, 'class' => 'form-horizontal']) !!}
+@if (isset($previousModel))
+<div class="well well-sm text-center">
+ <a
+ onclick="prefill(event)"
+ href="#"
+ data-project_id="{{ $previousModel->project_id }}"
+ data-estimate="{{ $previousModel->estimatedDuration }}"
+ data-summary="{{ $previousModel->summary}}"
+ >Prefill from previous entry</a>
+</div>
+@endif
+
@include('partials.formgroup-menu', ['name' => 'project_id', 'label' => 'Project', 'items' => $projects, 'selectedItem' => $model->project_id])
@include('partials.formgroup-standard', ['name' => 'estimatedDuration', 'label' => 'Estimate'])
@@ -45,4 +57,5 @@
@section('page_scripts')
<script src="{{ asset('js/vue.min.js') }}"></script>
<script src="{{ asset('js/pickable.js') }}"></script>
+<script src="{{ asset('js/prefill.js') }}"></script>
@endsection

0 comments on commit 51db7eb

Please sign in to comment.