Skip to content

Commit

Permalink
maintaining widget index change on the server
Browse files Browse the repository at this point in the history
  • Loading branch information
kreenamehta committed Nov 7, 2016
1 parent d1003f5 commit e1104ee
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
33 changes: 31 additions & 2 deletions assignment/services/widget.service.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ module.exports = function (app) {
res.send(400);
}

/**
* uploads an image
* @param req
* @param res
*/
function uploadImage(req, res) {


Expand All @@ -133,17 +138,41 @@ module.exports = function (app) {
widgets[w].name = originalname;
widgets[w].width = width;
widgets[w].url = '/assignment/uploads/'+filename;
// res.send(widgets[w]);
res.redirect("/assignment/index.html#/user/"+userId+"/website/"+websiteId+"/page/"+pageId+"/widget/"+widgetId);
return;
}
}
res.redirect("/assignment/index.html#/user/"+userId+"/website/"+websiteId+"/page/"+pageId+"/widget/"+widgetId);
}


/**
* updates the index of the widget in the array to maintain the order of widgets
* while sorting them
* @param req
* @param res
*/
function updateWidgetIndices(req, res) {
var initial = req.query.initial;
var final = req.query.final;
widgets.splice(final, 0, widgets.splice(initial, 1)[0]);
var pageId = req.params.pageId;

var realInitial = -1;
var realFinal = -1;
var loopInCurrentPage = -1;

for(var w in widgets){
if(widgets[w].pageId === pageId){
loopInCurrentPage++;
if(loopInCurrentPage === parseInt(initial)){
realInitial = w;
}else if(loopInCurrentPage === parseInt(final)){
realFinal = w;
}
}
}

widgets.splice(realFinal, 0, widgets.splice(realInitial, 1)[0]);
res.send(200);
}
};
24 changes: 21 additions & 3 deletions public/assignment/directives/jga-directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
angular
.module("jgaDirectives", [])
.directive("jgaSortable", jgaSortable); // .jga-sortable


/**
* sorts the widgets
* @returns {{scope: {}, link: linker, controller: sortableController, controllerAs: string}}
*/
function jgaSortable() {

function linker(scope, element, attributes) {
Expand All @@ -31,12 +35,26 @@
}
}

function sortableController(WidgetService) {

function sortableController($routeParams, WidgetService) {
var vm = this;
vm.sort = sort;
var pageId = $routeParams.pid;

/**
* sorts the widgets based on the initial and final index
* @param start
* @param end
*/
function sort(start, end) {
WidgetService.sort(start, end);
WidgetService
.sort(start, end, pageId)
.success(function (success) {

})
.error(function (error) {

});
}

}
Expand Down
11 changes: 9 additions & 2 deletions public/assignment/services/widget.service.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,15 @@
return $http.delete(url);
}

function sort(start, end) {
var url = "/api/page/:pageId/widget?initial=index1&final=index2";
/**
* sorts the widgets on a given page
* @param start
* @param end
* @param pageId
* @returns {*}
*/
function sort(start, end, pageId) {
var url = "/api/page/"+pageId+"/widget?initial=index1&final=index2";
url = url
.replace("index1", start)
.replace("index2", end);
Expand Down

0 comments on commit e1104ee

Please sign in to comment.