Skip to content

Commit

Permalink
Merge pull request #394 from teixas/code-quality
Browse files Browse the repository at this point in the history
Improve Code quality
  • Loading branch information
disko committed Feb 23, 2015
2 parents df79824 + c7ade7b commit 818b38a
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 139 deletions.
14 changes: 7 additions & 7 deletions kotti/static/kotti.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

"use strict";
var kotti = {
dom_changed_handlers: []
domChangedHandlers: []
};
var jq = jQuery;

Expand All @@ -15,7 +15,7 @@ var jq = jQuery;
return this.filter(selector).add(this.find(selector));
};

kotti.dirty_forms = function (node) {
kotti.dirtyForms = function (node) {
var forms = $("form").not("[class~=dirty-ignore]"),
initial = forms.serialize();

Expand All @@ -35,8 +35,8 @@ var jq = jQuery;
});
};

kotti.dom_changed = function (node) {
$.each(kotti.dom_changed_handlers, function (index, func) {
kotti.domChanged = function (node) {
$.each(kotti.domChangedHandlers, function (index, func) {
func(node);
});
};
Expand All @@ -48,14 +48,14 @@ var jq = jQuery;
];
}
$.each(handlers, function (index, func) {
kotti.dom_changed_handlers.push(func);
kotti.domChangedHandlers.push(func);
});
kotti.dom_changed(node);
kotti.domChanged(node);
};

// deform might be undefined, e.g. in kotti_tinymce's kottibrowser
if (window.deform) {
deform.load();
deform.load();
}

kotti.main();
Expand Down
2 changes: 1 addition & 1 deletion kotti/static/kotti.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

267 changes: 136 additions & 131 deletions kotti/static/upload.js
Original file line number Diff line number Diff line change
@@ -1,143 +1,148 @@
/*jshint multistr:true */
// JSLint options:
/*global $, angular, document, qq*/

"use strict";
var app = angular.module('kotti', []);

