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

2017-chu-kmgs4524 #3

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 53 additions & 3 deletions entry.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,65 @@
import $ from 'jquery';
import getForm from "./lib/form.data.js";
import insertData from "./lib/insertData.js";
import deleteData from "./lib/deleteData.js";
import updateData from "./lib/updateData.js";
import getTodo from "./lib/todo.data.js";
import submitUpdate from "./lib/submitUpdate.js";
import cancel from "./lib/cancel.js";

let form = $("form");
let todo = $("ul.todo"); //Todo List

form.on('submit', function (e) {

form.on('submit', function (e) {
//console.log('form.on: ', $(this));
e.preventDefault();
var value = getForm(e);
insertData(value);
alert(`Add item: ${value}`);
if(value === undefined){
alert("請輸入內容");
} else{
insertData(value);
alert(`Add item: ${value}`);
console.log("submit");
}
});

//監聽Delete button
todo.on('click', ".btnDel",function(e) { //e: EventObject
//$(this).parent().remove();
deleteData($(this));
console.log($(this));
});

//監聽Update button
todo.on('click', '.btnUpd', function(e) {
updateData($(this));
console.log("onClick:", $(this));
});

//監聽按下Updata後出現的button Decide
todo.on('click', '.btnDeci', function(e) {
e.preventDefault();
console.log("onClick: ", $(this));

var value = getTodo(e);
console.log('getTodo: ', value);
if(value === undefined){
alert("請輸入");
} else{
submitUpdate(e, value);
}
//console.log($(this));


});

//監聽按下Updata後出現的Cancel button
todo.on('click', '.btnCancel', function(e){
console.log("onClick: ", $(this));
cancel($(this));
});




2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<h1>This is my todo</h1>
<form action="#">
<input type="text" name='item' value="hello">
<input type="submit" value="submit">
<input type="submit" value="submit">
</form>

<h2>My todo list</h2>
Expand Down
7 changes: 7 additions & 0 deletions lib/cancel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var $ = require('jquery');

module.exports = function(target, value){
console.log('cancel() receive target:', target, value);
// ($(target).parent())[0].innerHTML = `<li>${data}<button class="btnDel">Delete</button>
// <button class="btnUpd">Update</button></li>`;
};
8 changes: 8 additions & 0 deletions lib/deleteData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var $ = require('jquery');

module.exports = function(target){
//$(this).parent().remove();
target.parent().remove();
console.log("deleteData");
return true;
};
3 changes: 2 additions & 1 deletion lib/form.data.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import $ from 'jquery';

module.exports = function (e) {
var target = e.currentTarget;
console.log(`currentTarget:${target}`);
var inputNode = $(target).find('input[name=item]');
var inputValue = inputNode.val();
inputNode.val('').focus();
inputNode.val('').focus(); //?
return inputValue;
};
3 changes: 2 additions & 1 deletion lib/insertData.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var $ = require('jquery');

module.exports = function (data) {
$("ul.todo").append(`<li>${data}</li>`);
$("ul.todo").append(`<li>${data}<button class="btnDel">Delete</button>
<button class="btnUpd">Update</button></li>`);
return true;
}
9 changes: 9 additions & 0 deletions lib/submitUpdate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var $ = require('jquery');

module.exports = function(e, value) {
var target = e.currentTarget;

//console.log("submitUpdate() receive target.parent, value:", $(target).parent()[0].innerHTML, value);
$(target).parent()[0].innerHTML = `${value}<button class="btnDel">Delete</button>
<button class="btnUpd">Update</button>`;
};
14 changes: 14 additions & 0 deletions lib/todo.data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import $ from 'jquery';

module.exports = function (e) {
var target = e.currentTarget;
console.log(`currentTarget:${target}`); //currentTarget === this
var inputValue = $(target).parent().find('input').val();
//inputNode.val('').focus();
//console.log("inputValue:", inputValue);
return inputValue;
// var inputNode = $(target).find('input[name=item]');
// var inputValue = inputNode.val();
// inputNode.val('').focus(); //?
// return inputValue;
};
16 changes: 16 additions & 0 deletions lib/updateData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var $ = require('jquery');

module.exports = function(target){
// var text = target.parent().text();
// console.log(text);

// $(this).parent().html() = `Changed<button class="btnDel">Delete</button>
// <button class="btnUpd">Update</button>`;

(target.parent())[0].innerHTML = `<input type="text" name="item">
<button class="btnCancel">Cancel</button>
<button class="btnDeci">Decide</button>`;
// ($(this).parent())[0].innerHTML = `<input type="text"><button class="btnCncel">Cancel</button>
// <button class="btnSubmit">Submit</button>`;

};