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

Js mvc #93

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
172 changes: 4 additions & 168 deletions app/View/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,179 +6,15 @@
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.8.18/jquery-ui.min.js"></script>
<script type="text/javascript" src="../js/create.js"></script>

<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.19/themes/cupertino/jquery-ui.css">

<link rel="stylesheet" type="text/css" href="../CSS/style.css">
<script>
$(document).ready(function() {
$.ajax({

type: 'GET',
url: '/notes/api/userTag',
data: 'id=testdata',
datatype: "JSON",
cache: false,

success: function(result) {
console.log(result);

var userTagsFromServer = JSON.parse(result);
console.log(userTagsFromServer);

setTags(userTagsFromServer);
},
error: function(result) {
var errorMsg = JSON.parse(result);
alert("Message: " + errorMsg);
}
});
});

function addTag() {
if ((document.getElementById("txtTags").value).length == 0) {
alert("The textbox should not be empty");
return;
}
var txtTagsIdValue = document.getElementById("txtTags").value;
var userTag = {
"id": null,
"userId": "null",
"tag": txtTagsIdValue,
"isDeleted": "0"
};
createNoteTagsList(userTag);
document.getElementById("txtTags").value = "";
}
var userTagsArray = [];

function setTags(userTagsFromServer) {
var userTags = document.getElementById('user-tags');
for (var i = 0; i < userTagsFromServer.length; i++) {
//create and append tag to tag list

var li = document.createElement("LI");

li.setAttribute("class", "inputTags");
li.appendChild(document.createTextNode(userTagsFromServer[i].tag));
li.userTagsFromServer = userTagsFromServer[i];

li.addEventListener("click", function() {
console.log(this.userTagsFromServer);
var userTag = this.userTagsFromServer;

createNoteTagsList(userTag);

}, false);
userTags.appendChild(li);
}
}

function createNoteTagsList(userTag) {
var noteTags = document.getElementById('note-tags');
var liClickTag = document.createElement("LI");

var div = document.createElement("span");
div.appendChild(document.createTextNode(userTag.tag));

liClickTag.appendChild(div);

liClickTag.userTag = userTag;

var appendDelete = appendDeleteLink(userTag);

liClickTag.appendChild(appendDelete);
noteTags.appendChild(liClickTag);

userTagsArray.push(liClickTag.userTag);

$("a[id^='del']").live("click", null, function(e) {
var li = $(this).parent();
var tag = li[0].childNodes[0].innerText;

console.log(tag);
console.log(userTagsArray);
for (var i = 0; i < userTagsArray.length; i++) {
if (tag == userTagsArray[i].tag)
userTagsArray.splice(i, 1);
passUserTagsAndCreateNoteTags(userTagsArray);
}
li.remove();
});

passUserTagsAndCreateNoteTags(userTagsArray);

}

function appendDeleteLink(userTag) {
var x = document.createElement("A");
var t = document.createTextNode("Delete");

x.setAttribute("href", "#");
x.setAttribute("id", "del");
x.setAttribute("value", JSON.stringify(userTag));
x.appendChild(t);
return x;
}

function passUserTagsAndCreateNoteTags(userTagsArray) {
var userId = getUserId();
console.log(userId);

var noteTags = [];
for (var i = 0; i < userTagsArray.length; i++) {
var userTag = userTagsArray[i];
userTag.userId = userId;
noteTags[i] = {
"id": noteTagId,
"noteId": noteId,
"userTagId": userTagId,
"isDeleted": isDeleted,
"userTag": userTag
};
}
console.log(noteTags);

passNoteTagsAndCreateNoteModel(noteTags);
}

function passNoteTagsAndCreateNoteModel(noteTags) {
var title = document.getElementById("title").value;
var body = document.getElementById("body").value;

var noteTagId = document.getElementById("noteTagId").value;
var noteId = document.getElementById("noteId").value;
var userTagId = document.getElementById("userTagId").value;
var isDeleted = document.getElementById("isDeleted").value;

var json = {
"title": title,
"body": body,
"noteTags": noteTags
};
document.getElementById("noteModel").value = JSON.stringify(json);
}

function getUserId() {
var allcookies = document.cookie;
console.log(allcookies);

// Get all the cookies pairs in an array
cookiearray = allcookies.split(';');
console.log(cookiearray);

// Now take key value pair out of this array
for (var i = 0; i < cookiearray.length; i++) {
name = cookiearray[i].split('=')[0].trim();
value = cookiearray[i].split('=')[1].trim();
if (name == "userId")
return value;
}
}
</script>
<link rel="stylesheet" type="text/css" href="../css/style.css">
</head>

<body>
<h3><p id="output"></p></h3>
<form method="post">
<div id="comment_form" class="container">
<div>Create Note :</div>
Expand All @@ -193,7 +29,7 @@ function getUserId() {
<div id="divTest">
<ul id="user-tags">
<input type="text" id="txtTags" />
<span><button type="button" onclick="addTag()">Add</button></span>
<span><button type="button" id="addBtnClk">Add</button></span>
</ul>
</div>
<div id="note-tags">
Expand Down
2 changes: 1 addition & 1 deletion public/CSS/style.css → public/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@
background-color: #2175bc;
color: #fff;
text-decoration: none;
}
}
196 changes: 196 additions & 0 deletions public/js/create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
var UserTagModel = function() {};

