Skip to content

Instantly share code, notes, and snippets.

Created March 6, 2014 00:10
Show Gist options
  • Save anonymous/9379477 to your computer and use it in GitHub Desktop.
Save anonymous/9379477 to your computer and use it in GitHub Desktop.
###WHAT WAS CHANGED BEFORE - CONTROLLER ###
bbControllers.controller 'CacheListController',
['$scope', 'Restangular', '$rootScope', 'Snippet', '$modal', '$templateCache', "$window", 'Widget', '$sce',
'$location', '$route', 'Session'
($scope, Restangular, $rootScope, Snippet, $modal, $templateCache, $window, Widget, $sce, $location, $route,Session) ->
$scope.newSnippet = {}
$scope.currentEditedHtmlFiles = []
findIndex = (array, callback) ->
_i = 0
while _i < array.length
return _i if callback(array[_i])
_i++
-1
$scope.saveCurrentHtmlTemplate = () ->
$scope.isEdited = true
$('#highlighter').empty()
$scope.currentHtmlTemplate.body = $scope.currentHtmlTemplate.edited_body
currentHtmlTemplatePositionInArray = findIndex $scope.currentEditedHtmlFiles, (element) ->
return element.file_name == $scope.currentHtmlTemplate.file_name
if currentHtmlTemplatePositionInArray == -1
currentHtmlTemplatePositionInArray = $scope.currentEditedHtmlFiles.length
$scope.currentEditedHtmlFiles[currentHtmlTemplatePositionInArray] = $scope.currentHtmlTemplate
console.log currentHtmlTemplatePositionInArray
$scope.saveTemplate($scope.currentHtmlTemplate)
getControllerScope "BBCtrl", (scope) ->
scope.current_page
scope.showPage scope.current_page
$scope.saveTemplate = (template) ->
$templateCache.put(template.file_name, template.body)
$scope.generateURL = () ->
Widget.saveWidget($scope.currentWidget, $scope.currentEditedHtmlFiles,
$scope.currentCssTemplate,$scope.currentJsTemplate ).then (newWidget) ->
console.log newWidget
$scope.currentWidget = newWidget
Widget.url = newWidget.url
$scope.setIds(newWidget)
$window.location.hash = (newWidget.url)
$scope.register = () ->
Widget.saveWidget($scope.currentWidget).then (newWidget) ->
$scope.currentWidget = newWidget
Widget.url = newWidget.url
$scope.setIds(newWidget)
$window.location.href = ('/users/sign_up')
$scope.updateWidget = () ->
Widget.updateWidget($scope.currentWidget, $scope.currentEditedHtmlFiles,
$scope.currentCssTemplate, $scope.currentJsTemplate).then (newWidget) ->
console.log newWidget
$scope.currentWidget = newWidget
Widget.name = newWidget.name
$scope.setIds(newWidget)
]
###WHAT WAS CHANGED BEFORE - FACTORY ###
angular.module('BBPlnkr').factory 'Widget', ['$templateCache', 'Restangular', 'Session', ($templateCache, Restangular, Session) ->
_baseWidgets
_baseWidgets = Restangular.all('api/widgets')
_userWidgets = []
updateWidget: (widget, html, css, js ) ->
widget.snippets_attributes = [
{
id: css.id
body: css.body
file_name: css.file_name
file_extension: 'css'
},{
id: js.id
body: js.body
file_name: js.file_name
file_extension: 'js'
}
]
for x in html
widget.snippets_attributes.push {id: x.id, body: x.body, file_name: x.file_name, file_extension: x.file_extension}
delete widget['snippets']
Restangular.copy(widget).put();
widget.put()
]
### WHAT WAS CHANGED - CONTROLLER ###
findIndex = (array, callback) ->
_i = 0
while _i < array.length
return _i if callback(array[_i])
_i++
-1
$scope.saveCurrentHtmlTemplate = () ->
$scope.isEdited = true
$('#highlighter').empty()
$scope.currentHtmlTemplate.body = $scope.currentHtmlTemplate.edited_body
currentHtmlTemplatePositionInArray = findIndex $scope.currentEditedHtmlFiles, (element) ->
return element.file_name == $scope.currentHtmlTemplate.file_name
if currentHtmlTemplatePositionInArray == -1
currentHtmlTemplatePositionInArray = $scope.currentEditedHtmlFiles.length
$scope.currentEditedHtmlFiles[currentHtmlTemplatePositionInArray] = $scope.currentHtmlTemplate
console.log currentHtmlTemplatePositionInArray, 'SOME TEXT HERE'
$scope.saveTemplate($scope.currentHtmlTemplate)
getControllerScope "BBCtrl", (scope) ->
scope.current_page
scope.showPage scope.current_page
### FACTORY CALL FOR WHAT WAS CHANGED ###
# Pseudo-code:
# For all elements in html
# if there is a matching one in snippets_attributes, update it
# else add it
#Most probably need to define findIndex Inside the factory also...
#Let me teporarily put the
findIndex = (array, callback) ->
_i = 0
while _i < array.length
return _i if callback(array[_i])
_i++
-1
for x in html
positionOfxInSnippets = findIndex widget.snippets_attributes, (element) ->
return element.file_name == widget.snippets_attributes.file_name
if positionOfxInSnippets == -1
positionOfxInSnippets = widget.snippets_attributes.length
console.log widget.snippets_attributes.length, 'IN THE FACTORY'
widget.snippets_attributes[positionOfxInSnippets] = {} #But i think you could do equals
widget.snippets_attributes[positionOfxInSnippets].id = x.id
widget.snippets_attributes[positionOfxInSnippets].body = x.body
widget.snippets_attributes[positionOfxInSnippets].file_name = x.file_name
widget.snippets_attributes[positionOfxInSnippets].file_extension = x.file_extension
#delete widget.snippets_attributes['partial_name'] #? go for it//
delete widget['snippets'] #This is to delete an attribute from the array it (snipppets)
Restangular.copy(widget).put();
#Well I wouldn't know what to change
widget.put()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment