(function () {
'use strict';
angular.module('edit-song', [])
.directive('editSong', ['$uibModal', function ($uibModal) {
return {
restrict: 'A',
scope: false,
link: function (scope, element, attr) {
/*Open the modal and pass upload data to the instance*/
element.bind('click', function () {
var modalInstance = $uibModal.open({
animation: true,
templateUrl: '/app/ng-templates/editSong.html',
controller: 'editSongCtrl',
size: 'md',
windowClass: 'editModal',
resolve: {
song: function () {
return scope.song;
}
}
});
})
}
}
}])
.controller('editSongCtrl', ['$scope', '$timeout', '$http', '$state', '$sce', '$uibModalInstance', 'song', 'songsFactory', 'userFactory', 'searchFactory', function ($scope, $timeout, $http, $state, $sce, $uibModalInstance, song, songsFactory, userFactory, searchFactory) {
$scope.song = song;
$scope.formValid=false;
$scope.editInfo=true;
$scope.editTags=false;
var songcopy = angular.copy($scope.song);
var currentGenre = songcopy.genre;
var currentInst = songcopy.instrument;
var currentBpm = songcopy.bpmValue;
var currentKey = songcopy.musickey;
$scope.tagsData={};
$scope.tagsData.keys = ['A', 'A#', 'B', 'B#', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#'];
var loadedGenres=false;
var loadedInstruments=false;
$scope.$watch('editTags', function (et) {
if (et) {
if (!loadedGenres) {
$http.get('/api/songbox.php?action=genre_list').success(function (genres) {
$scope.tagsData.genres = genres.available;
loadedGenres=true;
})
}
if (!loadedInstruments) {
LoadInstruments();
}
}
})
$scope.tagsData.instrumentHover='';
$scope.tagsData.instrumentMacro=false;
function LoadInstruments() {
$http.get('/api/songbox.php?action=instrument_list').success(function (instruments) {
// $scope.tagsData.instruments = instruments.available;
var instrumentMacros = [];
var pushedIds=[];
angular.forEach(instruments.available, function (i) {
if (pushedIds.indexOf(i.group)>-1) {
angular.forEach(instrumentMacros, function (iM) {
if (iM.id == i.group) {
iM.instruments.push({
icon: i.icon,
instrument: i.name,
selected: $scope.song.instrument == i.name ? true : false
})
}
})
return;
} else {
pushedIds.push(i.group);
instrumentMacros.push({
id: i.group,
macro: i.group_name,
icon: i.icon,
selected: $scope.song.insticon == i.icon ? true : false,
mainInstrument: i.name,
instruments: [
{
icon: i.icon,
instrument: i.name,
selected: $scope.song.instrument == i.name ? true : false
}
]
})
}
});
$scope.tagsData.instrumentMacros = instrumentMacros;
console.log('insts loaded', $scope.tagsData.instrumentMacros)
if ($scope.song.instrument && $scope.song.instrument.length) {
var selectedMacro;
angular.forEach($scope.tagsData.instrumentMacros, function (m) {
if (m.selected) {
selectedMacro=m;
}
})
$scope.tagsData.instrumentMacro = selectedMacro;
} else {
$scope.tagsData.instrumentMacro=false;
}
console.log('current macro', $scope.tagsData.instrumentMacro)
loadedInstruments=true;
})
}
$scope.notesInit=$sce.trustAsHtml($scope.song.notesOriginal.replace(/ /g, ' \u00A0'));
console.log('NOTES HERE', 'original', $scope.song.notesOriginal, 'notes', $scope.song.notes, 'init:', $scope.notesInit)
$scope.ControlNotesEnter=function ($evt, dropdownOpen) {
if ($evt.keyCode == 13 && dropdownOpen) {
$evt.preventDefault();
return false;
}
}
$scope.SwitchMacro = function (m) {
angular.forEach($scope.tagsData.instrumentMacros, function (im) {
if (im.macro==m.macro) {
im.selected=true;
angular.forEach(im.instruments, function (i) {
if (i.instrument==im.mainInstrument) {
i.selected=true;
$scope.song.instrument=im.mainInstrument;
} else {
i.selected=false;
}
})
} else {
im.selected=false;
}
})
$scope.tagsData.instrumentMacro=m;
$scope.CheckIfFormValid();
}
$scope.bpmInvalid = false;
$scope.ValidateBpm = function () {
var bpm = $scope.song.bpmValue;
console.log('validate bpm', bpm)
if (typeof bpm == 'undefined') {console.log('bpm error');$scope.bpmInvalid=true;$scope.$apply()}
if (bpm == "" || bpm == null) {
$scope.bpmInvalid = false;
$scope.bpmValid= false;
} else {
if (bpm >= 40 && bpm <= 240) {
$scope.bpmValid = true;
$scope.bpmInvalid = false;
} else {
$scope.bpmInvalid = true;
$scope.bpmValid = false;
}
}
}
$scope.song.showCoverArtists = false;
$scope.song.showCoverSongs = false;
$scope.song.isCover = $scope.song.cover_artistid ? true : false;
$scope.song.coverSong = $scope.song.isCover ? $scope.song.name : null;
$scope.song.coverArtist = $scope.song.cov_art_name ? $scope.song.cov_art_name : '';
var wasCover = songcopy.isCover;
var currentCoverArtist = songcopy.cov_art_name || null;
$scope.$watch('notesInit', function (nt) {
$scope.song.notesOriginal=nt;
})
$scope.$watch('song.notesOriginal', function (nt) {
console.log('notes original change', nt);
})
$scope.coverArtistsLoading=false;
$scope.FindArtists = function (query) {
console.log('QUERY', query)
if (!query || query == "") {
return false;
} else {
$scope.coverArtistsLoading=true;
query=query.trim();
return $http.get('https://musicbrainz.org/ws/2/artist?query='+query+'*&fmt=json').then(function (response) {
response.data=response.data.artists;
//console.log(response);
$scope.coverArtistsLoading=false;
return response;
});
}
}
$scope.coverSongsLoading=false;
$scope.FindSongs = function (query) {
console.log('QUERY', query)
if (!query || query == "") {
return false;
} else {
$scope.coverSongsLoading=true;
query=query.trim();
var url = $scope.song.coverArtist && $scope.song.coverArtist.length ? 'https://musicbrainz.org/ws/2/recording?query='+query+'* AND artist:'+ $scope.song.coverArtist +'&fmt=json' : "https://musicbrainz.org/ws/2/recording?query="+query+"*&fmt=json";
return $http.get(url).then(function (response) {
console.log(response);
var alreadyMentioned = [];
var songs=response.data.recordings;
var songsClean = [];
angular.forEach(songs, function (r) {
if (alreadyMentioned.indexOf(r.title)>=0) {return} else {
alreadyMentioned.push(r.title);
songsClean.push(r);
}
})
response.data.recordings=songsClean;
response.data=response.data.recordings;
$scope.coverSongsLoading=false;
return response;
});
}
}
$scope.SelectCoverArtist = function (tag) {
console.log(tag);
$scope.song.coverArtist=tag.name;
$scope.song.showCoverArtists=false;
$scope.CheckIfFormValid();
//$scope.song.showCoverSongs=true;
return false;
}
$scope.SelectCoverSong = function (tag) {
console.log(tag);
$scope.song.coverSong=tag.title;
$scope.song.showCoverSongs=false;
$scope.CheckIfFormValid();
//$scope.song.showCoverSongs=true;
return false;
}
var currentImage = songcopy.picture;
var currentName = songcopy.name;
var currentNotes = songcopy.notesOriginal;
var currentLyrics = songcopy.lyrics;
console.log('songcopy in edit', songcopy)
var wasInstrumental = songcopy.want_inst && $.inArray('Vocals', songcopy.want_inst.split('|'))>-1 ? true : false;
console.log(wasInstrumental, 'wasInstrumental')
$scope.song.isCoverInstrumental=wasInstrumental;
$scope.uploadMode=false;
$scope.userImages = [];
userFactory.GetUserPhotos($scope.song.artistid, function (images) {
$scope.userImages = images;
})
$scope.PickSongPhoto = function (imageid, imageurl) {
console.log('picked song', imageid, imageurl)
$scope.song.picture = imageurl;
$scope.newPictureId= imageid;
}
$uibModalInstance.closed.then(function() {
if (!$scope.confirmed) {
console.log('not confirmed')
$scope.song.picture=currentImage;
$scope.song.name = currentName;
$scope.song.notes = currentNotes;
$scope.song.instrument = currentInst;
$scope.song.genre = currentGenre;
$scope.song.musickey = currentKey;
$scope.song.bpmValue=currentBpm;
}
});
$scope.CheckIfFormValid = function () {
// console.log('checking form_________________________________________________________________')
if ($scope.song.coverArtist=='') {$scope.song.coverArtist=null}
/*
console.log('iscover: ', wasCover, $scope.song.isCover);
if (wasCover==$scope.song.isCover) {console.log('___wascover unchanged')}
console.log('name: ', currentName, $scope.song.name);
if (currentName==$scope.song.name) {console.log('___name unchanged')}
console.log('notes: ', currentNotes, $scope.song.notesOriginal);
if (currentNotes==$scope.song.notesOriginal) {console.log('___notes unchanged')}
console.log('new img: ', $scope.newAddedImg);
if (!$scope.newAddedImg) {console.log('___no new img')}
console.log('cover artist: ', currentCoverArtist, $scope.song.coverArtist);
if (currentCoverArtist==$scope.song.coverArtist) {console.log('___cover artist unchanged')}
console.log('image: ', currentImage, $scope.song.picture);
if (currentImage==$scope.song.picture) {console.log('___image unchanged')}
console.log('genre: ', currentGenre, $scope.song.genre);
if (currentGenre==$scope.song.genre) {console.log('___genre unchanged')}
console.log('instrument: ', currentInst, $scope.song.instrument);
if (currentInst==$scope.song.instrument) {console.log('___instrument unchanged')}
console.log('bpm: ', currentBpm, $scope.song.bpmValue);
if (currentBpm==$scope.song.bpmValue) {console.log('___bpm unchanged')}
console.log('key: ', currentKey, $scope.song.musickey);
if (currentKey==$scope.song.musickey) {console.log('___key unchanged')}
console.log('checked form_________________________________________________________________')*/
if (
currentName==$scope.song.name
&& currentName==$scope.song.coverSong
&& wasCover==$scope.song.isCover
&& currentNotes==$scope.song.notesOriginal
&& !$scope.newAddedImg
&& currentCoverArtist==$scope.song.coverArtist
&& currentImage == $scope.song.picture
&& wasInstrumental == $scope.song.isCoverInstrumental
&& currentLyrics == $scope.song.lyrics
&& currentGenre == $scope.song.genre
&& currentInst == $scope.song.instrument
&& (currentBpm == $scope.song.bpmValue || !$scope.bpmValid)
&& currentKey == $scope.song.musickey
&& !$scope.newImgDropped
) {$scope.formValid=false;console.log('form invalid')} else {
$scope.formValid=true;console.log('form valid');
}
}
$scope.SaveChanges = function () {
if ($scope.song.isCover) {$scope.song.name = $scope.song.coverSong}
console.log(currentName, $scope.song.name, $scope.song.coverArtist, wasCover, $scope.song.isCover);
if (!$scope.formValid) {return}
$scope.saving = true;
$scope.confirmed=true;
$scope.completed=false;
// console.log(currentNotes, $scope.song.notesOriginal)
// console.log(currentImage, $scope.song.picture)
// console.log(currentName, $scope.song.name)
// console.log($scope.song.instrument, $scope.song.bpm, $scope.song.musickey)
var updateMeta = false;
var updateImg = false;
var metaUpdated = false;
var imageUpdated = false;
var updateWishlist=false;
if (currentName !== $scope.song.name || currentName!==$scope.song.coverSong || currentCoverArtist!==$scope.song.coverArtist || currentNotes !== $scope.song.notesOriginal || wasCover!==$scope.song.isCover || currentGenre!==$scope.song.genre || currentInst !== $scope.song.instrument || currentBpm !== $scope.song.bpmValue || currentKey !== $scope.song.musickey || currentLyrics !== $scope.song.lyrics) {
updateMeta=true;
} else {
$scope.metaUpdated=true;
}
if (currentImage!==$scope.song.picture) {
updateImg=true;
} else {
$scope.imageUpdated = true;
}
var currentWishlist = songcopy.want_inst.split('|');
if (currentWishlist.length==1 && currentWishlist[0]=="") {
currentWishlist=[];
}
console.log('currentWishlist', currentWishlist)
var newWishlist;
if (wasInstrumental !== $scope.song.isCoverInstrumental) {
updateWishlist=true;
if ($scope.song.isCoverInstrumental) {
currentWishlist.push('Vocals');
console.log('currentWishlist2', currentWishlist)
newWishlist=currentWishlist.join('|');
console.log('newWishlist', newWishlist)
} else {
newWishlist=currentWishlist
}
}
var coverArtist = $scope.song.isCover ? $scope.song.coverArtist : false;
if (updateMeta) {
console.log('NOTES ORIGINAL BEFORE REPACE', $scope.notesInit);
$scope.song.notesOriginal= $scope.song.notesOriginal.replace(/contenteditable="false" /g, "").replace(/class="mention" /g, "");
console.log('NOTES ORIGINAL BEFORE UPDATE', $scope.song.notesOriginal);
var txt = document.createElement("textarea");
txt.innerHTML = $scope.song.notesOriginal;
$scope.song.notesOriginal=txt.value;
songsFactory.EditSongMeta($scope.song.id, $scope.song.name, $scope.song.notesOriginal, $scope.song.lyrics, $scope.song.genre, $scope.song.instrument, $scope.song.bpmValue, $scope.song.musickey, coverArtist, function () {
$scope.metaUpdated=true;
$scope.saving=false;
if (updateWishlist) {
songsFactory.UpdateWantList($scope.song.id, newWishlist, function () {
if (!updateImg) {
$scope.Done();
$timeout(function(){
$state.reload();
},500)
}
})
} else {
if (!updateImg) {
$scope.Done();
$timeout(function(){
$state.reload();
},500)
}
}
})
}
if (updateImg) {
if ((!$scope.uploadMode && !$scope.newAddedImg)) { // || ($scope.uploadMode && $scope.song.picture!==currentImage) ) {
songsFactory.EditSongImage($scope.song.id, $scope.newPictureId, function () {
$scope.imageUpdated=true;
$scope.saving=false;
$scope.Done();
$timeout(function(){
$state.reload();
},500)
})
} else {
// console.log($scope.newAddedImg, 'NEW ADDED');
userFactory.PostUserPhoto($scope.newAddedImg, 0, function (result) {
var newImgId = result.imageid;
songsFactory.EditSongImage($scope.song.id, newImgId, function () {
$scope.imageUpdated=true;
$scope.saving=false;
$scope.Done();
$timeout(function(){
$state.reload();
},500)
})
})
}
}
}
$scope.$watch('imageUpdated', function (iu) {
if ($scope.saving) {return}
if (iu == true && $scope.metaUpdated == true) {
$scope.completed = true;
}
})
$scope.$watch('metaUpdated', function (mu) {
if ($scope.saving) {return}
if (mu==true && $scope.imageUpdated == true) {
$scope.completed=true;
}
})
$scope.Done = function () {
$uibModalInstance.dismiss();
/*
songsFactory.GetSong($scope.song.id, function (s) {
$scope.song = s;
$scope.$apply();
}) */
}
$scope.songImgDropzoneConfig = {
'options': { // passed into the Dropzone constructor
url: "/api/upload_graphic.php",
type: "POST",
uploadMultiple: false,
addRemoveLinks: false,
maxFiles: 1,
clickable: true,
forceFallback: false,
thumbnailHeight: 200,
thumbnailWidth: 200,
autoProcessQueue: false,
previewsContainer: ".preview_container",
previewTemplate: '
\
\
\
\
',
accept: function(file, done) {
// console.log(file);
$scope.newImgDropped=true;
$scope.$apply();
$scope.CheckIfFormValid();
if (file.type != "image/jpeg" && file.type != "image/png" && file.type != "image/jpg") {
done("Error! Files of this type are not accepted");
return;
}
else {
done();
$scope.newAddedImg = file;
}
},
fallback: function () {
$('#avatar_dropzone, #current_avatar').hide();
$('').insertAfter('#current_avatar');
$('#avatar_fallback input').change(function(){
$('#avatar_fallback button').show()
})
}
},
'eventHandlers': {
'sending': function (file, xhr, formData) {
console.log('DZ sendint', file, form)
},
'success': function (file, response) {
console.log('DZ success', file, response)
},
'drop': function (file, msg) {
//console.log(file, msg)
console.log('DZ drop', file, msg)
},
'removedfile': function () {
$scope.newAddedImg = null;
$scope.newImgDropped = false;
$scope.$apply();
$scope.CheckIfFormValid();
},
'dragover': function(){
$(".droparea").addClass("active_drop");
},
'dragleave': function(){
$(".droparea").removeClass("active_drop");
}
}
};
$scope.$watch('newImgDropped', function (ni) {
if (ni) {
$timeout(function (){
var nisrc = $('img[data-dz-thumbnail]').attr('src');
// console.log(nisrc);
$scope.song.picture=nisrc;
},500)
} else {
$scope.song.picture=currentImage;
}
})
}])
}());