// copied from https://gist.github.com/thomseddon/3511330
app.filter('bytes', function() {
return function(bytes, precision) {
app.filter('bytes', function () {
return function (bytes, precision) {

if (isNaN(parseFloat(bytes)) || !isFinite(bytes)) {
return '-';
}
if (typeof precision === 'undefined') {
precision = 1;
}

var units = ['bytes', 'kB', 'MB', 'GB', 'TB', 'PB'],
number = Math.floor(Math.log(bytes) / Math.log(1024));
if (!isFinite(number)) {
number = 0;
}

return (bytes / Math.pow(1024, Math.floor(number))).toFixed(precision) + ' ' + units[number];
};
});

if (isNaN(parseFloat(bytes)) || !isFinite(bytes)) return '-';
if (typeof precision === 'undefined') precision = 1;
function UploadController($scope, $http, $log) {

var units = ['bytes', 'kB', 'MB', 'GB', 'TB', 'PB'];
var number = Math.floor(Math.log(bytes) / Math.log(1024));
if (!isFinite(number)) number = 0;
$log.info("Initializing UploadController...");

return (bytes/Math.pow(1024, Math.floor(number))).toFixed(precision) + ' ' + units[number];
};
});
$scope.files = [];
$scope.errors = [];
$scope.numFilesWaiting = 0;

$scope.uploadAll = function () {
$scope.uploader.uploadStoredFiles();
};

$scope.dismissError = function (file) {
$scope.errors.splice($scope.errors.indexOf(file), 1);
};

function UploadController ($scope, $http, $log) {

$log.info("Initializing UploadController...");

$scope.files = [];
$scope.errors = [];
$scope.num_files_waiting = 0;

$scope.uploadAll = function() {
$scope.uploader.uploadStoredFiles();
};

$scope.dismissError = function(file) {
$scope.errors.splice($scope.errors.indexOf(file), 1);
};

$scope.apply = function(fn) {
var phase = this.$root.$$phase;
if(phase == '$apply' || phase == '$digest') {
if(fn && (typeof(fn) === 'function')) {
fn();
}
} else {
this.$apply(fn);
}
};

$scope.uploader = new qq.FineUploaderBasic({
debug: true,
autoUpload: false,
button: $('#btn-select-files')[0],
element: document.getElementById('uploader'),
request: {
endpoint: null
},
callbacks: {
onValidate: function(fileOrBlobData) {
$log.info("onValidate");
},
onSubmit: function(id, name) {
$log.info("onSubmit");
$scope.apply(function() {
var file = $scope.uploader.getFile(id);
$http.get(
$scope.endpoints.content_types,
{params: {mimetype: file.type}})
.success(function(data, status, headers, config) {

var content_types = data.content_types;

var file = {
id: id,
name: name,
size: $scope.uploader.getSize(id),
file: $scope.uploader.getFile(id)
};

if (content_types.length === 0) {
// todo: display meaningful error message
file.status = 'Error';
file.error = 'There is no content type in this context that knows create items from that file type.';
$scope.errors.splice(id, 0, file);
return false;
$scope.apply = function (fn) {
var phase = this.$root.$$phase;
if (phase === '$apply' || phase === '$digest') {
if (fn && (typeof (fn) === 'function')) {
fn();
}
} else {
this.$apply(fn);
}
};

$scope.uploader = new qq.FineUploaderBasic({
debug: true,
autoUpload: false,
button: $('#btn-select-files')[0],
element: document.getElementById('uploader'),
request: {
endpoint: null
},
callbacks: {
onValidate: function (fileOrBlobData) {
$log.info("onValidate");
},
onSubmit: function (id, name) {
$log.info("onSubmit");
$scope.apply(function () {
var file = $scope.uploader.getFile(id);
$http.get(
$scope.endpoints.contentTypes,
{params: {mimetype: file.type}}
).success(function (data, status, headers, config) {
var contentTypes = data.contentTypes,
file = {
id: id,
name: name,
size: $scope.uploader.getSize(id),
file: $scope.uploader.getFile(id)
};

if (contentTypes.length === 0) {
// todo: display meaningful error message
file.status = 'Error';
file.error = 'There is no content type in this context that knows create items from that file type.';
$scope.errors.splice(id, 0, file);
return false;
}

file.status = 'ready for upload';
file.transfered = {bytes: 0, percent: 0};
file.allowedTypes = contentTypes;
file.desiredType = contentTypes[0];

$scope.files.splice(id, 0, file);
$scope.numFilesWaiting += 1;
});
});
},
onUpload: function (id, name) {
$log.info("onUpload");
$scope.apply(function () {
$scope.files[id].status = 'uploading';
$scope.uploader.setParams({
contentType: $scope.files[id].desiredType.name
}, id);
$scope.numFilesWaiting -= 1;
});
},
onProgress: function (id, name, uploadedBytes, totalBytes) {
$scope.apply(function () {
$scope.files[id].transfered.bytes = uploadedBytes;
$scope.files[id].transfered.percent = Math.round(uploadedBytes / totalBytes * 100);
});
},
onCancel: function (id, name) {
$log.info("onCancel");
$scope.apply(function () {
$scope.files[id].status = 'cancelled';
});
},
onError: function (id, name, errorReason) {
$log.info("onError");
$scope.apply(function () {
$scope.files[id].status = 'failed';
});
return false;
},
onComplete: function (id, name, response) {
$log.info("onComplete");
// debugger;
$scope.apply(function () {
if ($scope.files[id].status === 'uploading') {
$scope.files[id].status = 'complete';
$scope.files[id].url = response.url;
}
});
}
}
});

$scope.$watch('endpoints', function (endpoints) {
$scope.uploader.setEndpoint(endpoints.upload);
});

file.status = 'ready for upload';
file.transfered = {bytes: 0, percent: 0};
file.allowed_types = content_types;
file.desired_type = content_types[0];

$scope.files.splice(id, 0, file);
$scope.num_files_waiting ++;
});

});
},
onUpload: function(id, name){
$log.info("onUpload");
$scope.apply(function() {
$scope.files[id].status = 'uploading';
$scope.uploader.setParams(
{content_type: $scope.files[id].desired_type.name}, id);
$scope.num_files_waiting --;
});
},
onProgress: function(id, name, uploadedBytes, totalBytes){
$scope.apply(function() {
$scope.files[id].transfered.bytes = uploadedBytes;
$scope.files[id].transfered.percent = Math.round(uploadedBytes / totalBytes * 100);
});
},
onCancel: function(id, name) {
$log.info("onCancel");
$scope.apply(function() {
$scope.files[id].status = 'cancelled';
});
},
onError: function(id, name, errorReason) {
$log.info("onError");
$scope.apply(function() {
$scope.files[id].status = 'failed';
});
return false;
},
onComplete: function(id, name, response){
$log.info("onComplete");
// debugger;
$scope.apply(function() {
if ($scope.files[id].status == 'uploading') {
$scope.files[id].status = 'complete';
$scope.files[id].url = response.url;
}
});
}
}
});

$scope.$watch('endpoints', function(endpoints) {
$scope.uploader.setEndpoint(endpoints.upload);
});

$log.info("UploadController initialized.");
$log.info("UploadController initialized.");
}

0 comments on commit 818b38a

Please sign in to comment.