").attr({ "id": "snippet-" + snippetList[i].snippetID }).appendTo(mainList);
- $.ajax({
- url: "snippet.php?snippetID=" + snippetList[i].snippetID,
- type: "GET",
- success: function (result) { addSnippet(result) },
- error: function (result) { displayError(result) }
- });
+ for (let snippet of snippetList) {
+ addSnippet(snippet);
}
}
function addSnippet(snippet) {
+ $("
").attr({ "id": "snippet-" + snippet.snippetID }).appendTo(mainList);
var code = $('
').attr("id", "snippet-" + snippet.snippetID + "-body").text(snippet.snippet);
buildCard("snippet", snippet.snippetID, snippet.snippetName, snippet.languages, snippet.snippetDescription, code);
}
@@ -476,8 +463,7 @@ function saveLang() {
var langID = $("#editLangID").val();
var langName = $("#txtEditLangName").val().trim();
- var data = {};
- data['langID'] = langID;
+ var data = { langID: langID };
if (changeName && langName == "") {
displayError({responseText: "Name cannot be empty"});
@@ -506,35 +492,10 @@ function saveLang() {
});
var langsToAdd = inANotInB(newLangsList, curLangsList);
+ manageAssocLang("lang", "PUT", langID, langsToAdd);
+
var langsToDelete = inANotInB(curLangsList, newLangsList);
-
- addLangsLang(langID, langName, langsToAdd, langsToDelete != 0);
- deleteLangsLang(langID, langName, langsToDelete);
- }
-}
-
-function addLangsLang(langID, langName, langList, forceUpdate) {
- for (var i = 0; i < langList.length; i++) {
- $.ajax({
- url: "lang.php",
- type: "PUT",
- data: {langID: langID, associatedLang: langList[i]},
- error: displayError
- });
- }
-
- if (forceUpdate || langList.length != 0)
- successAlert("Successfully saved language: " + langName, "lang")
-}
-
-function deleteLangsLang(langID, langName, langList) {
- for (var i = 0; i < langList.length; i++) {
- $.ajax({
- url: "lang.php",
- type: "DELETE",
- data: {langID: langID, associatedLang: langList[i]},
- error: displayError
- });
+ manageAssocLang("lang", "DELETE", langID, langsToDelete);
}
}
@@ -549,8 +510,7 @@ function saveSnippet() {
var snippetID = $("#editSnippetID").val();
var snippetName = $("#txtEditSnippetName").val().trim();
- var data = {};
- data['snippetID'] = snippetID;
+ var data = { snippetID: snippetID };
if (changeName && snippetName == "") {
displayError({responseText: "Name cannot be empty"});
@@ -580,32 +540,10 @@ function saveSnippet() {
});
var langsToAdd = inANotInB(newLangsList, curLangsList);
- addLangsSnippet(snippetID, langsToAdd);
+ manageAssocLang("snippet", "PUT", snippetID, langsToAdd);
var langsToDelete = inANotInB(curLangsList, newLangsList);
- deleteLangsSnippet(snippetID, langsToDelete);
- }
-}
-
-function addLangsSnippet(snippetID, langList) {
- for (var i = 0; i < langList.length; i++) {
- $.ajax({
- url: "snippet.php",
- type: "PUT",
- data: {snippetID: snippetID, langID: langList[i]},
- error: displayError
- });
- }
-}
-
-function deleteLangsSnippet(snippetID, langList) {
- for (var i = 0; i < langList.length; i++) {
- $.ajax({
- url: "snippet.php",
- type: "DELETE",
- data: {snippetID: snippetID, langID: langList[i]},
- error: displayError
- });
+ manageAssocLang("snippet", "DELETE", snippetID, langsToDelete);
}
}
@@ -620,8 +558,7 @@ function saveResource() {
var resourceID = $("#editResourceID").val();
var resourceName = $("#txtEditResourceName").val().trim();
- var data = {};
- data['resourceID'] = resourceID;
+ var data = { resourceID: resourceID };
if (changeName && resourceName == "") {
displayError({responseText: "Name cannot be empty"});
@@ -650,30 +587,21 @@ function saveResource() {
});
var langsToAdd = inANotInB(newLangsList, curLangsList);
- addLangsResource(resourceID, langsToAdd);
+ manageAssocLang("resource", "PUT", resourceID, langsToAdd);
var langsToDelete = inANotInB(curLangsList, newLangsList);
- deleteLangsResource(resourceID, langsToDelete);
+ manageAssocLang("resource", "DELETE", resourceID, langsToDelete);
}
}
-function addLangsResource(resourceID, langList) {
- for (var i = 0; i < langList.length; i++) {
+function manageAssocLang(type, action, id, langList) {
+ for (let item of langList) {
+ var data = { langID: item };
+ data[type + "ID"] = id;
$.ajax({
- url: "resource.php",
- type: "PUT",
- data: {resourceID: resourceID, langID: langList[i]},
- error: displayError
- });
- }
-}
-
-function deleteLangsResource(resourceID, langList) {
- for (var i = 0; i < langList.length; i++) {
- $.ajax({
- url: "resource.php",
- type: "DELETE",
- data: {resourceID: resourceID, langID: langList[i]},
+ url: type + ".php",
+ type: action,
+ data: data,
error: displayError
});
}
@@ -731,9 +659,9 @@ function displayError(error, str) {
function inANotInB(a, b) {
var list = [];
- for (var i = 0; i < a.length; i++) {
- if (b.indexOf(a[i]) == -1)
- list.push(a[i]);
+ for (let item of a) {
+ if (b.indexOf(item) == -1)
+ list.push(item);
}
return list;
}