var UserTagView = function() {};

var NoteTagView = function() {};

var DeleteLinkView = function() {};

var UserTagController = function() {};

var NoteTagController = function() {};

function passUserTagsAndCreateNoteTags(userTagsArray) {
var userId = getUserId();
console.log(userId);

var noteTags = [];
for (var i = 0; i < userTagsArray.length; i++) {
var userTag = userTagsArray[i];
userTag.userId = userId;
noteTags[i] = {
"id": noteTagId,
"noteId": noteId,
"userTagId": userTagId,
"isDeleted": isDeleted,
"userTag": userTag
};
}
console.log(noteTags);

passNoteTagsAndCreateNoteModel(noteTags);
}

function passNoteTagsAndCreateNoteModel(noteTags) {
var title = document.getElementById("title").value;
var body = document.getElementById("body").value;

var noteTagId = document.getElementById("noteTagId").value;
var noteId = document.getElementById("noteId").value;
var userTagId = document.getElementById("userTagId").value;
var isDeleted = document.getElementById("isDeleted").value;

var json = {
"title": title,
"body": body,
"noteTags": noteTags
};
document.getElementById("noteModel").value = JSON.stringify(json);
}

function getUserId() {
var allcookies = document.cookie;
console.log(allcookies);

// Get all the cookies pairs in an array
cookiearray = allcookies.split(';');
console.log(cookiearray);

// Now take key value pair out of this array
for (var i = 0; i < cookiearray.length; i++) {
name = cookiearray[i].split('=')[0].trim();
value = cookiearray[i].split('=')[1].trim();
if (name == "userId")
return value;
}
}
NoteTagView.addTag = function(txtIdValue) {
if ((txtIdValue).length == 0) {
alert("The textbox should not be empty");
return;
};

var userTag = {
"id": null,
"userId": "null",
"tag": txtIdValue,
"isDeleted": "0"
};

NoteTagView.prototype.createNoteTags(userTag);

document.getElementById("txtTags").value = "";
}

DeleteLinkView.prototype.createDeleteLink = function(userTag) {
var x = document.createElement("A");
var t = document.createTextNode("Delete");

x.setAttribute("href", "#");
x.setAttribute("id", "del");
x.setAttribute("value", JSON.stringify(userTag));
x.appendChild(t);
return x;
};
var userTagsArray = [];
NoteTagView.prototype.createNoteTags = function(userTag) {
var noteTags = document.getElementById('note-tags');
var liClickTag = document.createElement("LI");

var div = document.createElement("span");
div.appendChild(document.createTextNode(userTag.tag));

liClickTag.appendChild(div);

liClickTag.userTag = userTag;

var appendDelete = DeleteLinkView.prototype.createDeleteLink(userTag);

liClickTag.appendChild(appendDelete);
noteTags.appendChild(liClickTag);

userTagsArray.push(liClickTag.userTag);

$("a[id^='del']").live("click", null, function(e) {
var li = $(this).parent();
var tag = li[0].childNodes[0].innerText;

console.log(tag);
console.log(userTagsArray);
for (var i = 0; i < userTagsArray.length; i++) {
if (tag == userTagsArray[i].tag)
userTagsArray.splice(i, 1);
passUserTagsAndCreateNoteTags(userTagsArray);
}
li.remove();
});
passUserTagsAndCreateNoteTags(userTagsArray);
};

UserTagView.prototype.setTags = function(userTagsFromServer) {
var userTags = document.getElementById('user-tags');
for (var i = 0; i < userTagsFromServer.length; i++) {
//create and append tag to tag list

var li = document.createElement("LI");

li.setAttribute("class", "inputTags");
li.appendChild(document.createTextNode(userTagsFromServer[i].tag));
li.userTagsFromServer = userTagsFromServer[i];

li.addEventListener("click", function() {
console.log(this.userTagsFromServer);
var userTag = this.userTagsFromServer;

NoteTagView.prototype.createNoteTags(userTag);

}, false);
userTags.appendChild(li);
}
};

UserTagModel.search = function() {
$.ajax({
type: 'GET',
url: '/notes/api/userTag',
data: 'id=testdata',
datatype: "JSON",
cache: false,

success: function(result) {
console.log(result);
var userTagsFromServer = JSON.parse(result);

UserTagView.prototype.setTags(userTagsFromServer);

},
error: function(result) {
var errorMsg = JSON.parse(result);
alert("Message: " + errorMsg);
}
});
};
NoteTagController.prototype.loadView = function(txtIdValue) {
var noteTagView = NoteTagView.addTag(txtIdValue);
console.log(noteTagView);

};
UserTagController.prototype.loadModel = function() {
var userTagModel = UserTagModel.search();
console.log(userTagModel);

};
$(document).ready(function() {

var userTagController = new UserTagController;
userTagController.loadModel();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use meaningful names.


var noteTagController = new NoteTagController;

var addBtnClk = document.getElementById("addBtnClk");
addBtnClk.addEventListener('click', function() {
var txtIdValue = document.getElementById("txtTags").value;
noteTagController.loadView(txtIdValue)
});

});